• How to make an SNPC that can be used
    10 replies, posted
How can I make an snpc that can have a menu open on it? I tried this but it doesnt work: cl_init: [lua] include('shared.lua') ENT.RenderGroup = RENDERGROUP_BOTH /*--------------------------------------------------------- Name: Draw Desc: Draw it! ---------------------------------------------------------*/ function ENT:Draw() self.Entity:DrawModel() end /*--------------------------------------------------------- Name: DrawTranslucent Desc: Draw translucent ---------------------------------------------------------*/ function my_message_hook( um ) local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 500, 400 ) -- Size of the frame DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( false ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup( true ) Msg( "The message has been received!" ) end usermessage.Hook("my_message", my_message_hook) function ENT:DrawTranslucent() self:Draw() end [/lua] Init: [lua] function ENT:Initialize() self:SetModel( "models/Police.mdl" ) self:SetHullType( HULL_HUMAN ); self:SetHullSizeNormal(); self:SetSolid( SOLID_BBOX ) self:SetMoveType( MOVETYPE_STEP ) self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_OPEN_DOORS | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_USE_SHOT_REGULATOR | CAP_AIM_GUN ) self:SetMaxYawSpeed( 5000 ) self:SetUseType(SIMPLE_USE) //don't touch stuff above here self:SetHealth(99999) //self:Give( "weapon_ak47" ) //Can be given sweps. local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:Use(activator,caller) Print("Hey") umsg.Start("my_message", activator) umsg.End("my_message") end function ENT:OnTakeDamage(dmg) self:SetHealth(self:Health() - dmg:GetDamage()) if self:Health() <= 0 then //run on death self:SetSchedule( SCHED_FALL_TO_GROUND ) //because it's given a new schedule, the old one will end. end end [/lua] Shared: [lua] ENT.Base = "base_ai" ENT.Type = "ai" ENT.PrintName = "" ENT.Author = "" ENT.Contact = "" //fill in these if you want it to be in the spawn menu ENT.Purpose = "" ENT.Instructions = "" ENT.AutomaticFrameAdvance = true /*--------------------------------------------------------- Name: OnRemove Desc: Called just before entity is deleted ---------------------------------------------------------*/ function ENT:OnRemove() end /*--------------------------------------------------------- Name: PhysicsCollide Desc: Called when physics collides. The table contains data on the collision ---------------------------------------------------------*/ function ENT:PhysicsCollide( data, physobj ) end /*--------------------------------------------------------- Name: PhysicsUpdate Desc: Called to update the physics .. or something. ---------------------------------------------------------*/ function ENT:PhysicsUpdate( physobj ) end /*--------------------------------------------------------- Name: SetAutomaticFrameAdvance Desc: If you're not using animation you should turn this off - it will save lots of bandwidth. ---------------------------------------------------------*/ function ENT:SetAutomaticFrameAdvance( bUsingAnim ) self.AutomaticFrameAdvance = bUsingAnim end [/lua] I don't know whats wrong, or why the menu wont open.. I tried touching the ent and that worked, but using the ent doesn't work.
If you take a closer look at the [b][url=http://wiki.garrysmod.com/?title=Entity_Hooks]Entity Hooks[/url][/b] page, you'll see that ENT.Use is a hook that is only valid for an "anim" sent. You are trying to create an SNPC, or an "AI" sent. There is no ENT.Use hook for AI, so you'll have to do something in the think hook, like check if a player is looking at the NPC, holding use, and is close enough.
How do you make it so if there looking at it and holding use and close enough? Edit: Found out instead of using USE you use: ENT:AcceptInput( name, activator, caller )
No dude. If you'll notice, that is also not in the AI category. There is no easy way of doing this. (well, if that works then please tell me - it should be on the wiki)
It worked... but then it wouldn't update after that. Like when I reload the game and map. So ya it doesn't work either.. How can I get it to work? What does PERP and Darkland do?
GM:PlayerUse( ply, Entity ) If the entity is that then do stuff end =)
[QUOTE=emperus;19874808]GM:PlayerUse( ply, Entity ) If the entity is that then do stuff end =)[/QUOTE] There is 'ENT:AcceptInput or ENT:Use'. But on SNPC you have to use AcceptInput cuz I had this problem before and it seems that ENT:Use is broken/does not exist on snpc.
As a last resort you could always have it trace a line when you press e which checks the entity for being your snpc.
Here's how I do it, not perfect but works well enough : [lua]hook.Add("PlayerInitialSpawn", "SetLastUse",function(ply) if !ply.LastUse then ply.LastUse = 0 end end) -- This is just to stop USE from being spammed, using Entity.SetUseType is probably better. local delay = 1 --Time between each activation function ENT:AcceptInput( Name, Activator, Caller ) if( Name == "Use" ) and CurTime() >= Activator.LastUse + delay then Activator.LastUse = CurTime() -- Stuff happens here end end[/lua]
How can I make its so the use box on it is smaller? [lua] self.Entity:SetModel("models/Humans/Group01/Female_01.mdl") self.Entity:PhysicsInit(SOLID_VPHYSICS) self.Entity:SetMoveType(MOVETYPE_STEP) self.Entity:SetSolid(SOLID_BBOX) self.CanUse = true [/lua] Like I can press e next to it, and it still opens the menu.
[QUOTE=Brodster55;19883977]How can I make its so the use box on it is smaller? self.Entity:SetModel("models/Humans/Group01/Female_01.mdl") self.Entity:PhysicsInit(SOLID_VPHYSICS) self.Entity:SetMoveType(MOVETYPE_STEP) self.Entity:SetSolid(SOLID_BBOX) self.CanUse = true Like I can press e next to it, and it still opens the menu.[/QUOTE] The 'use box' as you call it is the 'hitbox' of the model. If you want a smaller hitbox then you'd have to use a different model. Alternatively you could do some calculations so that if the angle between the way you're facing and the target is too large then it won't give the box.
Sorry, you need to Log In to post a reply to this thread.