• Newly coded entity not spawning.
    9 replies, posted
Hey all, me again. Wanted to make a new entity (flying ship) as some practice, but the blasted thing won't spawn. I remember SWEPs doing this in the past also. Here is the code for each file (yes it may seem familiar to certain developers, I'm just doing this for practice.) [b]cl_init.lua[/b] [lua]include("shared.lua") function ENT:Initialize() print("It initialized. So far so good.") end function ENT:Draw() self:DrawModel() end[/lua] [b]shared.lua[/b] [lua]ENT.Type = "anim" ENT.Base = "base_anim" ENT.Author = "KillerSteel" ENT.Purpose = "Learning, and for Horizon once I get that done :/." ENT.Contact = "Contact myself? Hurr." ENT.PrintName = "FLYING BOMB" ENT.Category = "Horizon Test Ships" ENT.Spawnable = true ENT.AdminSpawnable = true function ENT:GetSpeed() return self:SetNWInt("Speed",0) end[/lua] [b]init.lua[/b] [lua]--[[ Plans for current ship...well: 1. ENT:Use() still needs to be coded. 2. Valid weaponry needs to be added, with similar entity. 3. Finalization and cleaning of code 4. EXTREME testing before addition to gamemode. ]] AddCSLuaFile("shared.lua") AddCSLuaFile("cl_init.lua") include("shared.lua") function ENT:Initialize() self:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() phys:EnabledGravity(false) end end function ENT:Think() if ply:KeyDown(IN_ATTACK) then ENT:Shoot() end end function ENT:PhysicsSimulate(phys,deltatime) local ply = self:GetPlayer() phys:Wake() local pr = {} pr.secondstoarrive = self.RotateSpeed*8 pr.pos = self.Entity:GetPos()+self.Entity:GetForward() pr.maxangular = 5000 pr.maxangulardamp = 10000 pr.maxspeed = 1000000 pr.maxspeeddamp = 10000 pr.dampfactor = 0.8 pr.teleportdistance = 5000 pr.deltatime = deltatime pr.angle = Angle(self:GetAngles().p,self:GetAngles().y,0) if ply:KeyDown(IN_ATTACK2) then pr.angle = Angle(ply:GetAimVector():Angle().p,ply:GetAimVector():Angle().y,(self:GetAngles().y-ply:GetAimVector():Angle().y)) end plys:ComputeShadowControl(pr) end function ENT:Shoot() DebugPrint("Shooting something, fire check") end function ENT:Use(ply,caller) self:GetPhysicsObject():Wake() self:GetPhysicsObject():EnableMotion(true) ply:Spectate( OBS_MODE_CHASE ) ply:DrawViewModel( false ) ply:DrawWorldModel( false ) ply:StripWeapons() ply:SetScriptedVehicle( self ) end[/lua] Any notions? Tips? Possible clean up strategies (because I know this isn't the cleanest it can be)? Possibly a fix?
Error: line 26, attempt to index local 'ply' (a nil value) Error: line 32, attempt to call function 'GetPlayer' (a nil value) Error: line 50, attempt to index local 'plys' (a nil value) Error: line 54, attempt to call function 'DebugPrint' (a nil value) Just to name a few. Other than that, it could be that you aren't spawning it correctly.
Didn't get any errors on my attempts (damn thing just didn't spawn, couldn't do any GOOD testing.) Is my code really that bugged?
SENT errors don't show up when you spawn it, they show up when you load a map. Look further up in the console for them.
[lua] --[[ Plans for current ship...well: 1. ENT:Use() still needs to be coded. 2. Valid weaponry needs to be added, with similar entity. 3. Finalization and cleaning of code 4. EXTREME testing before addition to gamemode. ]] AddCSLuaFile("shared.lua") AddCSLuaFile("cl_init.lua") include("shared.lua") function ENT:Initialize() self:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() phys:EnabledGravity(false) end end function ENT:Think() local ply = self:LocalPlayer() if ply:KeyDown(IN_ATTACK) then ENT:Shoot() end end function ENT:PhysicsSimulate(phys,deltatime) local ply = LocalPlayer() phys:Wake() local pr = {} pr.secondstoarrive = self.RotateSpeed*8 pr.pos = self.Entity:GetPos()+self.Entity:GetForward() pr.maxangular = 5000 pr.maxangulardamp = 10000 pr.maxspeed = 1000000 pr.maxspeeddamp = 10000 pr.dampfactor = 0.8 pr.teleportdistance = 5000 pr.deltatime = deltatime pr.angle = Angle(self:GetAngles().p,self:GetAngles().y,0) if ply:KeyDown(IN_ATTACK2) then pr.angle = Angle(ply:GetAimVector():Angle().p,ply:GetAimVector():Angle().y,(self:GetAngles().y-ply:GetAimVector():Angle().y)) end phys:ComputeShadowControl(pr) end function ENT:Shoot() print("Shooting something, fire check") end function ENT:Use(ply,caller) local ply = self:LocalPlayer() self:GetPhysicsObject():Wake() self:GetPhysicsObject():EnableMotion(true) ply:Spectate( OBS_MODE_CHASE ) ply:DrawViewModel( false ) ply:DrawWorldModel( false ) ply:StripWeapons() ply:SetScriptedVehicle( self ) end [/lua]
For one, LocalPlayer() is client side, so by running that in init.lua, you're calling a value that doesn't exist.
use [lua]ply[/lua]instead of [lua]LocalPlayer()[/lua]
[QUOTE=HeavyMtl123;22158827]use [lua]ply[/lua]instead of [lua]LocalPlayer()[/lua][/QUOTE] You need to define player. Wouldn't work if not defined.
You're doing shit with retrieving the player who is controlling that entity. It's not ply, it's not self:LocalPlayer(), and it's not self:GetPlayer(). If your entity does NOT collide with its owner, and you have no problem with that, you can use self:SetOwner() to set the owner of that entity to a given player, and self:GetOwner() to retrieve it. Else, you can just set a variable inside of that entity whenever it's spawned. Just declare [b][url=wiki.garrysmod.com/?title=ENT.SpawnFunction]ENT.SpawnFunction [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and fill it like the wiki says. The "ply" argument of that function is the player who spawned it, all you have to do is store it somewhere, either in a variable (like, you can call it self.Owner), or via self:SetOwner(). [editline]09:16AM[/editline] Oh, wait, it seems that the owner of the entity is determined by the player who pressed "use" on it. Well, it pretty much works the same way, since the first parameter of the Use hook is the player who triggered it. You'll have to make sure to handle the case where the entity does NOT have an owner. If the entity has an owner, ValidEntity(self.Owner) (or ValidEntity(self:GetOwner()), depending on which method you used) should be true.
Sorry, you need to Log In to post a reply to this thread.