• Getting all table values, and printing them?
    4 replies, posted
I'm making a plant mod, but I'm having a problem printing all rows from each line out of the 'Plants' table. [lua] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( 'shared.lua' ) local MaxAllowableEntities = 4; local CurrentEntities = 0; local PlantTable = {} PlantTable[0] = "models/weapons/w_bugbait.mdl"; PlantTable[1] = "models/props_outland/pumpkin01.mdl"; local Plants = {} function MaxEntities(ply, cmd, args) if(ply:IsAdmin()) then MaxAllowableEntities = tonumber(args[1]); for k,v in pairs(player.GetHumans()) do v:ChatPrint("Admin "..ply:Nick().. " set the PlantMod Max Plants to "..MaxAllowableEntities.."! \n"); v:EmitSound("rapid/levelup.wav", 500, 200); end else ply:EmitSound("rapid/error.wav", 500, 200) ply:ChatPrint("Error:Only admins can set the limits! \n"); end end concommand.Add("PM_MaxPlantEntities", MaxEntities) function lants(ply, cmd, args) Msg("Plant IDs Planted so far out of "..MaxAllowableEntities.."\n"); print("Plant IDs Planted so far out of "..MaxAllowableEntities.."\n"); if(Plants[0] == 0) then Msg("None Planted at the moment. \n"); print("None Planted at the moment. \n"); else for k, plants in pairs(Plants) do Msg("Number "..k.. " -- ID "..plants.ID.."Model "..tostring(plants.MType).."\n"); print("Number "..k.. " -- ID "..plants.ID.."Model "..tostring(plants.MType).."\n"); end end end concommand.Add("PM_ListPlants", lants) function ENT:SpawnFunction( ply, tr ) self.MType = "Pumpkin" self.ID = math.Round(math.Rand(15, 500)) table.insert(Plants, self.ID, self.MType); self.GTime = math.Round(math.Rand(CurTime()+10, CurTime()+80)); if(self.Time == nil) then self.Time = 0; end if(self.Lvl == nil) then self.Lvl = 1; end if(self.Eatable == nil) then self.Eatable = false; end selfGrow(self.Entity) if(CurrentEntities < MaxAllowableEntities) then selfGrow(self.Entity); if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 2 local ent = ents.Create( "gp_tree" ) ent:SetPos( SpawnPos ) ent:Spawn() ent:Activate() ent:EmitSound("items/itempickup.wav", 500, 200) CurrentEntities = CurrentEntities + 1; for k, ply in pairs(player.GetHumans()) do ply:ChatPrint("[PM]Plant ID "..ent.ID.." has been planted. It will grow in "..self.GTime.." seconds."); end return ent else ply:ChatPrint("Max plant limit reached!"); ply:EmitSound("rapid/error.wav",500,200) end end /*--------------------------------------------------------- Name: Initialize ---------------------------------------------------------*/ function ENT:Initialize() self:SetUseType(SIMPLE_USE) self.Entity:SetModel( tostring(PlantTable[0]) ) end function selfGrow(eent) if(eent != nil) then if(eent.Time >= eent.GTime) then self.Lvl = self.Lvl + 1; self.GTime = math.Round(math.Rand(CurTime()+20, CurTime()+120)); end end timer.Simple(1, selfGrow, eent) end function CheckLevel(ent) if(ent.Lvl == 1) then ent:SetModel(tostring(PlantTable[0])) end if(ent.Lvl == 2) then ent:SetModel(tostring(PlantTable[1])) ent.Eatable = true; for k, ply in pairs(player.GetHumans()) do ply:ChatPrint("[PM]Plant ID "..ent.ID.." has grown. \n"); end end end /*--------------------------------------------------------- Name: Use ---------------------------------------------------------*/ function ENT:Use( activator, caller ) if(self.Eatable == true) then activator:SetHealth(activator:Health() + 50); CurrentEntities = CurrentEntities - 1; self:Remove(); else activator:ChatPrint("[PM] You must wait for it to grow before you can eat it! \n"); activator:EmitSound("rapid/error.wav", 500, 200); end end function ENT:OnRemove() CurrentEntities = CurrentEntities - 1; table.remove(Plants, self.ID); end [/lua] How could I fix this?
PrintTable [editline]12th May 2011[/editline] [code]] lua_run PrintTable( player.GetAll() ) > PrintTable( player.GetAll() )... 1 = Player [1][c-unit] [/code]
[QUOTE=c-unitV2;29767749]PrintTable[/QUOTE] That works, but I need it to print "ID", "Model". Not the entire table with the = signs on it. Such as ID = First value, Model = Second Value
[lua] for k, v in pairs( Plants ) do print("ID: "..k.." Model: "..v) end [/lua] [editline]12th May 2011[/editline] or just print(k, v) [editline]12th May 2011[/editline] Oh, I misread your code I believe you would do print(v.Mtype, v.ID)
[QUOTE=c-unitV2;29767907][lua] for k, v in pairs( Plants ) do print("ID: "..k.." Model: "..v) end [/lua] [editline]12th May 2011[/editline] or just print(k, v) [editline]12th May 2011[/editline] Oh, I misread your code I believe you would do print(v.Mtype, v.ID)[/QUOTE] [lua] for k, v in pairs( Plants ) do print("ID: "..v.ID.." Model: "..v.MType) end [/lua] Gives me the error: [@addons\rapid's growable plants\lua\entities\gp_tree\init.lua:36] bad key to string index (number expected, got string) EDIT: I got it working using the print(k,v); but in the near future I might have problems with a table with more then 2 values.
Sorry, you need to Log In to post a reply to this thread.