Working with Entities?

Hey again people.
Kayvoo here for some more help xD

I want to get started working with entities. Spawning them, editing them etc.
My goal is a NPC system with access to menu’s uppon using them (not moving and thinking NPC’s)

For this I can predict that I would need quite a lot of knowledge.

Some things I want to learn:

  1. Using players eye position to spawn entities at certain spots.
  2. Spawning these Shopper NPC’s and saving their location.
  3. Working with entities in general. (the entities folder of the gamemode)

I am only oriented towards gamemode development, and I do not wish to make entities in general.

Any kind of help or suggestion will be appreciated a lot!
Thanks in advance.

Use ply:GetEyePos() and TraceLine



--Range of 100 for trace line
--Filter the player out
local tr = util.TraceLine({start = ply:GetEyePos(), endpos = ply:GetEyePos() + ply:GetAimVector() * 100, filter = ply})
local ent = ents.Create("prop_physics")
ent:SetPos(tr.HitPos + tr.HitNormal)
ent:Spawn()
ent:Activate()


  1. Depends on what you want to save it to.
    Use the file library if you want it to be saved locally in the data folder, or use MySQL for it to be stored and shared across servers.
  1. You have to create a new folder for each new entity, with the 3 files cl_init.lua, shared.lua, and init.lua. To create this entity, you have to put in the name of the folder in ents.Create
    init.lua:


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
--Init stuff
end


cl_init.lua:



include("shared.lua")
function ENT:Initialize()
--Init stuff
end


shared.lua:



ENT.Type 				= "anim"
ENT.RenderGroup = RENDERGROUP_OPAQUE
ENT.PrintName			= "EntityName"
ENT.Author				= "Author"
ENT.Contact				= ""

ENT.Spawnable			= false
ENT.AdminSpawnable		= false


Okay awesome!
If I wanted to save the NPC positions in a MySQL database that I own. How?
And if not… then any other ways you would suggest?

You can use JSON to save a file with a table containing all the NPCs on your server.

Although I prefer MySQL. To save into the MySQL database, you need to know a little bit of SQL scripting.

If you decide to go for MySQL, I recommend you go for a library called “tmysql”.
I believe this is a good link: https://code.google.com/p/gmodmodules/source/browse/trunk/gm_tmysql3/Release+-+GM13/gmsv_tmysql.dll?r=224
(I can’t remember where I got it myself)

If you are going for SQL then use this module instead http://forum.facepunch.com/showthread.php?t=1220537 this goes in lua/bin