Get data from map entity

Hi there

I am trying to get some data from the map that I am using. The map creates a bunch of entities, some of which are of entity type “power_socket”. In EntSpy I can see that they have a “buildingid” assigned to them, but how do I get this from the code?

I have made a function that uses the OnEntityCreated hook to get the entity. After checking if the class is actually power_socket, I try to do ent.buildingid but without luck. Any chance you guys can help me out?

Kind regards
Maldark

Entity:GetKeyValues

[editline]8th April 2016[/editline]

aka



yourentity:GetKeyValues()["buildingid"]


would return 2.

Hi there, appreciate the help. I’ve tried doing the GetKeyValues and looping over all of them before asking for help here, sadly none of the keys were buildingid.

Using the following code:

I am getting this lua error:
sv_buildings.lua:45: attempt to concatenate field ‘buildingid’ (a nil value)

what happens if you print the keyvalues table?

Try adding this to your init.lua



function ENT:KeyValue( key, value )
	if key == "buildingid" then
		self.bid = value
	end
end


Then you can get the building id of the entity by doing ent.bid

Here you go:

Just tried, gives me the same error as before, “attempt to concatenate field ‘buildingid’ (a nil value)”

Are you sure you are targeting the correct entity there? It seems that buildingid is not set from looking at that

I am not sure what to tell you. I did not make the map, but I am pretty sure that I am targetting the correct entity. It shows up with “power_socket” as classname in Entspy and I’ve counted, the amount of power_sockets in Entspy correlate with the amount of times the “(ent:GetClass() == “building_cap”)” is true. If there is anything you can think of, let me know and I’ll gladly test it.

Thank you

EDIT: I also just noticed, the hammerid is set to 0, although in the Entspy image I linked you can clearly see that it is not 0. I don’t know if this gives you any hints to what could be wrong?

what about making a server concommand, which loops through ALL entitities with the given class and lists ALL keys of that entity?

This gives the same as shown in #6:

which is produced by the following code:

Got it working last night, had to use the EntityKeyValue hook and assign the building id to the entity there.

Appreciate your help.