DevilXD Posted March 16, 2014 Share Posted March 16, 2014 Is there any way to pause the script ex. for half a second? I need it for this loop:while energy <= 300 doenergy = energy - 300SpawnPrefab("goldnugget")Sleep(0.5)endCan someone help me ? Link to comment Share on other sites More sharing options...
seronis Posted March 16, 2014 Share Posted March 16, 2014 do a scheduled task. those work on a timer and there are tons of examples in the vanilla code Link to comment Share on other sites More sharing options...
Heavenfall Posted March 16, 2014 Share Posted March 16, 2014 It's called DoPeriodicTask() Link to comment Share on other sites More sharing options...
DevilXD Posted March 18, 2014 Author Share Posted March 18, 2014 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 More sharing options...
Heavenfall Posted March 18, 2014 Share Posted March 18, 2014 DoPeriodicTask lets you do a function every x seconds. It's not exactly sleep, but if you have a recurring check you want to run that's what you should use. Link to comment Share on other sites More sharing options...
squeek Posted March 18, 2014 Share Posted March 18, 2014 A true sleep wouldn't make any sense. It'd pause the entire game.DevilXD you want to do something like this: inst:DoPeriodicTask(0.5, function() if energy >= 300 then SpawnPrefab("goldnugget") energy = energy - 300 endend) Link to comment Share on other sites More sharing options...
simplex Posted March 18, 2014 Share Posted March 18, 2014 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 More sharing options...
squeek Posted March 19, 2014 Share Posted March 19, 2014 Halt, don't be so hasty!inst:StartThread(function() print "Hello" Sleep(5) print "world!"end)Isn't DoPeriodicTask basically a shortcut for that code you posted? Link to comment Share on other sites More sharing options...
simplex Posted March 19, 2014 Share Posted March 19, 2014 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 toinst: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 More sharing options...
DevilXD Posted March 19, 2014 Author Share Posted March 19, 2014 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 More sharing options...
iWitch Posted March 19, 2014 Share Posted March 19, 2014 yourlocal closeanimation = falselocal openanimation = trueinside function MakeGold will be reseted each time when function starts.so no point to change them at least at end of your functionyou must define this variables outside this function to save their valuebut they still will be reseted each time you login/logout into gameyou 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 areaabout your code, you dont need here 2 variable, just 1like 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 endendbut i am not sure will DoTaskInTime work inside DoPeriodicTasksqueek and simplex must know Link to comment Share on other sites More sharing options...
DevilXD Posted March 19, 2014 Author Share Posted March 19, 2014 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 More sharing options...
iWitch Posted March 19, 2014 Share Posted March 19, 2014 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 More sharing options...
DevilXD Posted March 19, 2014 Author Share Posted March 19, 2014 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 More sharing options...
DevilXD Posted March 19, 2014 Author Share Posted March 19, 2014 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 Link to comment Share on other sites More sharing options...
iWitch Posted March 19, 2014 Share Posted March 19, 2014 heh interesting using of lootdropper Link to comment Share on other sites More sharing options...
Recommended Posts
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.