• "Dead" Entity
    10 replies, posted
So I'm using ents.Create to create an NPC on keypress, it then later gets removed by a simple timer. Here's my current code being ran in the server. [code] hook.Add("KeyPress", "test3", function(ply, key) local Weapon = ply:GetActiveWeapon() if (key == IN_RELOAD) then if ( IsValid( Weapon ) and Weapon:GetClass() == "weapon_crowbar" ) then local ent2 = ents.Create("npc_citizen") local ent = ply:GetEyeTrace().Entity ent2:SetModel(ply:GetModel()) ent2:SetPos(ply:GetEyeTrace().HitPos) timer.Simple(5, function () local effectdata = EffectData() util.Effect("invisSmoke", effectdata) effectdata:SetOrigin(ent2:GetPos()) end) timer.Simple(5.1, function() ent2:Remove() end) print (ply:GetEyeTrace().HitPos) end end end) [/code] It works fine but the entity that is being spawned is what I'm calling "dead" it doesn't act like an npc it just spawn a player model, that doesn't move or look around, players don't collide with it. It's just a player model. I'd like if someone could direct me to where I could bring this entity "to life". Thanks.
You need to do [lua]ent2:Spawn()[/lua]
I think you need to call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/Spawn]Entity:Spawn[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/Activate]Entity:Activate[/url] on it- and maybe add some spawnflags [URL="https://github.com/garrynewman/garrysmod/blob/9abd01e5ca27efe5abe33384a7b13d9e326b7540/garrysmod/gamemodes/sandbox/gamemode/commands.lua#L368"]like GMod does by default[/URL] if you want it to constantly think or something [editline]13th July 2016[/editline] ninja'd
[QUOTE=MPan1;50699563]I think you need to call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/Spawn]Entity:Spawn[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/Activate]Entity:Activate[/url] on it- and maybe add some spawnflags [URL="https://github.com/garrynewman/garrysmod/blob/9abd01e5ca27efe5abe33384a7b13d9e326b7540/garrysmod/gamemodes/sandbox/gamemode/commands.lua#L368"]like GMod does by default[/URL] if you want it to constantly think or something [editline]13th July 2016[/editline] ninja'd[/QUOTE] Alright well I did that and the entity spawns and despawns instantly. [code] hook.Add("KeyPress", "test3", function(ply, key) local Weapon = ply:GetActiveWeapon() if (key == IN_RELOAD) then if ( IsValid( Weapon ) and Weapon:GetClass() == "weapon_crowbar" ) then local ent2 = ents.Create("npc_citizen") local ent = ply:GetEyeTrace().Entity ent2:Spawn() ent2:Activate() ent2:SetModel(ply:GetModel()) ent2:SetPos(ply:GetEyeTrace().HitPos) timer.Simple(5, function () local effectdata = EffectData() util.Effect("invisSmoke", effectdata) effectdata:SetOrigin(ent2:GetPos()) end) timer.Simple(5.1, function() ent2:Remove() end) print (ply:GetEyeTrace().HitPos) end end end) [/code] Not sure what you mean by spawnflags, maybe you could explain it a little more? EDIT: after messing around with it just spawning it in over and over again after it gets removed there was one time where it actually spawned and functioned like an NPC. [editline]12th July 2016[/editline] -snip- automerge
Also, just gonna throw it out there - player models and NPCs use different models, from what I recall, so if you're assigning an NPC to 'models/player/alyx.mdl' for example, they're gonna t-pose
You should always use IsValid when indexing objects not created in your timer because they might not be valid anymore and will cause your script to error. In those 5 seconds that passed, ent2 can be invalid.
Well i got it working correctly but whenever I spawn the entity on a rooftop or any area with missing space underneath it. The npc instantly despawns. Anyone know why?
[QUOTE=Thane;50701024]Well i got it working correctly but whenever I spawn the entity on a rooftop or any area with missing space underneath it. The npc instantly despawns. Anyone know why?[/QUOTE] NPCs like to snap to ground. Try changing this: [code] ent2:SetPos(ply:GetEyeTrace().HitPos) [/code] To this: [code] local tr = ply:GetEyeTrace() ent2:SetPos(tr.HitPos+tr.HitNormal) [/code] Another option would be: [code] ent2:SetPos(ply:GetEyeTrace().HitPos+Vector( 0, 0, 1 )) [/code] What this is doing is its nudging the NPC up a unit so that it doesn't spawn flush with the ground causing it to think its clipping in the world. Hope this solves your problem.
the first solution didn't work. But I tried the second and at first it didn't work until I changed the vector to (0, 0, 10) Although this looks weird as the npc is just floating in mid air. Is there any other options you might have in mind? Somehow I didn't notice this but the NPC doesn't despawn it just teleports to the area below the platform it's being spawned on. [editline]13th July 2016[/editline] [QUOTE=Thane;50701569]the first solution didn't work. But I tried the second and at first it didn't work until I changed the vector to (0, 0, 10) Although this looks weird as the npc is just floating in mid air. Is there any other options you might have in mind? Somehow I didn't notice this but the NPC doesn't despawn it just teleports to the area below the platform it's being spawned on.[/QUOTE] Is there any way I can recreate the exact way that entities are spawned in the spawn menu, because they spawn where you're looking and they don't fall through the ground lol.
[QUOTE=Thane;50701569]the first solution didn't work. But I tried the second and at first it didn't work until I changed the vector to (0, 0, 10) Although this looks weird as the npc is just floating in mid air. Is there any other options you might have in mind? Somehow I didn't notice this but the NPC doesn't despawn it just teleports to the area below the platform it's being spawned on. [editline]13th July 2016[/editline] Is there any way I can recreate the exact way that entities are spawned in the spawn menu, because they spawn where you're looking and they don't fall through the ground lol.[/QUOTE] Try [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=https://wiki.garrysmod.com/page/Entity/DropToFloor]Entity:DropToFloor[/url]. Also, you should check out the original garry's mod methods, they contain shitload of useful stuff, here [img]https://github.com/favicon.ico[/img] [url=https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/sandbox/gamemode/commands.lua]Sandbox:commands.lua[/url] look for line 290. Good luck!
I figured out it's a problem when setting the model for the npc to something other than another npc with the class "npc_civilian" So I've resorted to just letting it spawn in a random civilian each time, and eventually I'll just create an NPC of my own with my desired model and be able to go from there. This way is better because I can make the NPC behave in ways I couldn't before. This way it actually walks around, says things like "Hi", or "Excuse me", Or maybe I can just find one off of the workshop and ask the creator if it'd be alright to use his NPC in an addon of mine. Anyways thanks all for the help! :smile:
Sorry, you need to Log In to post a reply to this thread.