• How do I access a variable from ENT:Initialize?
    4 replies, posted
function ENT:Initialize() self:SetModel("models/props_lab/crematorcase.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end self.isBaking = false self.finishBakeTime = 0 getPos = self:GetPos() end function ENT:StartTouch(ent) if ent:GetClass() == "morphine_uncooked" and self.isBaking == false then ent:Remove() self.isBaking = true startCooking() end end function startCooking() timer.Create("waitThenRemove", 5, 1, function() local morphine = ents.Create("morphine_cooked") morphine:SetPos(getPos + Vector(0,15,0)) morphine:Spawn() end) end This is pretty much what i have right now. Basically the timer does not know what "self.isBaking" means and cannot access it. How do I access it from inside the function "isCooking"?
To use self inside the startCooking function you need to name the function ENT:startCooking(), this will allow you to use self.isBaking inside the function. Now in the timer function self is no longer defined as the ENT so you can create a local variable inside the startCooking function. Here's an example: function ENT:startCooking() --You can use self.isBaking in this function local isBaking = self.isBaking timer.Create("waitThenRemove", 5, 1, function() local morphine = ents.Create("morphine_cooked") morphine:SetPos(getPos + Vector(0,15,0)) morphine:Spawn() print(isbaking) --Will print self.isBaking end) end
It worked thanks
Okay wait what are you doing? Why are you starting a timer that runs for one second, a given number of times, decreasing the number of times it should run each time...? I’m so confused about everything you’re trying to do... What I’m trying to say is that the code looks like it should work, but it depends when this stuff is called. Could you just post all of your entity’s code?
I got it working, but thanks for help. Maybe you can answer my most recent question regarding cam 3D2D :p
Sorry, you need to Log In to post a reply to this thread.