Getting a keyvalue set in Hammer for use in a lua script?
4 replies, posted
I'm working on a DM gamemode, and looking for a way to assign spawnpoints to teams in lua. So far i've added a keyvalue in the info_player_start ents called "team" (in hammer) with a value of either red or blue. I've been trying to retreive the values by doing:
[code]
for k,v in pairs(ents.FindByClass("info_player_start")) do
print(v:GetKeyValues().team)
if v:GetKeyValues().team == "red" then
table.insert(RedSpawn,v)
print("Red spawn added")
end
if v:GetKeyValues().team == "blue" then
table.insert(BlueSpawn,v)
print("Blue spawn added")
end
end
[/code]
I know I'm getting the correct ents, but i cant get the keyvalues, so i was wondering how i'd get that to work.
Thanks in advance!
That looks right to me, what do you get with PrintTable(v:GetKeyValues())?
Suprisingly enough, i get nothing at all when i do that. Is this function broken or something?
[editline]06:40PM[/editline]
I did a little test, and found out that ents.FindByClass("info_player_start") does not return anything, although thats the classname of the spawn ent. Should i be using a different ent?
[QUOTE=matte3560;17915243]Suprisingly enough, i get nothing at all when i do that. Is this function broken or something?
[/QUOTE]
Yes, it's broken. Well, at least partly. It doesn't return all of the keyvalues of an entity properly, and most of the returned values are usually wrong.
You can use the "EntityKeyValue" hook instead to get the correct keyvalues: [url]http://wiki.garrysmod.com/?title=Gamemode.EntityKeyValue[/url].
You could also just create a new SENT for the spawnpoints, and then get the keyvalues using [URL=http://wiki.garrysmod.com/?title=ENT.KeyValue]ENT.KeyValue[/URL]. That's what I'd do.
I managed to solve it by using two different spawn ents, but thanks anyway!
Sorry, you need to Log In to post a reply to this thread.