Forum
CS2D Scripts How does mousemap work?How does mousemap work?
10 replies 1
mousemapx and mousemapy represent the current mouse position in the game world (relative to the currently visible part of the map).
So the math is:
1
2
2
mouseMapX = ownPlayerX - screenWidth/2 + mouseX mouseMapY = ownPlayerY - screenHeight/2 + mouseY
The screenWidth/2 and screenHeight/2 part is because the player is at the center of the screen.
Rule of thumb:
Use mousex/mousey if you want to display HUD images/texts at the mouse position
Use mousemapx/mousemapy for everything else e.g. spawning things on the map at the mouse position.
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
local mouseMapX = player(myID, "x") - player(myID, "screenw")/2 + player(myID, "mousex") local mouseMapY = player(myID, "y") - player(myID, "screenh")/2 + player(myID, "mousey") addhook("second", "OnSecond") OnSecond = (function() 	if (mouseMapX == player(enemyID, "x") and mouseMapY == player(enemyID, "y")) then 		msg("Yes") 	else 		msg("No") 	end end)
Would be appreciated
You don't have to (and you shouldn't!) calculate that stuff yourself. There's mousemapx and y so you don't have to do it yourself!
2. You are only calculating the position once. You would have to do it each time you want to check it (inside the hook) because it's changing all the time.
3. The position is in pixels. When you are comparing pixel coordinates you would have to point exactly at one specific pixel of an enemy to make that stuff work. You should calculate the distance between player and mouse and check if that is below a certain threshold like 20 pixels or something.
DC has written
3. The position is in pixels. When you are comparing pixel coordinates you would have to point exactly at one specific pixel of an enemy to make that stuff work. You should calculate the distance between player and mouse and check if that is below a certain threshold like 20 pixels or something.
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
addhook("always", "OnAlways") OnAlways = (function() 	if (player(myID, "mousemapx") == player(enemyID, "x") and player(myID, "mousemapy") == player(enemyID, "y")) then 		parse("hudtxt 0 Yes") 	else 		parse("hudtxt 0 No") 	end end)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
addhook("always", "OnAlways") function onAlways() 	--[[ 	Some way to get myID and enemyID 	]] 	-- Check if it's within 20 pixel threshold 	local inrangex, inrangey 	inrangex = player(myID,"mousemapx") >= player(enemyID,"x")-10 and player(myID,"mousemapx") <= player(enemyID,"x")+10 	inrangey = player(myID,"mousemapy") >= player(enemyID,"y")-10 and player(myID,"mousemapy") <= player(enemyID,"y")+10 	if inrangex and inrangey then 		parse("hudtxt 0 Yes") 	else 		parse("hudtxt 0 No") 	end end
I guess ms100 (that's 10 times a second instead of 50 times a second) should be ok too.
Even better would be to save the displayed state and to only call that parse("hudtxt ...") (or whatever you do to visualize things on the client side) when the displayed stuff really changed.
DC has written
I guess ms100 (that's 10 times a second instead of 50 times a second) should be ok too.
Isn't the always hook running 60 times a second?
Also it's sometimes a little higher (62~) or lower.
It's documented here always
1