NPC Shop Tutorial

From GMod Wiki

Jump to: navigation, search
NPC Shop Tutorial
Page white text.png Description:This tutorial will teach you how to create a simple shop menu for when you press "E" on a certain NPC
link=User:LuckyLuke Original Author:LuckyLuke
Group.png Contributors:Crazy Quebecer
Calendar.png Created:June 5, 2010
Notepad-48.png Note : This tutorial assumes you know how to create a scripted entity and only covers the parts that are required to make a working shop NPC. A working example is included at the end.


Creating a shop NPC is not very difficult as it is the simplest form of a NPC. You don't really need to move or react to indirect events. You will have to set it up like a regular scripted NPC but won't have to play with AI or schedules. The other thing you will need is a way to tell when the player wants to interact with your NPC and react to it.


Shared

The shared.lua file is traditionally included by the server and the client lua states, so it is a good place to setup information that will be useful to both. It is important for this code to be ran before the rest, so it should be included in both your server and client init files.

ENT.Base = "base_ai" -- This entity is based on "base_ai"
ENT.Type = "ai" -- What type of entity is it, in this case, it's an AI.
ENT.AutomaticFrameAdvance = true -- This entity will animate itself.
 
function ENT:SetAutomaticFrameAdvance( bUsingAnim ) -- This is called by the game to tell the entity if it should animate itself.
	self.AutomaticFrameAdvance = bUsingAnim
end


Serverside

The following initialization code will setup your entity to be a regular NPC. Make sure to have included shared.lua before this.

function ENT:Initialize( ) --This function is run when the entity is created so it's a good place to setup our entity.
 
	self:SetModel( "models/humans/group01/female_01.mdl" ) -- Sets the model of the NPC.
	self:SetHullType( HULL_HUMAN ) -- Sets the hull type, used for movement calculations amongst other things.
	self:SetHullSizeNormal( )
	self:SetNPCState( NPC_STATE_SCRIPT )
	self:SetSolid(  SOLID_BBOX ) -- This entity uses a solid bounding box for collisions.
	self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD ) -- Adds what the NPC is allowed to do ( It cannot move in this case ).
	self:SetUseType( SIMPLE_USE ) -- Makes the ENT.Use hook only get called once at every use.
	self:DropToFloor()
 
	self:SetMaxYawSpeed( 90 ) --Sets the angle by which an NPC can rotate at once.
 
end

The traditional way to interact with NPCs is by using them. The following code will detect that event.

function ENT:AcceptInput( Name, Activator, Caller )	
	if Name == "Use" and Caller:IsPlayer() then
		umsg.Start("ShopNPCUsed", Caller) -- Prepare the usermessage to that same player to open the menu on his side.
		umsg.End() -- We don't need any content in the usermessage so we're sending it empty now.
	end
end

This function is ran every time the entity receives an input. If a player sent the "Use" input we can proceed with sending that player an usermessage to trigger the clientside part of our shop NPC.


Clientside

Here you have to hook to the usermessage sent by the server and trigger your shop menu with it. Refer to the Derma tutorials for a good idea on how to create a Derma Menu.

After you have created the Derma Menu inside a function, it should look like this :

local function myMenu()
	--Derma Stuff ( All localed, we don't want it to be global. )
end
 
usermessage.Hook("ShopNPCUsed",myMenu) --Hook the menu, so we can use it Serverside


Example

Hopefully these few pointers should have given you a fair idea of how to make a shop NPC of your own. Click here to download a working example.

Notepad-48.png Erratum : The example's cl_init.lua file has a function that is declared as a global. It would be much safer to have this be a local function.


See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox