Jump to content

Checking if something happened in timer and then disabling


Recommended Posts

Hello dear people of the MTA Forum,

This is my first post, should I have overlooked a sticky or something explaining rules of posting, please be kind.

I've been scripting for 12 days straight since I first started learning lua. Having some experience with other programming languages helped me a great deal and I've been able to figure everything out up until now.

I cannot for the life of me understand one thing:

Let's say I have a function that makes the player sit down and do some other stuff while sitting. I don't have a particular example on hand so I'll just make one up.

condition = false 
function sitAndDo(player) 
setPedAnimation(player,"BEACH","ParkSit_M_loop",0,false,false,true,true) 
    theTimer = setTimer(function() 
            if condition == true then 
                killTimer(theTimer) 
            end 
    end,10000,0) 
end 

This doesn't seem particularly clean, also it doesn't work. I want to continuously check if something happened yet and then of course make the timer stop. How do I accomplish this? I've been searching for days. Should i trigger a custom event as soon as the condition is true that kills the timer? Can someone please give me an example on finding the cleanest way?

Also on an unrelated note: how do I check if the player/an element has moved. I cannot seem to find a suitable Event for that.

Edited by Guest
Link to comment

First, welcome to the mta forums! :)

Btw, you forgot [ lua] [/lua] tags ;p (it's left from the spoiler and right from the "center" button )

Your code checks every 10 seconds if the variable called "condition" is true, but you don't have a variable called "condition" in your script, so you can't check it if it's true or not.

  
condition = false 
function sitAndDo(player) 
setPedAnimation(player,"BEACH","ParkSit_M_loop",0,false,false,true,true) 
    theTimer = setTimer(function() 
            if condition == true then 
                killTimer(theTimer) 
            end 
    end,10000,0) 
end 
  
function toggleVariabale() 
condition = not condition -- this is the same as: 
--[[ 
if condition == true then 
condition = false 
elseif condition == false then 
condition = true 
end 
]] 
end 
  

Link to comment

Thank you for the quick reply! I'll make sure to add lua tags in the future.

About the variable condition: i was aware of that, basically the "....blablabla...." part was where the variable would be initialized, however I cannot seem to address the timer from within itself.

[EDIT] I edited my original code to yours just to make clear what I'm trying to say, I hope that's ok.

Link to comment

Actually you should be able to kill a timer in the function he was calling, but I've never tested that.

What about a timer that is only executed once starting a function that checks your stuff, if its false it starts another timer doing the same, until its finally true, or killed if it takes too long.

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