• table.insert oddity
    2 replies, posted
Hello. I am trying to add superpowers from Heroes for the heck of it. Part of my design is to have a table of all the super powers stored at the server-side. This table is called "supernaturalpowers". The problem now is that I have one lua file per power, and these define the power. After the power object has been created and defined, I want to use table:insert to add the newly created object to the aforementioned list. Except it isn't working, and the errors it's spitting out are quite odd. autorun/server/heroes_powers.lua [code]//base power class function Power:Initialize() self.name = "base"; self.fullname = "Base Power Class"; self.desc = ""; self.suppressed = false; //power is suppressed end function Power:Give() end function Power:Take() end function Power:Think() end function Power:PlayerDeath(victim, weapon, killer) end function Power:Suppress() end //loading powers supernaturalpowers = {};[/code] powers/power_regeneration.lua [code]function Power:Initialize() self.name = "regeneration"; self.fullname = "Rapid cellular regeneration"; self.desc = "Those with rapid cellular regeneration will have buffed health. Health will slowly regenerate over time, and the user cannot be killed ordinarily."; self.suppressed = false; //power is suppressed self.killed = false; //player was killed ( 'revive' soon) end function Power:Think() if(suppressed) then return; end ... self.nextThink = CurTime() + 2; end ... table.insert(supernaturalpowers, Power); //<- oops?[/code] When using the above code, I get an error that the 1st argument of table.insert has to be a number, but I'm specifying a table instead. When I try to use: [code]supernaturalpowers.insert(supernaturalpowers.Count() + 1, Power);[/code] I get an error that the function is not defined. I have done Msg(supernaturalpowers) and confirmed that it is indeed sending me a table. But for some reason, it won't allow me to insert the power to it. On an unrelated note, is my design correct? I wish to make it so powers can be added and removed seamlessly.
uh. table.insert(supernaturalpowers,Power) should work fine, because it does here.
Consider using metatables as an alternative, then creating a new power by using setmetatable({},Power) and editing it there. Then you table.insert it and it should work fine.
Sorry, you need to Log In to post a reply to this thread.