Get an entity model from classname

Hey everyone ! I was wondering if there was a way to get the model of an entity with the classname, or if I needed to spawn the entity then do ENT:GetModel().

You’ll have to spawn it first. There is no system that attaches entity classes to models.

AH !
Thanks I’ll try it ^^

What do you need this for? Most entities in Source don’t have a particular model inherent to their class, but some (such as weapons) only ever use one model.

If you know ahead of time which model an entity uses, you may be able to just make a table mapping classnames to models and look it up.

[lua] local models = {weapon_smg = “weapons/weapon_smg.mdl”, …}
function GetModelFromClassname(classname) return models[classname] end[/lua]

Most basic example.

No I cant use this, the user will entern the classname of the ent and it needs to find the model, that’s for a npc buy system

Doesn’t work for engine entities, and that just gets you the class table. While weapons have ViewModel and WorldModel keys across all SWEPs, entities don’t have a shared key for the model.

What NPCs do they buy? Stock HL2 NPCs? What if an NPC uses a randomized model, such as npc_citizen? There’s no way to get a single model from a classname for sure.

How do you want this system to work and what for? If you can elaborate further, we can probably try to suggest you alternative designs and implementation.

IMO, trying to buy an entity by forcing the user to enter a classname sounds like a pretty terrible design choice in the first place.

In fact there will be a npc which you can talk to, in order to open a GUI, and in this gui you will be able to buy preselected ents in the config, so I wanted a way to only enter the classname and to get the model ^^

Why don’t you in your config add a model field, that’s what EVERYONE does