Can You Delay Inside of a Script?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Can You Delay Inside of a Script?

Post by Lark »

I have some sequences that I'd like to start, and the "easiest" way would be to trigger a script, do an effect, sleep for .5 seconds (or so), do another effect, and etcetera. But I don't see a sleep(), delay(), or wait() function in lua. Can this be done? When I trigger the following script, for example, I get two firebursts at random locations, but at the same time:

Code: Select all

function twoFirebursts()
  spawn("fireburst", self.level, self.x + math.random(-1, 1), self.y + math.random(-1, 1), 0)
  --sleep(.5)   --I would like to insert a .5 second delay here...
  spawn("fireburst", self.level, self.x + math.random(-1, 1), self.y + math.random(-1, 1), 0)
end
Or do I have to do something like this? Change the code to spawn one fireburst...

Code: Select all

function oneFireburst()
  spawn("fireburst", self.level, self.x + math.random(-1, 1), self.y + math.random(-1, 1), 0)
  timer_fireburst_1:deactivate()
  end
...and then trigger for the script would be modified to activate the script and a timer that was deactivated and set to .5 seconds. The timer in turn activates the script again. This works fine for this example, but this is just an example and not what I really want. I've searched the forums for such a feature, but I can't find one. All help is greatly appreciated! Thank you, -Lark
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: Can You Delay Inside of a Script?

Post by Lilltiger »

Here is a example of what you want :)
Just paste it into a lua script in the editor!

Code: Select all

-- The sript creates a timer and place it where the script is,
-- and connect it to the script.

t = spawn("timer", self.level, self.x,self.y,0, "timer"..self.id)
t:setTimerInterval(0.5)
t:addConnector("activate", self.id, "doFireburst")

-- The number of firebursts is stored here.
counter = 0;

function firebursts(number_of_fireburst)
	-- Increase the counter with the added number of bursts.
	counter = counter + number_of_fireburst

	-- Dont restart it if it's already started.
	if( t:isActivated() ~= true ) then
		t:activate()
	end
end

function doFireburst()
	-- If the counter is above 0, spawn a burst
	if( counter > 0 ) then
		spawn("fireburst", self.level, self.x + math.random(-1, 1), self.y + math.random(-1, 1), 0)
		-- Decrease the counter.
		counter = counter - 1
		t:activate()
	else
		t:deactivate()
	end
end

-- Now you can just call the function to spawn the number of bursts you want.
firebursts(5)
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Can You Delay Inside of a Script?

Post by Komag »

in an nutshell, no, there is no direct time delay ability, you have to use timers one way or another
Finished Dungeons - complete mods to play
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: Can You Delay Inside of a Script?

Post by Lark »

Thank you for the example and thank you for verifying that there is no way to sleep in a script. It sure would be nice and simplify some code, but at least everything is still possible with timers. I like the example in that it is self-contained making it more portable once written. I really appreciate the help!

Thanks everyone, -Lark
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: Can You Delay Inside of a Script?

Post by Lilltiger »

np, imo all code should be written as generic and self containing as possible, so it's easy to reuse.
Post Reply