Jump to content

Lua "sleep" function


Recommended Posts

But, when I use:

inst:DoTaskInTime( X , function()      SpawnPrefab("goldnugget")end)

..., it only delays a big wave of gold nuggets for X seconds...

 

Using DoPerodicTask() may work, I have to try that...

 

PS.: I've found "Sleep" commands  in "data / scripts / nis / maxwellintro.lua", but this doesn't give me much...

PS2.: I'm a Pole, sorry, if you can't understand me...

Link to comment
Share on other sites

A true sleep wouldn't make any sense. It'd pause the entire game.

Halt, don't be so hasty!

inst:StartThread(function()    print "Hello"    Sleep(5)    print "world!"end)
But I agree that constructions like the above are hardly the best choice since there's some added complexity to them (though sometimes their greater flexibility is useful, see for example prefabs/resurrectionstatue.lua). It is usually better to use DoTaskInTime or DoPeriodicTask.
Link to comment
Share on other sites

Isn't DoPeriodicTask basically a shortcut for that code you posted?

Aside from the first print statement, it'd be essentially what DoTaskInTime does (DoTaskInTime and DoPeriodicTask interface with the scheduler directly, but the scheduler itself does implement these constructs like above, except using a slightly more basic function that StartThread).

DoPeriodicTask would be equivalent to

inst:StartThread(function()    while true do        Sleep(5)        print "something"    endend)
(when the function given to StartThread ends, the thread ends; in particular it's somewhat simpler to cancel a task from within the task, since you can just return from the function)

A notable difference is that to cancel a thread from the outside you'd call KillThread on the return value of StartThread (KillThread is a global function, not a method), instead of using a Cancel method.

Link to comment
Share on other sites

Can I cancel DoPerodicTask() from inside of it? So, instead of:

local closeanimation = falseif  energy >= 300  then    inst.AnimState:PlayAnimation("open")     inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")    closeanimation = trueendwhile energy >= 300 do    energy = energy - 300    SpawnPrefab("goldnugget")endif closeanimation == true then    inst:DoTaskInTime(0.5, function()        inst.AnimState:PlayAnimation("close")         inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")    end)end

..., I can use this:

local function MakeGold()        local closeanimation = false    local openanimation = true        if ( energy >= 300 ) and ( openanimation == true ) then        inst.AnimState:PlayAnimation("open")         inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")        openanimation = false    end        if energy >= 300 then        energy = energy - 300        SpawnPrefab("goldnugget")        closeanimation = false    else        closeanimation = true    end        if closeanimation == true then        inst:DoTaskInTime(0.5, function()            inst.AnimState:PlayAnimation("close")             inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")        end)        openanimation = true    end    end

..., and this:

inst:DoPeriodicTask( 0.5 , MakeGold() )

... in the "prefab initialization" function, right ?  Will it work ?

 

PS.: I want to make chest, that drops gold like a fouintain, not like a single wave (original way) ...

Link to comment
Share on other sites

your

local closeanimation = falselocal openanimation = true
inside function MakeGold will be reseted each time when function starts.

so no point to change them at least at end of your function

you must define this variables outside this function to save their value

but they still will be reseted each time you login/logout into game

you spawning prefab, but doesnt teleport him to proper place, look into king pig code how he drops gold.

with your - gold will spawn at 0,0,0 in map. and its often invisible and unwalkable area

about your code, you dont need here 2 variable, just 1

like

 

local cheestIsOpen = falselocal function MakeGold()if energy >= 300 then        energy = energy - 300        if not chestIsOpen then              inst.AnimState:PlayAnimation("open")              inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")              chestIsOpen = true        end        i = SpawnPrefab("goldnugget")        if i then           --do teleport to proper place things        end    else        if chestIsOpen then            inst:DoTaskInTime(0.5, function()                  inst.AnimState:PlayAnimation("close")                  inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")                  chestIsOpen = false             end)         end    endend
but i am not sure will DoTaskInTime work inside DoPeriodicTask

squeek and simplex must know :-)

Link to comment
Share on other sites

Okay, so I changed the MakeGold() function, to have these variables outside of the function...

 

I'm calling this function from inside of the prefab file (chest), so it should have position... The previous code was working... I only want to change the way of the chest drops gold ( Fountain instead of single wave ) ...

Link to comment
Share on other sites

Okay, so I changed the MakeGold() function, to have these variables outside of the function...

 

I'm calling this function from inside of the prefab file (chest), so it should have position... The previous code was working... I only want to change the way of the chest drops gold ( Fountain instead of single wave ) ...

you creating new object, its havent any position by default

you old code works because you probably used DebugSpawn instead of SpawnPrefab. Or you was so lucky and your chest was on 0,0,0 :-)

Link to comment
Share on other sites

Oh, ok... I wasn't looking into code so much, and i missed that, a chest have a "lootdropper" component, and this:

 

inst.components.lootdropper:SpawnLootPrefab(itemname)

 

...is how it's handling gold drop... Code is quite messy...

 

Nvm, someone can tell me, if i can do a gold fountain the way i was writing above ?

Link to comment
Share on other sites

Ok, so I (FINALLY !!!) have managed to write this code properly. If someone want see it:

local function CreateItem(itemname)    inst.components.lootdropper:SpawnLootPrefab(itemname)endlocal ShouldOpen = falselocal ShouldClose = falselocal ChestIsOpen = falselocal function MakeGold()        if energy >= 300 then         ShouldOpen = true    else        ShouldClose = true    end        if ( ChestIsOpen == false ) and ( ShouldOpen == true ) then        inst.AnimState:PlayAnimation("open")         inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")        ShouldOpen = false        ChestIsOpen = true    else        ShouldOpen = false    end        if energy >= 300 then        energy = energy - 300        CreateItem("goldnugget")    else        ShouldClose = true    end        if ( ChestIsOpen == true ) and ( ShouldClose == true ) then        inst:DoTaskInTime(0.5, function()            inst.AnimState:PlayAnimation("close")             inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")        end)        ShouldClose = false        ChestIsOpen = false    else        ShouldClose = false    endend

... and task initialization:

inst:DoPeriodicTask(0.25, MakeGold)

I made it twice as faster, it was too slow :-)

 

Thanks for everyone, who helped me :wickerbottomthanks:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...