• Spawn Entity on Initialize
    6 replies, posted
Hi I'm making an addon with an NPC. Everything is working, but I can't figure out how to make it so that the NPC spawns at a certain pos & angle when the map initializes. How would I go about doing this? right now I have in the addonname/lua/entities/entityname/init.lua: [CODE]function GM:Initialize() self:Spawn() self:SetPos( -159.360626 -970.589050 -131.968750 ) self:SetAngle( 0,0,0 ) end[/CODE] But that didn't quite work, lol. Could nayone help me out with this? Thanks :)
You need to use the PostInitEntity hook instead. That's when you can begin interacting with entities. [editline]15th July 2015[/editline] Also, the variable 'self' won't be what you expect it to be in that hook. Read this for a better understanding of it: [url]http://www.lua.org/pil/16.html[/url] You'll want to create a new entity using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/Create]ents.Create[/url]. Something like this: [code] local myEnt = ents.Create("Class_Of_Your_Entity") myEnt:Spawn() myEnt:... [/code]
I replaced Initialize with InitPostEntity, with no luck. Same with adding the variable thing. Current code: [CODE]function GM:InitPostEntity() local myEnt = ents.Create("npc_seller") myEnt:SetModel( "models/Barney.mdl" ) myEnt:SetPos( Vector( -159.36, -970.5, -132 ) ) myEnt:Spawn() end [/CODE]
Use a hook rather than overwriting the function [lua] hook.Add( "InitPostEntity", "spawnnpc", function() local myEnt = ents.Create("npc_seller") myEnt:SetModel( "models/Barney.mdl" ) myEnt:SetPos( Vector( -159.36, -970.5, -132 ) ) myEnt:Spawn() end ) [/lua] If that still doesn't work, is by any chance your entities base base_nextbot? I've noticed some issues with spawning them in on that hook.
Woohoo! That worked! Thanks!
lol had that question too Thanks for clearing it up XD
-snip-
Sorry, you need to Log In to post a reply to this thread.