Jump to content

Table with Objects


Axel

Recommended Posts

I'm not so good at tables so i thought you guys might help me.

I want to make 1 table that stores all the objects attached to a player, but my problem is destroying a specific object and not the others, and and easy way to export it..

Something like this

 objects = { } 
function attachObj(modelid) 
if objects[source] then 
destroyElement(objects[source] 
objects[source] = nil 
else 
if modelid == 1210 then 
object = createObject ( modelid, 0, 0, 0 ) 
objects[source] = object 
exports.bone_attach:attachElementToBone ( object, source, 11, -0.2, 0, 0.1, 0, 90, 10 ) 
elseif modelid == 2647 then 
.. 
end 
end 
  

So idk how to store both of the elements on the player then destroy each of them whenever i want to..

Link to comment

Regarding deleting an object from a table:

I think the most obstacle you're facing is that you can't tell which object is which, because there aren't any IDs attached to them. Judging by your code a player can have one object of one type attached the them.

In that case you'll probably want to store it like this:

Every player has an objects table with an ID and the Object element or there's one table with all players in it, which has the objectdata in it.

Then to remove the object, use a for loop

  
-- example new table structure 
Objectdata = { 
    ['player1'] = { ["model"] = 1500, ["object"] = 'RANDOM5' }, 
    ['player2'] = { ["model"] = 1800, ["object"] = 'RANDOM8' },  
} 
  
function remObj(modelid) 
    for playerob_k,playerob_v in pairs(Objectdata) do  
     
        if playerob_v['model'] == modelid then 
            -- remove object from world 
            destroyElement(playerob_v['object']) 
             
            -- remove element from table 
            Objectdata[playerob_k] = nil 
        end 
    end 
  
end 
  
remObj(1500); 
  

tested on http://www.lua.org/cgi-bin/demo

Recap:

Basically just use a multi dimensional array

http://stackoverflow.com/questions/5691 ... ble-create

http://stackoverflow.com/questions/1758 ... by-its-key

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