Jump to content

Function on MarkerHit


Bonsai

Recommended Posts

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. :cry:

I hope someone understands the Problem, and knows how to fix it.

Greetings

Link to comment
  • Discord Moderators
  
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
  
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! :P

Thanks

Link to comment
  • Discord Moderators
  
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
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
  
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
  
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. :P

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 :D

Thx again.

Link to comment
  • Discord Moderators
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

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
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

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

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...