Jump to content

[HELP] A timer for every vehicle


adithegman

Recommended Posts

Hey! I just started scripting in LUA and I need some help... I have a very good knowledge of the C languages (mainly C++) but I'm having trouble understanding a few things. 

What I want to do is create a timer (x = setTimer(...) ) for every vehicle on the server... and I have no idea how to do it. I know it must be tied to tables, I tried using tables but I failed...

I only just started on LUA today so I'm a pretty big noob ;) 

NOTE: This account is not MINE so disregard my other posts

Edited by adithegman
Link to comment

Is really needed binding every vehicle to a timer? Did you check the events? Most things can be achieved with events, and most people (me at least) don't like timers xD

 

A note on what @iPrestege said: also store the created timers on a table so you can manage them. Don't store only vehicles. The best way would be creating a table for every pair of vehicle-timer, so for every vehicle you can easily acces its timer. Or use the vehicle as key/index

Spoiler

example of that table:


local thingsTable1 = {}
local thingsTable2 = {}

function callMeOnStart()
	for _,theVehicle in pairs(getElementsByType( "vehicle" )) do
		--pair of vehicle+timer
		thingsTable1[#thingsTable1+1] = {theVehicle, setTimer(function(theVeh) end, 60000, 0, theVehicle)}
		--or even better, use the veh as index
		thingsTable2.theVehicle = setTimer(function(theVeh) end, 60000, 0, theVehicle) -- equivalent: thingsTable2[theVehicle] = setTimer(function(theVeh) end, 60000, 0, theVehicle)
	end
end

 

 

Link to comment
9 minutes ago, LoPollo said:

Is really needed binding every vehicle to a timer? Did you check the events? Most things can be achieved with events, and most people (me at least) don't like timers xD

 

A note on what @iPrestege said: also store the created timers on a table so you can manage them. Don't store only vehicles. The best way would be creating a table for every pair of vehicle-timer, so for every vehicle you can easily acces its timer. Or use the vehicle as key/index

  Reveal hidden contents

example of that table:



 
local thingsTable1 = {}
local thingsTable2 = {}

function callMeOnStart()
	for _,theVehicle in pairs(getElementsByType( "vehicle" )) do
		--pair of vehicle+timer
		thingsTable1[#thingsTable1+1] = {theVehicle, setTimer(function(theVeh) end, 60000, 0, theVehicle)}
		--or even better, use the veh as index
		thingsTable2.theVehicle = setTimer(function(theVeh) end, 60000, 0, theVehicle) -- equivalent: thingsTable2[theVehicle] = setTimer(function(theVeh) end, 60000, 0, theVehicle)
	end
end

 

 

Thanks for the example man! I think I got the idea... And BTW, I don't really need a timer for every vehicle, It is something close to that but a bit more complicated to explain so I just asked that question hoping that it will implicitly answer my ohter unknowings and fortunately it did. Thanks for the help!

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