My First Entity

After several searches, I’ve been able to put together bits and pieces of generic entity code. This is what I have gathered:

init.lua:



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

function ENT:Initialize()
    self:SetModel("models/Items/Flare.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetMoveType(MOVETYPE_VPHYSICS)
    self:SetSolid(SOLID_VPHYSICS)
    local phys = self:GetPhysicsObject()
    if phys:IsValid() then phys:Wake() end
end

function ENT:Touch( ply )  
    if ( ply:IsValid() and ply:IsPlayer() ) then  
        Msg("Taco")
        self.Entity:Remove()  
    end  
end  

cl_init.lua:



include("shared.lua")

function ENT:Initialize()
end

function ENT:Draw()
    self.Entity:DrawModel()
end

shared.lua


ENT.Type         = "anim"
ENT.Base         = "base_entity"

ENT.PrintName    = ""
ENT.Author        = ""
ENT.Contact        = ""

ENT.Spawnable            = true
ENT.AdminSpawnable        = false
ENT.RenderGroup         = RENDERGROUP_OPAQUE

AddCSLuaFile( "shared.lua" )

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

    
end

In my gamemode code, I have this item spawn. Currently, it just … floats (whereas other entities drop to the ground). Also, I added the ENT:Touch hoping this would work for simulating a player ‘picking up’ an item. Right now, the player just walks through the item, nothing happens.

I don’t know anything about scripting entities and I’ve found no tutorials on the subject. Any thoughts on making this work?

Thanks

You’re not activating the entity. Also, self.Entity is deprecated, use self.

Looks quite neat for a first entity. Well done!

Will do.

[editline]01:02PM[/editline]

Well, I can hardly take any credit for it. I stole code from different posts to try and put this together. Thanks, tho

[editline]01:05PM[/editline]

This is my code in the gamemode script that spawns the item


        local Drop = ents.Create("test")
        Drop:SetPos(NPCPos + Vector (0,0,SpawnHeight))
        Drop:Spawn()
        Drop:Activate()    

It is activated there, is that enough?

So far, adding activate to the entity lua has had no effect. It still floats and nothing happens when I walk through it.

For touch change ply to hitEnt, the floating thing happens to my stuff too but it normally drops once I touch it.

If it still floats in the air and has no collision after changing that then try removing



    local phys = self:GetPhysicsObject()
    if phys:IsValid() then phys:Wake() end

Thats the only other thing I could think of.

Changing ply to hitEnt shouldn’t change anything. I should be able to use whatever name I want there…

Attempted both suggestions, absolutely no change. :frowning:

Edit:

Ok, think it had something to do with the model… somehow. I changed it to a large prop and suddenly it works. It drops to the ground and the touch works. Any idea why?

Edit2:

It has to just be that some models can’t have physics applied to them… and it will somehow cause the whole script to not work. I changed it to

models/props_junk/garbage_takeoutcarton001a.mdl

A small chinese take-out carton, works fine. Doesn’t fit in with my game AT ALL, but… it’s the best I can do, apparently.