Jump to content

[Question] Getting function to stop if element is not a certain rotation


kieran

Recommended Posts

I am currently trying to replace gates at KACC, I have a script that removes the gates, and creates new ones in their place, then when a player hits a col shape, gates open, the only problem is, I want to check if the gates are fully opened (at a certain rotation since moveObject adds/subtracts from current rotation) and only if they are, move them back, never really done rotations with moveObject, so this is a pretty hard thing to start on...

Here is server side code.

--Replace gates in KACC and add Col shape to open/close them
    removeWorldModel (985 , 7.3802581, 2497.2419, 2774.2747, 10.7167)
    removeWorldModel (986, 7.4280729, 2497.2119, 2768.4243, 11.38819)
	RGate = createObject (986, 2497.3999, 2769.0991, 11.53, 0, 0, 90)
    LGate = createObject (985, 2497.3999, 2777.0701, 11.53, 0, 0, 90)
	ColGate = createColCuboid (2487.15, 2765.28, 10, 20, 15.75, 5)

isGateOpen = false --By default this is false
local OpenTimer --In case I need to use these later to cancel timer
local CloseTimer 

function KACC_Open ( thePlayer, matchingDimension ) --Open gate
    if getElementType ( thePlayer ) == "player" then 
		if isTimer(OpenTimer) then --Checking for timer
			outputChatBox("Timer Found",thePlayer)
		end
		if isGateOpen == false then --If it's false
			x,y,z = getElementRotation(LGate) --Below if is how I tried fixing it, didn't work 
			if not z == 20 then
				return
			end
			moveObject(RGate, 2000, 2493.3999, 2766.7, 11.53, 0, 0, 70) --Move the gate and add 70 to z rotation
			moveObject(LGate, 2000, 2493.3999, 2779.6, 11.53, 0, 0, -70) --Move the gate and subtract 70 from z rotation
			OpenTimer = setTimer(function() isGateOpen = true end,2000,1) --Set timer to put boolean to true
		end	
    end
end

addEventHandler ( "onColShapeHit", ColGate, KACC_Open )	

function KACC_Close ( thePlayer, matchingDimension ) --Close gate
    if getElementType ( thePlayer ) == "player" then 
		if isGateOpen == true then --If it's true
			x,y,z = getElementRotation(LGate) --Below if is how I tried fixing it, didn't work
			if not z == 90 then
				return
			end
			moveObject (RGate, 2000, 2497.3999, 2769.0991, 11.53, 0, 0, -70) --Move the gate and subtract 70 from z rotation
			moveObject (LGate, 2000, 2497.3999, 2777.0701, 11.53, 0, 0, 70) --Move the gate and add 70 to z rotation
			CloseTimer = setTimer(function() isGateOpen = false end,2000,1) --Set a timer to put the boolean to false
		end	
    end
end

addEventHandler ( "onColShapeLeave", ColGate, KACC_Close )

I know where I went wrong, the 2 second timer isn't enough to set it true as player can run in and out in that time, but how would I fix it?  Because if I set it to true right away it moves too much the other way when I leave the col shape...

Thanks for any suggestions/fixes :D

Edited by kieran
Link to comment

The way I would do this, instead of having 2 states (opened/closed), I would have 3 states, opened/closed/moving. When I hit the marker and it's in the moving state, don't do anything, if it's in the open state you close it etc..

Edited by pa3ck
  • Thanks 1
Link to comment
9 hours ago, Tails said:

if not z == 20 then

Is what you're doing but z will probably never be 20 exactly, so do z >= 20 instead.


return

then you're returning but that doesn't stop the object

use stopObject() and then return it. Hope this helps.

It will actually be 20, when the player moves in col shape the gate rotates out and stops at 20 degrees, It's all done to an exact position, and the problem was my checking :P I was setting a 1.5 second timer to set my boolean's to true/false, so this means that a player could run into the col shape before the timer is complete, if you test the script it works fine, just needed tweaking, and as pa3ck said, I should of added a moving boolean and set it as soon as the gate starts moving...

The reason I check exact rotation is because moveObject sets an elements position, with the starting rotation being it's current rotation, so this was the easiest way I could think of...  But you make a great point, I must calculate it's current rotation and close it when the player leaves.

Anyway thanks to both of you for help, sorted it and working well. :D

Edited by kieran
Link to comment

It still may not be exactly 20 even if you set it as so, lua uses doubles(64 bit) to store numbers, which is floating point, the rotation is also floating point, but float(32 bit), at least internally in mta if i remember correctly. It may not be a problem in this case, but i suggest reading a but about floating point precision. Some good sites talking about that: http://0.30000000000000004.com/
a bit more technical: http://fabiensanglard.net/floating_point_visually_explained/

  • Thanks 1
Link to comment
1 hour ago, quindo said:

It still may not be exactly 20 even if you set it as so, lua uses doubles(64 bit) to store numbers, which is floating point, the rotation is also floating point, but float(32 bit), at least internally in mta if i remember correctly. It may not be a problem in this case, but i suggest reading a but about floating point precision. Some good sites talking about that: http://0.30000000000000004.com/
a bit more technical: http://fabiensanglard.net/floating_point_visually_explained/

Ah right, thank you, I am still trying to get my head around all the small stuff, this helps :P

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...