Basic Cooking Script Practise (Refert to Self outside ENT:) better way of coding
7 replies, posted
I don't understand how to use self outside of an ENT: call, how could I use a better function than think and how could I make this code work ?
Thank you guys in advance !
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
-- Values
local IngredientsForBread = {'egg_cookingmania', 'flour_cookingmania', 'milk_cookingmania'}
local CookingDelay = 5
--
function ENT:Initialize()
self:SetModel("models/props_c17/furnitureStove001a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
self:DrawShadow(true)
local phys = self:GetPhysicsObject()
if ( SERVER ) then
self:PhysicsInit( SOLID_VPHYSICS )
end
if phys:IsValid() then
phys:Wake()
end
self.IsCooking = false
self.FinishCookingTime = 0
self.bread = 0
self.egg = 0
self.flour = 0
self.milk = 0
end
function ENT:StartTouch(ent)
if table.HasValue(IngredientsForBread, ent:GetClass()) and self.IsCooking == false then
if ent:GetClass() == "egg_cookingmania" and self.egg < 2 then
ent:Remove()
self.egg = self.egg + 1
IsRecipeComplete()
elseif
ent:GetClass() == "flour_cookingmania" and self.flour < 1 then
ent:Remove()
self.flour = self.flour + 1
IsRecipeComplete()
elseif
ent:GetClass() == "milk_cookingmania" and self.milk < 1 then
ent:Remove()
self.milk = self.milk + 1
IsRecipeComplete()
end
end
end
function ENT:Think()
if self.IsCooking then
self:SetColor(Color(0, 255, 0))
if self.FinishCookingTime <= CurTime() then
self.IsCooking = false
print("I Am Here !")
local bread = ents.Create("bread_cookingmania")
bread:SetPos(self:GetPos() + Vector(0, 0, 25))
bread:Spawn()
end
else
self:SetColor(Color(255, 0, 0))
end
end
function IsRecipeComplete ()
if ENT.egg == 2 and self.flour == 1 and self.milk == 1 then
ENT.IsCooking = true
ENT.FinishCookingTime = CurTime() + 5
end
end
--[[ function ENT:Think()
if self.IsCooking == true then
self:SetColor(Color(255, 0, 0))
else
self:SetColor(Color(0, 255, 0))
end
if self.IsCooking == true then
if self.FinishCookingTime <= CurTime() then
self.IsCooking = false
local bread = ents.Create("bread_cookingmania")
bread:SetPos(self:GetPos() + Vector(0,0,50))
bread:Spawn()
end
end
end ]]--
-- function Cooking ()
-- endAddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
-- Values
local IngredientsForBread = {'egg_cookingmania', 'flour_cookingmania', 'milk_cookingmania'}
local CookingDelay = 5
--
function ENT:Initialize()
self:SetModel("models/props_c17/furnitureStove001a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
self:DrawShadow(true)
local phys = self:GetPhysicsObject()
if ( SERVER ) then
self:PhysicsInit( SOLID_VPHYSICS )
end
if phys:IsValid() then
phys:Wake()
end
self.IsCooking = false
self.FinishCookingTime = 0
self.bread = 0
self.egg = 0
self.flour = 0
self.milk = 0
end
function ENT:StartTouch(ent)
if table.HasValue(IngredientsForBread, ent:GetClass()) and self.IsCooking == false then
if ent:GetClass() == "egg_cookingmania" and self.egg < 2 then
ent:Remove()
self.egg = self.egg + 1
IsRecipeComplete()
elseif
ent:GetClass() == "flour_cookingmania" and self.flour < 1 then
ent:Remove()
self.flour = self.flour + 1
IsRecipeComplete()
elseif
ent:GetClass() == "milk_cookingmania" and self.milk < 1 then
ent:Remove()
self.milk = self.milk + 1
IsRecipeComplete()
end
end
end
function ENT:Think()
if self.IsCooking then
self:SetColor(Color(0, 255, 0))
if self.FinishCookingTime <= CurTime() then
self.IsCooking = false
print("I Am Here !")
local bread = ents.Create("bread_cookingmania")
bread:SetPos(self:GetPos() + Vector(0, 0, 25))
bread:Spawn()
end
else
self:SetColor(Color(255, 0, 0))
end
end
function IsRecipeComplete ()
if ENT.egg == 2 and self.flour == 1 and self.milk == 1 then
ENT.IsCooking = true
ENT.FinishCookingTime = CurTime() + 5
end
end
--[[ function ENT:Think()
if self.IsCooking == true then
self:SetColor(Color(255, 0, 0))
else
self:SetColor(Color(0, 255, 0))
end
if self.IsCooking == true then
if self.FinishCookingTime <= CurTime() then
self.IsCooking = false
local bread = ents.Create("bread_cookingmania")
bread:SetPos(self:GetPos() + Vector(0,0,50))
bread:Spawn()
end
end
end ]]--
-- function Cooking ()
-- end
Your pasting issue might come from you using tabs for indents instead of spaces in Sublime Text
The old code pasting was 10X better
-snip-
I dont think you can have an end before an else
Also like <CODE/BLUE> said in his video, the ENT:Think() for the colors isn't very good
Also for me, on your line 46 you have IsRecipeComplete, im guessing this is a bool in your shared file use
self:GetIsRecipeComplete()
and you also need to set
self:SetRecipeComplete()
After both ingredients are in.
When trying to see what ingredients touch the stove, I would do this: (An example from my money printer addon)
SHARED.LUA
function ENT:SetupDataTables()
self:NetworkVar("Int", 0, "Paper")
self:NetworkVar("Int", 1, "Money")
self:NetworkVar("Int", 2, "NextBeep")
self:NetworkVar("Int", 3, "NextPaper")
self:NetworkVar("Int", 4, "PrinterHealth")
self:NetworkVar("Bool", 0, "Activated")
end
Here's what I would set yours to:
function ENT:SetupDataTables()
self:NetworkVar("Int", 0, "Milk")
self:NetworkVar("Int", 1, "Flour")
self:NetworkVar("Int", 2, "Egg")
self:NetworkVar("Int", 3, "Bread")
self:NetworkVar("Int", 4, "StoveHealth")
self:NetworkVar("Bool", 0, "isCooking")
end
Now what you would do is replace
self.isCooking()
With
self:isCooking
(Change the . to a : ) I wanted to do : ) without a space but it did , i got mad, dent in my desk now
You can also remove all of the self.isCooking, self.bread, self.milk, etc
Im feeling nice so ill rewrite it for you to make it how you want it (i think)
HAS NOT BEEN TESTED MIGHT NOT WORK (BUT I HOPE IT WILL) CLICK THIS LINE OF TEXT
Danmit, I wrote all of that out and hes banned
Thank you very much that'really sweet of you, taking all this time for me.. I feel like I don't deserve it but really thank you once again I'll take all the time necessary to understand what you did. Much love.
No problem,best of luck to you and your cooking script
Sorry, you need to Log In to post a reply to this thread.