Facepunch / garrysmod-issues

Garry's Mod issue tracker
147 stars 56 forks source link

GetModel can return no value #3695

Closed Kefta closed 4 years ago

Kefta commented 5 years ago

Code:

hook.Add("OnEntityCreated", "", function(pEntity)
    print(pEntity:GetModel() == nil)
end)

A number of entities when the map spawns don't have models on the server nor client. Spawning NPCs in single-player will result in them having a nil model serverside but a string clientside. Credit to @Earu for noticing this.

Earu commented 5 years ago

This does not only happen with NPCs but with other types of entities too. I've been testing this in fact and in most cases the server realm doesn't know about the model of the entity but the client realm does (as in GetModel returns nil serverside).

ZehMatt commented 5 years ago

I think the reason for this is that OnEntityCreate doesn't mean its fully spawned yet, just got created, equivalent of calling ents.Create(class) and then calling this function right away before setting any model or position yet. I normally wait one frame to make sure all the data is set.

robotboy655 commented 5 years ago

I would imagine its point entities which cannot have a model.

Earu commented 5 years ago

@ZehMatt Even so the behavior is different on server and client for this, which should not be the case. @robotboy655 My tests were made with normal props no fancy brush entity or whatsoever.

ZehMatt commented 5 years ago

Its different because the client will initialize the entity from a snapshot and will be created on the client with all data set already, thats just how source works.

thegrb93 commented 5 years ago

That's fine and everything, but empty string would be more appropriate than nil/novalue imo.

robotboy655 commented 5 years ago

Which would break every script that uses nil to test if the entity has a model or not, unfortunately.

Even the base game itself https://github.com/Facepunch/garrysmod/blob/394ae745df8f8f353ea33c8780f012fc000f4f56/garrysmod/gamemodes/sandbox/entities/effects/propspawn.lua#L18

thegrb93 commented 5 years ago

Then the wiki page needs to be updated to 'returns string or nil'

Kefta commented 4 years ago

This is intentional behaviour.