murgh wrote:You should check if the object.JobId==nil before issuing a CreateJob for the object. That way it will issue the job only once.
Uhm... It doesn't work if I want to create two different jobs.
Let's assume I already created a Job_A that still have to be completed, if I met the condition to create Job_B, then object.JobId will not be nil and Job_B is skipped.
I made a Water Dispenser who needs water and paper cups to be refilled, this is my solution (for now):
Code: Select all
function Update(dt)
-- check for water
if this.waterLeft <= 0 then
-- fill dispenser
if this.refillingWater then
else
this.refillingWater = true
this.CreateJob("FillDispenserWater")
end
end
-- check for cups
if this.Slot1.i < 0 or this.cupsLeft == 0 then
-- fill dispenser
this.cupsLeft = 0
if theCups then
theCups.Delete()
end
theCups = nil
if this.refillingCups then
else
this.refillingCups = true
this.CreateJob("FillDispenserCups")
end
else
if theCups then
else
theCups = getObject("PaperCup",1,this.Slot1.i)
end
theCups.SubType = math.ceil(4*this.cupsLeft/maxCups)
theCups.Tooltip = "Contents Paper Cup x " .. this.cupsLeft
end
......
Code: Select all
function JobComplete_FillDispenserWater()
this.waterLeft = maxWater
EmptyBottle = Object.Spawn("WaterBottleEmpty",this.Pos.x,this.Pos.y)
if this.Or.x == -1 or this.Or.x == 1 then
EmptyBottle.ApplyVelocity(this.Or.x*3*randpx(),6*randpx() - 3,true)
else
EmptyBottle.ApplyVelocity(6*randpx() - 3,this.Or.y*3*randpx(),true)
end
this.refillingWater = false
end
function JobComplete_FillDispenserCups()
theCups = Object.Spawn("PaperCup",this.Pos.x,this.Pos.y)
theCups.Loaded = true
theCups.CarrierId.i = this.Id.i
theCups.CarrierId.u = this.Id.u
this.Slot1.i = theCups.Id.i
this.Slot1.u = theCups.Id.u
this.cupsLeft = maxCups
this.refillingCups = false
end