• How to create a NPC with Lua
    12 replies, posted
Hey, i want to spawn a NPC via Function. Can Someone help me ?
if( !IsValid(begleiter)) then return end begleiter isn't even in your code anywhere. Of course it won't work. If begleiter doesn't even exist then how can it be valid?
You probabaly mean to do this. npc = ents.Create( "npc_mandalorian" ) if !npc:IsValid() then return end npc:SetModel("models/porky-da-corgi/starwars/mandalorians/bountyhunter.mdl") npc:SetPos(ply:GetPos()) npc:Spawn() Also, here's a heads up. If the model isn't meant for the NPC (like the NPC has special move types that are only in certain models) then some pretty freaky shit can happen. If the model you are setting is the same as the default model, (this only applies for NPCs or maybe some other things), you don't have to FYI. And also, you proooobabaly want to make this local. Just a nagging feeling. local npc = ents.Create( "npc_mandalorian" ) if !npc:IsValid() then return end npc:SetModel("models/porky-da-corgi/starwars/mandalorians/bountyhunter.mdl") npc:SetPos(ply:GetPos()) npc:Spawn()
You're right, but my problem is that the code does not even spawn a broken NPC but just nothing. The Variable begleiter was there, because my Variables was on german and i just turned them quick into english but I forget that one. My whole Code was function begleiterSpawn(ply, text, public)     if (string.sub(text, 1, 16) == "/begleiter holen") then         begleiter = ents.Create( "npc_mandalorian" )         if( !IsValid(begleiter)) then return end         begleiter:SetModel("models/porky-da-corgi/starwars/mandalorians/bountyhunter.mdl")         begleiter:SetPos(ply:GetPos())         begleiter:Spawn()     end end hook.Add( "PlayerSay", "begleiter", begleiterSpawn )
That code should work, are you running it serverside?
I did it on Shared. I'm pretty new to Lua and don't know when to use Client-Side Shared or Server-Side at all.
Put it in garrysmod/lua/autorun/server/listenforchatcom.lua and please make it local. If you don't, the casualties will be enormous, swallowing everything in it's path. local begleiter = ents.Create( "npc_mandalorian" )
Using IsValid vs ent.IsValid will never make a difference with entities, just more efficient to do the latter where applicable.
Isn't IsValid already internally using the object's IsValid() function if it has one?
Then wouldn't it be more efficient to just use it from the get-go? Or is it one of those nitpicky "draw vs surface" things?
Most draw functions are ease-of-use shortcuts to many surface functions, - they each have their own place and isn't really a "nitpick." IsValid() can be useful shortcut in situations where you are dealing with nil and NULL both (panels), but not really so in terms of entities. Either way, the efficiency gain is marginal to unnoticeable and the real benefit is from knowing exactly which types you are dealing with, something that is very easy to ignore with Lua.
Personaly, I have the habit to use IsValid(object), I believe it is as efficient as using object:IsValid(), and you can't fuck anything up with it. As for the draw/surface, sometimes you need to use one over the another, anyone saying you should not use draw or surface is wrong
That's the main problem: treating entities and nil alongside each other will just result in not knowing when improper arguments/returns are passed. While in most cases this won't be a problem, type-mixing in general without explicit handling can lead to inconsistency and difficulty in debugging.
Sorry, you need to Log In to post a reply to this thread.