How to calculate others player screens to position something?

How to calculate others player screens to position something?

Is there like an easy way to do it and not a very complicated way?

People told me that I have to use ScrW() and ScrH() to devide or multiply it trough something but I am not actually sure how to do this and why I should do it because I tried it and it didn’t appear at the right position.

Please help

Could you be more specific on what you want to do? There’s no way to get another player’s screen width, height, or position without sending it all to the server first, and then to another player. There’s probably a better way of doing what you’re wanting to achieve.

I got everything from the wiki.

Here is a example what I calculated and yeah thats from the wiki:


	-- ScrW and ScrH
	-- Position (1360, 760) must get devided by my screen resolution.
	-- 1350 : 1920 = 0.703125
	-- 760 : 1080 = 0.7037037037
	-- now I have to round the decimals to 3 decimal points.
	-- 0.703 and 0.704

So here is the resulting code thing:


SetPos( ScrW() * 0.703, ScrH() * 0.639 );

The position I used were 1360, 690
and they didnt got placed where I wanted them to be at.
That upper example is also the same thing just with other positions.

In a wiki they said like devide the orignal position through the screen I think and then multiply it because other people have different screen sizes. How does the derma thing even get placed from the middle of the position or from the bottom right corner?

-snip-

Get’s placed from the top left. So you essentially have this setup:

(0,0)---------------(ScrW(),0)
¦ ¦
¦ ¦
¦ ¦
¦ ¦
¦ ¦
(0,ScrH())---------(ScrW(),ScrH())

OP, what do you want to find? Do you want to calculate the position of a pixel/pixels based on screen information? Do you want to find the position of a player’s eyes? As others said, you need to be specific

If you’re looking for resolution based placement without too much work, it’s just

their ScrW() / your ScrW() * width

their ScrH() / your ScrH() * height

Just make a function that does it for you. Don’t be like me and keep a txt file with 70+ different heights and widths scaled because no one like looking at ScrH() * .38747 and you can’t even make adjustments until you convert it back. You can even make a function that you can use on all panels like



local pnl = FindMetaTable("Panel")
	
local fitW = 1920
local fitH = 1080
	
function pnl:SetScaledSize(w,h)
	self:SetSize(ScrW() / fitW * w, ScrH() / fitH * h)
end


Although this would only work if you’re using straight up numbers. Once you start adjusting panel sizes based on parent sizes then you would have to split it and make other functions that get only the height scaled and the width scaled since it would scale twice for other resolutions

I want to position a hud on the screen. I don’t know why I see SetSize? Why is there setsize what I think about SetSize is like you give the window a size and what I think about SetPos is like you position the window somewhere on the screen.

[editline]25th April 2017[/editline]

weird I cinda dont understand what you want to tell me?
Yes, it gets placed on the top left for some reason but You see that I multiplicate it with something too?

The whole multiplying thing is to make it work on any resolution. For example:
If you create a gui for your resolution, using fixed numbers, then on another resolution it won’t look the same. However, (0.5ScrW(),0.5ScrH()) will be at the middle of the screen, no matter what the resolution.

yes thats what this


SetPos( ScrW() * 0.703, ScrH() * 0.639 )

is about
but its not working

Wheres the rest the code? Also, just incase I am getting the wrong idea. You are trying to draw something on the players screen right?

This is the code I use as example.


	SetSize(50, 20);
	-- ScrW and ScrH
	-- Position (1360, 760) must get devided by my screen resolution.
	-- 1350 : 1920 = 0.703125
	-- 760 : 1080 = 0.7037037037
	-- now I have to round the decimals to 3 decimal points.
	-- 0.703 and 0.704
	SetPos( ScrW() * 0.703, ScrH() * 0.704 );

then I create a button with these settings dont worry I only hidded name: here


local Button1 = vgui.Create("DButton", Button1Frame);
	SetSize(50, 20);
	SetVisible(true);
	Center()
	Button1.Paint = function(self, w, h)
		draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 255, 10) );
	end;

as you see



local Button1 = vgui.Create("DButton", Button1Frame);
	Button1:SetSize(50, 20);
	Button1:SetVisible(true);
	Button1:SetPos( ScrW() * 0.703, ScrH() * 0.704 );
	Button1.Paint = function(self, w, h)
		draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 255, 10) );
	end;


Don’t forget to make Button1Frame (a DFrame)

Actually I show you now a hud the others are the same just with other names.


-- Called when the panel is initialized.
function PANEL:Init()
	local LowVoltageButtonFrame = vgui.Create("DFrame");
	LowVoltageButtonFrame:SetSize(50, 20);
	-- ScrW and ScrH
	-- Position (1360, 760) must get devided by my screen resolution.
	-- 1350 : 1920 = 0.703125
	-- 760 : 1080 = 0.7037037037
	-- now I have to round the decimals to 3 decimal points.
	-- 0.703 and 0.704
	LowVoltageButtonFrame:SetPos( ScrW() * 0.703, ScrH() * 0.704 );
	LowVoltageButtonFrame:SetVisible(false);
	LowVoltageButtonFrame:SetDraggable(false);
	LowVoltageButtonFrame:SetText("");
	LowVoltageButtonFrame:ShowCloseButton(false);
	
	local LowVoltageButton = vgui.Create("DButton", LowVoltageButtonFrame);
	LowVoltageButton:SetSize(50, 20);
	LowVoltageButton:SetVisible(true);
	LowVoltageButton:Center()
	LowVoltageButton.Paint = function(self, w, h)
		draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 255, 10) );
	end;
end;

vgui.Register("cwLowVoltageButtonPanel", PANEL, "DPanel");

u dont because no one plays gmod on 4k

Can we still continue now?

Maybe, if you actually tell us what the problem is.
From what I can see you have a code that works, what’s the problem?

The things appeared on the top left of my screen and not where I wanted them to be at.

Can’t you just add or subtract from ScrW and ScrH instead of multiplying by them?

You definitely want to be scaling, not translating positions. 50% of ScrW and ScrH will be dead centre of everyone’s screen. There is no way of properly doing this without some form of multiplication involving ScrW and ScrH