Scripting time in real-time seconds

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
DrKojak
Posts: 23
Joined: Fri Oct 24, 2014 11:19 am

Scripting time in real-time seconds

Post by DrKojak »

Is it possible to script triggers on a timer in real-time seconds?
Or is it possible to accurately measure/divide the timer asset's 'timerintervals' into real-time seconds? the timer assets in the editor seem to be about 0.3 to 0.4 of a second each and i can't successfully time a load of scripts to play out at near-precisely timed intervals (timed to music)

What script would i need to write to, for example.

start a separate script at 10 seconds after the party steps onto a floor trigger
start a separate script at 30 seconds after
start a separate script at 32 seconds after
start a separate script at 34 seconds after
start a separate script at 36 seconds after
start a separate script at 70 seconds after
etc and so forth

this is going to go on for 6-7 minutes of activating trap sequences and spawning in enemies in-case the party is too slow to defeat the boss quickly, who will be teleported in at 70 seconds.
User avatar
Vice Dellos
Posts: 47
Joined: Thu Feb 28, 2013 12:56 pm

Re: Scripting time in real-time seconds

Post by Vice Dellos »

connect a timer with triggerOnStart ticked and interval 1 (1 second) onto a script with something like this

Code: Select all

time = 0


function eventCaller()
	time = time + 1
	if time == 10 then
		print("you have been playing for 10 seconds")
	elseif time == 30 then
		print("you have been playing for 30 seconds")
	elseif time == 32 then
		print("you have been playing for 32 seconds")
	elseif time == 34 then
		print("you have been playing for 34 seconds")
	end
end

then enter what you want to happen in each elseifs (where it says print now)
DrKojak
Posts: 23
Joined: Fri Oct 24, 2014 11:19 am

Re: Scripting time in real-time seconds

Post by DrKojak »

Vice Dellos wrote:connect a timer with triggerOnStart ticked and interval 1 (1 second) onto a script with something like this

Code: Select all

time = 0


function eventCaller()
	time = time + 1
	if time == 10 then
		print("you have been playing for 10 seconds")
	elseif time == 30 then
		print("you have been playing for 30 seconds")
	elseif time == 32 then
		print("you have been playing for 32 seconds")
	elseif time == 34 then
		print("you have been playing for 34 seconds")

	end
end

then enter what you want to happen in each elseifs (where it says print now)
ah, thanks man! the timer seems to be rather accurate per second
going to be lots of work syncing traps, spawning, enabling/disabling torches and lights to this song but hopefully it will be awesome.
https://www.youtube.com/watch?v=KAUBTAhp3n8

only thing i can't seem to get right is i want to start the timer when a trigger is stepped on, however no matter how i set it up it starts when the game or test has started...
User avatar
Vice Dellos
Posts: 47
Joined: Thu Feb 28, 2013 12:56 pm

Re: Scripting time in real-time seconds

Post by Vice Dellos »

add a line to the script (before the function so it gets done on startup) that disables the timer

something like:

Code: Select all

timer_1.timer:stop()
works i believe (where "timer_1" is the ID of the timer you are using)
DrKojak
Posts: 23
Joined: Fri Oct 24, 2014 11:19 am

Re: Scripting time in real-time seconds

Post by DrKojak »

Vice Dellos wrote: something like:

Code: Select all

timer_1.timer:stop()
works i believe (where "timer_1" is the ID of the timer you are using)
thanks man, i got it working, added a floor trigger where the player spawns in to the area that stops the timer immediately in a script, then added a trigger that starts the timer and begins running the script you shared.

thanks man!

time to let my masochistic labor of love begin!


will probably upload a video of the test once it's completed
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Scripting time in real-time seconds

Post by antti »

Another technique you can use in LoG2 are delayedCalls. The syntax is delayedCall(scriptEntityId, delayInSeconds, functionName). Here's an example:

Code: Select all

function startSequence()
	print("Sequence started...")
	delayedCall(self.go.id, 1.5, "firstEvent")
	delayedCall(self.go.id, 10, "secondEvent")
end

function firstEvent()
	print("This script is run 1.5 seconds after the start.")
end

function secondEvent()
	print("This is printed 10 seconds after start.")
end
Steven Seagal of gaming industry
DrKojak
Posts: 23
Joined: Fri Oct 24, 2014 11:19 am

Re: Scripting time in real-time seconds

Post by DrKojak »

antti wrote:Another technique you can use in LoG2 are delayedCalls. The syntax is delayedCall(scriptEntityId, delayInSeconds, functionName). Here's an example:

Code: Select all

function startSequence()
	print("Sequence started...")
	delayedCall(self.go.id, 1.5, "firstEvent")
	delayedCall(self.go.id, 10, "secondEvent")
end

function firstEvent()
	print("This script is run 1.5 seconds after the start.")
end

function secondEvent()
	print("This is printed 10 seconds after start.")
end
this one may be even better to use. thanks guys!
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: Scripting time in real-time seconds

Post by Lark »

:shock: This is utterly FANTASTIC! I can quit spawning and destroying timers to accomplish such sequencing now that delayedCall is known. Just when I think it can't get any better... Thank you Antti!! Well, please excuse me now, I have to go rip out a bunch of timer code. Utterly fantastic game!! -Lark
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Scripting time in real-time seconds

Post by NutJob »

antti wrote:Another technique you can use in LoG2 are delayedCalls. The syntax is delayedCall(scriptEntityId, delayInSeconds, functionName). Here's an example:

Code: Select all

function startSequence()
	print("Sequence started...")
	delayedCall(self.go.id, 1.5, "firstEvent")
	delayedCall(self.go.id, 10, "secondEvent")
end

function firstEvent()
	print("This script is run 1.5 seconds after the start.")
end

function secondEvent()
	print("This is printed 10 seconds after start.")
end
You just resurrected a scripting idea. Please keep saying stuff! ~laughs~

Thanks!

Edit: between delayedCall, getOrdinal, and setWorldPosition I have to practically rewrite every script I've wrote to-date. Oh well, somehow it's fun for me.
User avatar
gambit37
Posts: 218
Joined: Fri Mar 02, 2012 3:40 pm

Re: Scripting time in real-time seconds

Post by gambit37 »

Can parameters be passed to functions in a delayedCall?

For example, if I use delayedCall in a loop and pass a parameter based on the value of the loop, I receive an error: "Warning! invalid message: script_entity_5.lightOn(lanterns_levEntrance([e]))"

My code is a loop to turn on all lanterns on the level sequentially with a small delay between each one. "lanterns_levEntrance" is a table already created which contains entity references for all the lanterns on the level. The code works fine if I enable the lights/particles directly (the lines commented out below), but fails with the delayed call.

Code: Select all

function lightsOn()
	for e = 1, #lanterns_levEntrance do
		--lanterns_levEntrance[e].light:enable()
		--lanterns_levEntrance[e].particle:enable()
		delayedCall(self.go.id, 0.2, "lightOn(lanterns_levEntrance[e])")
	end
end

function lightOn(lightID)
	lightID.light:enable()
	lightID.particle:enable()
end

Post Reply