Is it possible to make it so each entry in a table has it's own table?
For instance, If I were to want a table of all the players, and each player would have their own table.....Like
Each entry in the table is a player.....
but within each player entry there's a spot for say HP, Money,DistanceFrmPly...
How would I do this exactly...
PlayerTable[Player3].HP, or PlayerTable[Player3].Money
Basically storing variables within each table entry.
I know it can be done cause I have seen it before but I can't exactly recall how to replicate it.
Yeah. Something like:
[lua]
PlayerTable = {}
for k,v in pairs(player.getall()) do
PlayerTable[v:UniqueID()] = {}
PlayerTable[v:UniqueID()).Health = 100
end
[/lua]
Would work.
It's very possible.
[lua]local tbl = { -- This is our main table
["juices"] = { -- This is the 'juices' table inside our main table
"orange juice", "lemon juice", "apple juice"
},
["foods"] = { -- This is the 'foods' table inside our main table
["junk food"] = { -- This is the 'junk food' table inside our 'foods' table
"pizza", "chips", "crisps"
},
["healthy stuff"] = { -- This is the 'healthy stuff' table inside our 'foods' table
"carrots", "apples", "grapes"
}
}
};
PrintTable(tbl); -- Print the table to our console[/lua]
It could be easily adapted to do what you need.
[lua]PlayerTable = {};
hook.Add("PlayerInitialSpawn", "Create an entry in the PlayerTable", function(ply)
if(!PlayerTable[ply:SteamID()]) then
PlayerTable[ply:SteamID()] = {}; -- If he doesn't have an entry in the table, let's create one
PlayerTable[ply:SteamID()]["Money"] = 200; -- Give him 200 dollars
PlayerTable[ply:SteamID()]["Props broken"] = 0; -- etc
PlayerTable[ply:SteamID()]["Favourite food"] = "pizza";
end
end);
hook.Add("PropBreak", "Modify an entry in the PlayerTable", function(ply, prop)
PlayerTable[ply:SteamID()]["Props broken"] = PlayerTable[ply:SteamID()]["Props broken"] + 1; -- Increase his 'props broken' count when he breaks a prop
end);[/lua]
Or you could just create a table for each player instead of having a global table with players in.
like this
[lua]
for k, v in pairs( player.GetAll() ) do
v.InfoTable = {};
v.InfoTable.Health = 100;
end;
[/lua]
Do that ^
[QUOTE=Chewgum.;18402536]Or you could just create a table for each player instead of having a global table with players in.
like this
[lua]
for k, v in pairs( player.GetAll() ) do
v.InfoTable = {};
v.InfoTable.Health = 100;
end;
[/lua][/QUOTE]
Or indeed,
[lua]for _,ply in ipairs( player.GetAll() ) do
v.Health = 100;
end[/lua]
Incidentally, when you are giving out examples to newbies, please use ipairs when you are addressing sequential integer index'd tables.
It doesn't really matter if you want to be inefficient in your own code, but please don't promote terrible habits in others.
If I did this for my aimbot
[quote]
BoneLocation = {}
function AddEntB(EntMdl,Head,Neck,Chest)
BoneLocation[EntMdl] = {
["Head"] = Head,
["Neck"] = Neck,
["Chest"] = Chest
}
end
AddEntB("zombie.mdl","ValveBiped.Bip01_Head1","ValveBiped.Bip01_Neck1","ValveBiped.Bip01_Chest1")
[/quote]
if I wanted to do something like this...then
if the model of the entity the player is looking at is zombie.mdl, and aimatneck would be set to neck
then it would get the model of the neck from BoneLocation[EntMdl].("Neck")
Something along the lines of
[quote]
local Ent = (LocalPlayer():GetEyeTrace().Entity)
local EntModel = Ent:GetModel()
if AimAtHead then
BoneName = BoneLocation[EntModel].("Head")
return end
else
if AimAtNeck then
BoneName = BoneLocation[EntModel].("Neck")
return end
else
if AimAtChest then
BoneName = BoneLocation[EntModel].("Chest")
return end
end
[/quote]
I figure this would work, then the rest of my aiming function for my aimbot would take the bone from the function and aim with it... Do you see anything wrong with my code there?
Didn't know about ipairs :P I'm a newbie too :P
Could you explain the differences between 'pairs' and 'ipairs' cuz i never really looked into that ;/
[QUOTE=Chewgum.;18402769]Could you explain the differences between 'pairs' and 'ipairs' cuz i never really looked into that ;/[/QUOTE]
ipairs only works on sequential integer indexed tables, pairs works on anything else. So ipairs should be used until you know some of the indexes are not integers.
ipairs iterates all integer indexes from 1..#tbl (i.e. it stops at _most_ gaps but not all of them)
pairs iterates everything (including 1.5, "hello", Entity(5), Vector(1,2,3) and so on)
referring to keys here
all entity table returning functions such as player.GetAll, ents.FindInSphere etc return sequential integer indexed tables, as do tables created with {a,b,c}, such as myTable = {"a", "b", "apple"}.
table.insert also creates such tables, so
[lua]myTable = {}
table.insert(myTable,"a")
table.insert(myTable,"b")
table.insert(myTable,"apple")[/lua]
would do the same thing.
If you know you are going to deal with a table like this, you should always use ipairs, as ipairs is much faster than pairs, since it knows what all the keys are, while pairs has to look through the table to find every possible key.
Or you could use metafunctions and metatables, because that's what they're made for.
Ah very cool, and what are metatables flapjack? :P Always up for learning new stuff.
_G , _R , _E
And so on
[code]PrintTable(_G)[/code]
Expect some lag.
You can use this for pretty cool stuff.
[code]_G[string.char(math.random(65 , 117)).."func"] = function(...) print(unpack(arg)) end[/code]
Random function names.
To be honest though, those are not metatables. Metatables are tables about data - for example players. Pretty similar to metafunctions.
Concretely in garrysmod every entity object shares the same MetaTable. The Entity metatable contains every method you can use on entities. Same goes for players and NPCs. What they allow you to do is to create methods shared by all of those objects (metamethods) or override the present ones. The advantage of methods is that they have an extra explicit argument "self".
As an example these do exactly the same thing :
[lua]function SetMoney(ply,money)
ply.Money = money
end
SetMoney(Entity(1), 100)[/lua]
[lua]local meta = getmetatable("Player")
function meta:SetMoney(money)
self.Money = money
end
Entity(1):SetMoney(100)[/lua]
[lua]function _R["Player"]:SetMoney(money) --Note that _R["Player"] is the name of the Player metatable. <---Apparently not
self.Money = money
end
Entity(1):SetMoney(100)[/lua]
[url=http://wiki.garrysmod.com/?search=G.getmetatable][I]G.getmetatable[/I] [img]http://wiki.garrysmod.com/favicon.ico[/img][/url]
[QUOTE=| FlapJack |;18407903]_R["Player"]
There is also _G["Player"] though.[/QUOTE]
Oh right.. since it is a table inside the entity metatable. :smile:
_G["Player"] is most probably the player library?
Infact, yeah. It is.
[code]PrintTable(_G["player"])[/code]
[code]21:48:29 > PrintTable(_G["player"])...
GetBots<TAB>=<TAB>function: 023266D8
GetByID<TAB>=<TAB>function: 0239FC08
GetByUniqueID<TAB>=<TAB>function: 02341E88
GetAll<TAB>=<TAB>function: 02383A50
GetHumans<TAB>=<TAB>function: 02349BC8[/code]
[QUOTE=| FlapJack |;18407992]I think so. For some reason, it returned a function when I tried to print it.[/QUOTE]
Because it is a function.
Player(userid)?
And quoted from the FindMetaTable page on the wiki:
[quote]If you make your own metatable and want it accessible by this function, put _R.NameHere = TableHere when you've finished defining it.[/quote]
Yo dawg, so I herd you like tables...
[QUOTE=Chrisaster;18408010]Because it is a function.
Player(userid)?[/QUOTE]
Was the difference between upper and lowercase. I knew it existed :P
[code]21:52:02 > print(_G["Player"])...
function: 02374B40
[/code]
[code]21:52:23 > print(_G["player"])...
table: 027A3CE0
[/code]
Sorry, you need to Log In to post a reply to this thread.