Can someone provide an example of how to use KeyValue for entities to be used in hammer? I have a simple currency system for my gamemode and I made an ent that is supposed to cause a prop in the map to move when the player uses the ent, if he has enough money, but when I use it, it returns an error like "attempt to compare number with nil." I'm not posting from my computer, so this isn't the actual code, but it's similar to what I wrote earlier.
in my map, I have the entity with the key "cost," the value of which is 500.
[lua]
function ENT:KeyValue(key, value)
if (key == "cost") then
end
end
ENT.entvalue = value
function ENT:Use(activator,caller)
if activator.money >= self.entvalue then
--hammer things--
end
end
[/lua]
[code]function ENT:KeyValue(key, value)
if (key == "cost") then
self.entvalue = value
end
end
[/code]
You gotta set the variable inside the function. I thought that was obvious. Turns out not.
Changed what I had to this
[lua]
function ENT:KeyValue( key, value )
if ( key == "cost" ) then
self.cost = value
end
end
function ENT:Use( ply, caller )
ply:SetNWInt( "kpoints" , ply.points )
if self.cost <= ply.points then
ply.points = (ply.points - self.cost)
ply:SetNWInt( "kpoints" , ply.points )
end
end
[/lua]
and got this
[ERROR] gamemodes/zombies/entities/entities/nz_door_act/init.lua:26: attempt to compare string with number
1. unknown - gamemodes/zombies/entities/entities/nz_door_act/init.lua:26
how do I get the number from the string it returns?
tonumber(str) ?
wiki.garrysmod.com/page/Global/tonumber
[editline]20th November 2013[/editline]
Also entity:KeyValue actually returns 2 strings like Ent:Keyvalue(string1, string2)
so you might wanna handle that in that function so you dont have tonumber everywhere.
[lua]
function ENT:KeyValue( key, value )
if ( key == "cost" ) then
self.cost = ""..tonumber(value)
end
end
[/lua]
It's still reading it as a string and I am very confused.
[QUOTE=Nornan12;42919344][lua]
function ENT:KeyValue( key, value )
if ( key == "cost" ) then
self.cost = ""..tonumber(value)
end
end
[/lua]
It's still reading it as a string and I am very confused.[/QUOTE]
Because you're concentrating the number with a string, converting it back to a string...
Shit, I'm high on something, I can't even begin to think of why I was concatenating it. Thanks, everyone.
Sorry, you need to Log In to post a reply to this thread.