• Issue with entity spawning
    1 replies, posted
So, I downloaded the base entity file for gmod 13 on garrysmods.org. Everything works, however the way it's set up is I need to define the class at the top of the file, like so. [CODE]local classname = "insert class here"[/CODE] I don't want to do this for every ent and every new one that I create, so I did this for the SpawnFunction. [CODE]function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 25; local ent = ents.Create( self:GetClass() ) ent:SetPos( SpawnPos ) ent:Spawn() ent:Activate() ent:DropToFloor() return ent end[/CODE] This is the error I get when I use self:GetClass() [CODE][ERROR] gamemodes/glife/entities/entities/money_printer/init.lua:29: attempt to call method 'GetClass' (a nil value) 1. SpawnFunction - gamemodes/glife/entities/entities/money_printer/init.lua:29 2. Spawn_SENT - gamemodes/sandbox/gamemode/commands.lua:610 3. unknown - gamemodes/sandbox/gamemode/commands.lua:675 4. unknown - lua/includes/modules/concommand.lua:54[/CODE] Basically I'm trying to make it dynamic so it just get's the class leaving no need to define it if you haven't figured out yet. If you need any more info please ask, thanks!
Try this. [CODE] function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 25; local class = self:GetClass() local ent = ents.Create( class ) ent:SetPos( SpawnPos ) ent:Spawn() ent:Activate() ent:DropToFloor() return ent end [/CODE] I think your issue is that the "ents.Create" function passes the argument "self:GetClass()" to the function, and when the "ents.Create()" function runs with the argument "self:GetClass()", "self" [i]might[/i] be different than what you're hoping it is. I could just be talking out of my ass, too, but you can at least try this.
Sorry, you need to Log In to post a reply to this thread.