Bonsai Posted September 10, 2010 Share Posted September 10, 2010 Hey all, I got another little problem, and I can't solve it by Wiki. I want a function to start its job, when a Player hits a marker. local level = 0 function drainSomeWater() level = level - 0.01 setWaterLevel ( level ) end setTimer ( drainSomeWater, 100, 15000 ) For example, that function triggered on Marker Hit. SetTimer is used here, to make the water go down slowly. So actually, I just need the Timer to start on Marker Hit. But I can't get it working. I hope someone understands the Problem, and knows how to fix it. Greetings Link to comment
DiSaMe Posted September 10, 2010 Share Posted September 10, 2010 Put setTimer into a function which is attached to onMarkerHit event. Link to comment
BinSlayer1 Posted September 10, 2010 Share Posted September 10, 2010 local level = 0 addEventHandler("onMarkerHit", getRootElement(), setTimer ( drainSomeWater, 100, 15000 ) ) function drainSomeWater() level = level - 0.01 setWaterLevel ( level ) end try this Link to comment
Discord Moderators Zango Posted September 10, 2010 Discord Moderators Share Posted September 10, 2010 local level = 0 [b]addEventHandler("onMarkerHit", getRootElement(), setTimer ( drainSomeWater, 100, 15000 ) )[/b] function drainSomeWater() level = level - 0.01 setWaterLevel ( level ) end try this I think you need a function for that. addEventHandler("onMarkerHit", getRootElement(), function(element) [b]if getElementType(element) == "player" then[/b] setTimer ( drainSomeWater, 100, 15000 ) end end ) I want a function to start its job, when a Player hits a marker. Link to comment
Bonsai Posted September 10, 2010 Author Share Posted September 10, 2010 local level = 0 [b]addEventHandler("onMarkerHit", getRootElement(), setTimer ( drainSomeWater, 100, 15000 ) )[/b] function drainSomeWater() level = level - 0.01 setWaterLevel ( level ) end try this I think you need a function for that. addEventHandler("onMarkerHit", getRootElement(), function(element) [b]if getElementType(element) == "player" then[/b] setTimer ( drainSomeWater, 100, 15000 ) end end ) I want a function to start its job, when a Player hits a marker. Nice one, works perfect. I don't really understand why it has to look like this, but anyway, Works! Thanks Link to comment
dzek (varez) Posted September 10, 2010 Share Posted September 10, 2010 What exactly you don't uderstand? We can try to explain it to you - becouse if you won't understand it now - you will return with same question soon or later Link to comment
Discord Moderators Zango Posted September 10, 2010 Discord Moderators Share Posted September 10, 2010 addEventHandler("onMarkerHit", getRootElement(), function(element) if getElementType(element) == "player" then setTimer ( drainSomeWater, 100, 15000 ) end end ) Is the same as function functionName (element) if getElementType(element) == "player" then setTimer ( drainSomeWater, 100, 15000 ) end end addEventHandler("onMarkerHit", getRootElement(), markerHit) but it's just an alternative way to put it. Link to comment
BinSlayer1 Posted September 10, 2010 Share Posted September 10, 2010 I think you need a function for that. Riiight, I made it too quick to even notice that Link to comment
Bonsai Posted September 11, 2010 Author Share Posted September 11, 2010 addEventHandler("onMarkerHit", getRootElement(), function(element) if getElementType(element) == "player" then setTimer ( drainSomeWater, 100, 15000 ) end end ) Why does this function have to be in there? I tried to do the same by just putting the function's name in there, but it didn't work. So I'm a bit confused here. Link to comment
BinSlayer1 Posted September 11, 2010 Share Posted September 11, 2010 addEventHandler("onMarkerHit", getRootElement(), [color=#BF0000]--This event is triggered when an element enters a marker created using createMarker. [/color] function(element) if getElementType(element) == "player" then [color=#BF0000] --if the element that entered the marker was a PLAYER [/color] setTimer ( drainSomeWater, 100, 15000 ) [color=#BF0000]--then this little timer will activate the function we called "drainSomeWater" like this:[/color] end [color=#BF0000] --Every 100 milliseconds, the function drainSomeWater will be repeated. It will be repeated 15000 times.[/color] end ) level = 0 [color=#BF0000]--as you should know, sea level is 0 (geography)[/color] function drainSomeWater() level = level - 0.01 -[color=#BF0000]-once we've entered the function in which setTimer threw us in, we'll decrease the level of the sea[/color] setWaterLevel ( level ) [color=#BF0000]--the actual size is set now[/color] -[color=#BF0000]-at first it only loses 0.01 of the size, but remember that it gets repeated 15000 times so hence we have total sea drown[/color] end Link to comment
Bonsai Posted September 11, 2010 Author Share Posted September 11, 2010 addEventHandler("onMarkerHit", getRootElement(), [color=#BF0000]--This event is triggered when an element enters a marker created using createMarker. [/color] function(element) if getElementType(element) == "player" then [color=#BF0000] --if the element that entered the marker was a PLAYER [/color] setTimer ( drainSomeWater, 100, 15000 ) [color=#BF0000]--then this little timer will activate the function we called "drainSomeWater" like this:[/color] end [color=#BF0000] --Every 100 milliseconds, the function drainSomeWater will be repeated. It will be repeated 15000 times.[/color] end ) level = 0 [color=#BF0000]--as you should know, sea level is 0 (geography)[/color] function drainSomeWater() level = level - 0.01 -[color=#BF0000]-once we've entered the function in which setTimer threw us in, we'll decrease the level of the sea[/color] setWaterLevel ( level ) [color=#BF0000]--the actual size is set now[/color] -[color=#BF0000]-at first it only loses 0.01 of the size, but remember that it gets repeated 15000 times so hence we have total sea drown[/color] end I know how that function words. Its just, I tried to do the same before I posted here. I created a function with a Timer in it, and set it to trigger on(Client)MarkerHit. addEventHandler("onMarkerHit", getRootElement(), functionWithTimerInIt) functionWithTimerInIt() setTimer ( drainSomeWater, 100, 15000) end And that did not work. But now, everything works fine using the other way Thx again. Link to comment
Discord Moderators Zango Posted September 11, 2010 Discord Moderators Share Posted September 11, 2010 functionWithTimerInIt() Isn't valid, you have to separate function with the name. You declare a function 'WithTimerInIt' but when you do not separate those, how is LUA supposed to know you do so. function WithTimerInIt() Link to comment
Bonsai Posted September 11, 2010 Author Share Posted September 11, 2010 functionWithTimerInIt() Isn't valid, you have to separate function with the name. You declare a function 'WithTimerInIt' but when you do not separate those, how is LUA supposed to know you do so. function WithTimerInIt() Woopsi, just a misspelling Link to comment
norby89 Posted September 12, 2010 Share Posted September 12, 2010 Don't forget you have to declare the function before the handler, else it won't work. Link to comment
50p Posted September 12, 2010 Share Posted September 12, 2010 Like norby said: viewtopic.php?f=91&t=23895&p=289200#p289183 Link to comment
Bonsai Posted September 12, 2010 Author Share Posted September 12, 2010 Okay, Another problem addEventHandler("onClientMarkerHit", grav3, function(element) if getElementType(element) == "player" then setTimer ( drainSomeWater, 100, 15000 ) end end ) function drainSomeWater() level = level - 0.03 setWaterLevel ( level ) destroyElement(grav3) end This works now. But it triggers for every Player. How can I make it just for the Player who hits the marker? Actually I know how to, but here, it doesnt work here. function drainSomeWater(thePlayer,dimension) if (dimension and thePlayer==getLocalPlayer()) level = level - 0.03 setWaterLevel ( level ) destroyElement(grav3) end end Link to comment
BinSlayer1 Posted September 12, 2010 Share Posted September 12, 2010 addEventHandler("onClientMarkerHit", getRootElement(), function(hitPlayer, matchingDimension) setTimer ( drainSomeWater, 100, 15000 ) end ) function drainSomeWater() level = level - 0.03 setWaterLevel ( level ) end dont forget to use type="client" in meta.xml to run it client side Link to comment
Bonsai Posted September 13, 2010 Author Share Posted September 13, 2010 [lua] dont forget to use type="client" in meta.xml to run it client side Of course But still doesn't work. Link to comment
50p Posted September 14, 2010 Share Posted September 14, 2010 Add source to your setTimer call and leave drainSomeWater as function drainSomeWater(thePlayer) ... Read wiki carefully. Check setTimer parameters on wiki. Link to comment
Bonsai Posted September 14, 2010 Author Share Posted September 14, 2010 Add source to your setTimer call and leave drainSomeWater as function drainSomeWater(thePlayer) ... Read wiki carefully. Check setTimer parameters on wiki. I tried that now, I'm not sure if I did it correctly, but it didn't work. The Water was going down for everybody, not just for the MarkerHit Player. addEventHandler("onClientMarkerHit", grav1, function(element) if getElementType(element) == "player" then setTimer ( drainSomeWater, 100, 200, thePlayer==getLocalPlayer() ) end end ) function drainSomeWater(thePlayer) level = level - 0.03 setWaterLevel ( level ) destroyElement(grav1) end I can't believe that its so difficult to get it working Link to comment
50p Posted September 14, 2010 Share Posted September 14, 2010 Oh wait, it's client-side. You don't need to pass source to setTimer but you have to learn Lua a bit more.. Just do this for your onClientMarkerHit event: addEventHandler( "onClientMarkerHit", grav1, function( thePlayer ) if thePlayer == getLocalPlayer( ) then setTimer ( drainSomeWater, 100, 200 ) end end ) In your case you don't need to check if thePlayer is actually player element because it'll be either player or vehicle element that hits the marker. Since they are both elements (userdata type) there will be no warning/error messages due to comparing different data types. Remember, grav1 has to hold a marker before you add the handler function. Link to comment
Bonsai Posted September 15, 2010 Author Share Posted September 15, 2010 Thanks a lot. It works, finally. Now I can finally finish this confusing map Thanks. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now