Jump to content

Loops


Deltanic

Recommended Posts

Hi

I've searched this site, the wiki and Google for some explanation, but I didn't find anything. What I want is to create a loop like this:

addEventHandler('onResourceStart', resourceRoot, 
function() 
object = { createObject ( 7191, 265.10437011719, 191.24276733398, 1009.1465454102, 270, 0, 0 ) }
object = { createObject ( 7191, 246.921875, 194.00668334961, 1009.1465454102, 270, 0, 90 ) }
--And more and more objects
for o=0, 117 do setElementInterior( object[ o ], 3 ) --But this loop doesn't work
end
end
)

Link to comment

Yes, I know its better to use a .map file, but it was bugging a LOT. I don't know why, but the first round in stealth you play that map, you see the objects, and after that, you won't see the objects anymore untill I restart the map. Than I tried to create the objects with lua and setElementInterior. Just tested with local object 1 = createObject(...). Result? That works. That's why I'm using this objects in a LUA file.

EDIT: Solution was to use this. (Map works now :D)

addEventHandler('onResourceStart', resourceRoot, 
function() 
object = {}
table.insert( object, createObject ( 7191, 265.10437011719, 191.24276733398, 1009.1465454102, 270, 0, 0 )  )
table.insert( object, createObject ( 7191, 246.921875, 194.00668334961, 1009.1465454102, 270, 0, 90 )  )
--And more and more objects
for o=0, 117 do setElementInterior( object[ o ], 3 )
end
end

@Below me: Thank you :)

Edited by Guest
Link to comment
addEventHandler('onResourceStart', resourceRoot, 
function() 
object = {} -- FYI, this line says "object" is a new empty table; object = {5,222,0.5} would say "object" is a new table with those values (object[1] would represent 5, object[2] would be 222 and so on (unlike in c where the array would start with object[0]))
table.insert( object, createObject ( 7191, 265.10437011719, 191.24276733398, 1009.1465454102, 270, 0, 0 )  )
table.insert( object, createObject ( 7191, 246.921875, 194.00668334961, 1009.1465454102, 270, 0, 90 )  )
--And more and more objects
for index,value in ipairs(object) do -- a loop which runs as many times as elements on the given table "object" (note also that you can rename "index" and "value" to whatever you want
setElementInterior( value, 3 ) -- value will always represent the value (in this case an object element) of the table which matches the current index
--[[ unrelated hint: outputChatBox(index) would spam your chat with the number 1 - 117 (if this is still your amount of objects) like this
1
2
3
4
(...)
]]
end
--unrelated hint2: you can get the amount of table entries with #. just like this: #object oh and this works for strings as well a = "hello" #a --> 5
end

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