Hey there,
I try with this clientside code to display a specific entity with a text on the HUD.
hook.Add( "HUDPaint", "LookForEnt", function()
local pos
for k,v in pairs(ents.GetAll()) do
if v:GetClass() != "my_ent" then continue end
pos = v:GetPos()
draw.SimpleTextOutlined("I'm Here!", "Trebuchet18", pos:ToScreen().x, pos:ToScreen().y, color_white, 0, 0, 1, color_black)
end
end )
(I also tried "for k,v in pairs(ents.FindByClass("my_ent"))" instead of "for k,v in pairs(ents.GetAll()) ..")
Well, the thing is that the code is working, but only if the player was near the entity. If the player was not even near the entity, the code will not work as if the entity did not exist for the player.
The player can move near to the entity and then move away, the player then sees the text of the entity. But if the player was already away from the beginning, the player does not see the text of the entity.
But I dont want that. I hope you understand what I mean (sorry if I have any mistakes with my english).
Maybe someone knows why and how I could fix this?
Thanks in advance
Some entities don't transmit to clients if they're far away which I'm betting is the cause for your problem, see http://wiki.garrysmod.com/page/ENTITY/UpdateTransmitState and TRANSMIT Enumerations
Regardless, see that in order to simply access all your entities directly, you're iterating all entities in the level and you're only proceeding if classes match - and you're doing this literally every frame. Instead you should consider holding a table of your entities that you can quickly access, or simply the single entity itself if there's only one of it for example
Hey, thanks for your answer.
how do you mean with holding a table of my entities?
For example you could have a table myEnts = {}, and then on hook.Add("OnEntityCreated") do table.insert(myEnts, newEntity) if the class matches
I think I got it
local myEnts = {}
hook.Add( "OnEntityCreated", "EntList", function( ent )
if ent:GetClass() != "my_ent" then return end
table.insert(myEnts, newEntity)
end )
hook.Add( "HUDPaint", "LookForEnt", function()
local pos
for k,v in pairs(myEnts) do
pos = v:GetPos()
draw.SimpleTextOutlined("I'm Here!", "Trebuchet18", pos:ToScreen().x, pos:ToScreen().y, color_white, 0, 0, 1, color_black)
end
end )
So woud you mean that in my case I think?
But if I put the OnEntityCreated Hook clientside, does it work just for the local player or will all entities be inserted into the table created of all players?
That's about right, if myEnts is built clientside then each player will have their own myEnts table. And yes, in the case that an entity is removed, you can just use IsValid() and if it isn't, remove it from the table (if it's still there for some reason) and continue to the next iteration
Thanks for your help!
Sorry, you need to Log In to post a reply to this thread.