Controlled Posted July 29, 2014 Share Posted July 29, 2014 I have a bunch of create objects in a .lua file. And I want to trigger them via a command, and destroy them seperately. but I'm wondering if I can make the code a lot smaller with a table? Example objects = { createObject (1237, 2838.873, 1712.0601, 9.844) createObject (1237, 2855.843, 1712.0601, 9.844) createObject (1237, 2823.625, 1712.0601, 9.844) } made = false function spawnObjects() if made then -- Destroy them else -- Create them end end I'm just wondering how to make and destroy them with a minimal amount of code.. Link to comment
Et-win Posted July 29, 2014 Share Posted July 29, 2014 for placeNumber, elementData in ipairs(objects) do --code end Loop through them PS: If you destroy them from the table, I think they will not be able to be created anymore. You will have to do that with a table for positions, size, etc. only. Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 for placeNumber, elementData in ipairs(objects) do --code end Loop through them PS: If you destroy them from the table, I think they will not be able to be created anymore. You will have to do that with a table for positions, size, etc. only. Ok so your saying do something like objects = { {1237, 2838.873, 1712.0601, 9.844}, {1237, 2855.843, 1712.0601, 9.844}, {1237, 2823.625, 1712.0601, 9.844}, } for placeNumber, elementData in ipairs(objects) do createObject(elementData[1], elementData[2], elementData[3], elementData[4]) end How would I destroy them? And how would I recreate the table after then? Link to comment
Et-win Posted July 29, 2014 Share Posted July 29, 2014 I guess (Not sure) with getElementsByType("object") and putting it on the resourceRoot. Otherwise, put the variable into another table, so you can delete everything using that table. Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 I guess (Not sure) with getElementsByType("object") and putting it on the resourceRoot. Otherwise, put the variable into another table, so you can delete everything using that table. I dont think I understand. If I do getElementsByType, I could start destroying objects that aren't in my table... How can I make the script check? Link to comment
Et-win Posted July 29, 2014 Share Posted July 29, 2014 As soon as you create the object, put it into a table. Then if you destroy all objects, then destroy everything you have put into that table. Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 As soon as you create the object, put it into a table. Then if you destroy all objects, then destroy everything you have put into that table. Can I use destroyElement(objects) On the table? Probably not, thats what I don't understand about doing this. You say put it into another table but that doesn't answer what I asked about to actually destroy everything in the table... Link to comment
Et-win Posted July 29, 2014 Share Posted July 29, 2014 objects = { {1237, 2838.873, 1712.0601, 9.844}, {1237, 2855.843, 1712.0601, 9.844}, {1237, 2823.625, 1712.0601, 9.844}, } objects2 = { } for placeNumber, elementData in ipairs(objects) do local gObject = createObject(elementData[1], elementData[2], elementData[3], elementData[4]) table.insert(createdObjects, gObject) end for placeNumber, elementData in ipairs(objects2) do destroyElement(elementData) end objects2 = { } I think this would work. Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 Loads in fine, doesn't delete though. Error: Expected Element got Table Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 Ok fixed it, only issue now is its not removing it from the table after... Causes the table to fill up and make errors. Link to comment
Controlled Posted July 29, 2014 Author Share Posted July 29, 2014 All fixed. Thanks for the help Et! Link to comment
Saml1er Posted July 29, 2014 Share Posted July 29, 2014 I'll show you another way. objects = { {1237, 2838.873, 1712.0601, 9.844}, {1237, 2855.843, 1712.0601, 9.844}, {1237, 2823.625, 1712.0601, 9.844} } for placeNumber, elementData in ipairs(objects) do local obj = createObject(elementData[1], elementData[2], elementData[3], elementData[4]) if obj then objects[placeNumber] = obj end end Link to comment
Controlled Posted July 30, 2014 Author Share Posted July 30, 2014 I'll show you another way. objects = { {1237, 2838.873, 1712.0601, 9.844}, {1237, 2855.843, 1712.0601, 9.844}, {1237, 2823.625, 1712.0601, 9.844} } for placeNumber, elementData in ipairs(objects) do local obj = createObject(elementData[1], elementData[2], elementData[3], elementData[4]) if obj then objects[placeNumber] = obj end end Thank you. Link to comment
Saml1er Posted July 30, 2014 Share Posted July 30, 2014 I'll show you another way. objects = { {1237, 2838.873, 1712.0601, 9.844}, {1237, 2855.843, 1712.0601, 9.844}, {1237, 2823.625, 1712.0601, 9.844} } for placeNumber, elementData in ipairs(objects) do local obj = createObject(elementData[1], elementData[2], elementData[3], elementData[4]) if obj then objects[placeNumber] = obj end end Thank you. You're welcome. 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