Jump to content

Question about tables/Elements


Recommended Posts

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

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

Link to comment
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. :D

You're welcome.

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