Crosshair Posted December 27, 2012 Share Posted December 27, 2012 Hi. I`m trying to make 2 commands that uses one command and i have some problems. plants = { } function plants(thePlayer, cmd, id) if (cmd == "plant") then local x,y,z = getElementPosition(thePlayer) plants[id] = createObject(plantID,x,y,z) end if (cmd == "harvest") then --TODO end end addCommandHandler("plant", plants) addCommandHandler("harvest", plants) When i enter the command "plant" gives me an error at plants[id] = createObject(plantID,x,y,z): table index is nill. If i remove the argument "cmd" from the function all works well. Link to comment
csiguusz Posted December 27, 2012 Share Posted December 27, 2012 This is maybe because you just entered the command "plant" and you did not wrote an id (like /plant 29 ). function plants(thePlayer, cmd, id) if (cmd == "plant") then if tonumber(id) then local x,y,z = getElementPosition(thePlayer) plants[id] = createObject(plantID,x,y,z) end end if (cmd == "harvest") then --TODO end end Link to comment
Anderl Posted December 27, 2012 Share Posted December 27, 2012 You can't declare different things with the same name. Link to comment
csiguusz Posted December 27, 2012 Share Posted December 27, 2012 You can't declare different things with the same name. Yes, this is also true. Link to comment
Crosshair Posted December 27, 2012 Author Share Posted December 27, 2012 Your right, but i want the player to not enter a number. Just to type plant and the plant will later rise from the ground, So i need the script to add the plant1 in the table with the first position and if he decides to add another plant(plant2) to be added also in the table with the second position. If i don`t use the cmd argument this works and if i use outputchatbox(plants[1]) works. Link to comment
Anderl Posted December 27, 2012 Share Posted December 27, 2012 You can't declare different things with the same name. There is also no "plantID" variable declared. Link to comment
csiguusz Posted December 27, 2012 Share Posted December 27, 2012 Your right, but i want the player to not enter a number. Just to type plant and the plant will later rise from the ground, So i need the script to add the plant1 in the table with the first position and if he decides to add another plant(plant2) to be added also in the table with the second position.If i don`t use the cmd argument this works and if i use outputchatbox(plants[1]) works. Then dont use id as table index, use this: table.insert(plants,createObject(plantID,x,y,z)) Link to comment
Crosshair Posted December 27, 2012 Author Share Posted December 27, 2012 You can't declare different things with the same name. There is also no "plantID" variable declared. I just didn`t included the variable. Then dont use id as table index, use this: table.insert(plants,createObject(plantID,x,y,z)) I make another thing right now. I added on the top id = 0, removed the id argument and add id=id+1 before it creates the object. Now if i use table.insert how do i retrieve the emelents ? with unpack ? What is the best option to do this ? Mine or yours ? Link to comment
csiguusz Posted December 27, 2012 Share Posted December 27, 2012 Mine is easier, table.insert, will do what you want to do with "id = 0 and id = id+1". You can retrieve the elements so: plants[1] is the first added element plants[2] is the second and so on, or do a loop: --this will destroy every elements in the table if it exists, and remove it from the table for i, v in ipairs(plants) do if isElement(v) then destroyElement(v) end table.remove(plants,i) end Link to comment
Crosshair Posted December 27, 2012 Author Share Posted December 27, 2012 Well thank you for the support and i`l see tomorrow what i will do. 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