Jump to content

setting variables as table key


kuwalda

Recommended Posts

Posted

Hi.

I made topic before about my problem about speaker objects, but it helped me only partly.

Main problems that accoured me:

  
--Creating array 
speakeruArray { } 
  
--creating entry in array with key foo and value.. 
speakeruArray["foo"] = createObject(2229, x, y, z-1) 
  
--trying to delete this object using does not work 
destroyElement(speakeruArray["foo"]) 
table.remove ( speakeruArray["foo"]) 
  

And my second question is about right syntax for using variable as key for table.

For example speakeruArray[myDefinedNumber] = createObject(2229, x, y, z-1)

Sorry for being such pain in ass for someone, but I have tried to figure this all out for myself for hours and nothing have worked.

Posted (edited)

Your syntax right

yes like this :

local table = { } 
  
table[1] = createObject( .... ) 
table[2] = createObject( .... ) 
table[3] = createObject( .... ) 
Edited by Guest
Posted

Why you use the table like that?

You can use the method the guy ontop of me said, but this will keep your way (named variable foo)

  
--Creating array 
speakeruArray { } 
  
--creating entry in array with key foo and value.. 
speakeruArray.foo = createObject(2229, x, y, z-1) 
  
--trying to delete this object using does not work 
destroyElement(speakeruArray.foo) 
  

Posted

Thanks for replys, they really helped alot, but still there is one more small problem with deleting those objects:

  
--Creation 
speakeruArray[number] = createObject(2229, x, y, z-1) 
  
--Removing 
destroyElement(speakeruArray.number) 
table.remove (speakeruArray, number) 
-- wihch is giving out 
-- Bad argument @ 'destroyElement' [Expected element at argument 1, got nil] 
  

So when I made small command

  
for i, v in pairs( speakeruArray ) do 
    outputChatBox("DEBUG:"..i..".", root, 255, 255, 0) 
end 
  

I realised all "i" values were my defined numbers, so is that fine or that is my problem?

Posted

Have you globally defined number?

  
-- Global defining 
number = 1 
  
--Creation 
speakeruArray[number] = createObject(2229, x, y, z-1) 
  
--Removing 
destroyElement(speakeruArray[number]) 
table.remove (speakeruArray, [number]) 
  

Posted
  
speakeruArray = { } 
  
function speakerCP(thePlayer, command, whatIwant, setting, number, vehicle) 
        if tostring(whatIwant) == "place" then 
            local x, y, z = getElementPosition(thePlayer) 
            outputChatBox(number, thePlayer, 0, 250, 0) 
            speakeruArray[number] = createObject(2229, x, y, z-1) 
            outputChatBox("created a speaker!", thePlayer, 0, 250, 0) 
        elseif tostring(whatIwant) == "remove" then 
            destroyElement(speakeruArray.cipars) 
            table.remove (speakeruArray, cipars) 
            outputChatBox("destroyed the Speaker!", thePlayer, 0, 0, 255) 
        elseif tostring(lemums) == "debug" then 
            for i, v in pairs( speakeruArray ) do 
                outputChatBox("DEBUG:"..i..".", root, 255, 255, 0) 
            end 
        end 
  

Debug is working as it should - displaying 1,2,3,4...

But removeing is not, as it only displays "You have Succesfully destroyed the Speaker!" ,but rest is giving error

Bad argument @ 'destroyElement' [Expected element at argument 1, got nil]

EDIT: HUGE THANKS TO EVERYONE who helped me, omg ... finally it is working as it should. Thank you all. :)

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