Cuberite Forum
Loops - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: Loops (/thread-807.html)



Loops - NiLSPACE - 02-18-2013

i'm currently making a Away-From-Keyboard plugin but i need to check if the player moves. i made this loop:
   PlayerMoved = 0
	repeat
		if not( X == Player:GetPosX() ) and ( Y == Player:GetPosY() ) and ( Z == Player:GetPosZ()) then
			PlayerMoved = 1
			Player:SetName( Name[Player:GetName()] )
			Server:SendMessage( Player:GetColor() .. Player:GetName() .. cChatColor.White .. " is back")
		end
		LOG("TEST")
	until PlayerMoved == 1
but once it starts it never stops. even if i walk. and it kind of destroys the server becouse the server is only working on the loop. does anyone know how to fix it?
btw the X and Y and Z are coordinates that are put in once the player uses /afk

I also used:
while not( X == Player:GetPosX() ) and ( Y == Player:GetPosY() ) and ( Z == Player:GetPosZ()) do
			Player:SetName( Name[Player:GetName()] )
			Server:SendMessage( Player:GetColor() .. Player:GetName() .. cChatColor.White .. " is back")
end



RE: Loops - FakeTruth - 02-18-2013

Instead of creating a loop, which blocks all server logic, check per tick or better yet use the on player moved function


RE: Loops - NiLSPACE - 02-18-2013

there is no HOOK_PLAYER_MOVED hook there only is HOOK_PLAYER_MOVING but that activates even when you didn't move


RE: Loops - FakeTruth - 02-18-2013

Still you shouldn't do the checking in a loop, because nothing changes within that loopTongue You must let the server update the player positions first by returning in your function


RE: Loops - xoft - 02-18-2013

_MOVED or _MOVING, doesn't really matter for you here. Just use that callback, inside it check if the player has moved more than 0.01 blocks away from their "/afk" position and if so, break the afk.

And welcome to the very first of your threading issues Smile