Load sequence

For AI and campaign script related discussions and questions
Post Reply
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Load sequence

Post by aubergine » 07 Feb 2013, 10:21

When a game is loaded, is this the sequence of events...?

1. Create environment
2. Define JS API native features on the global object
3. Eval the main script (which in turn will load any includes)
4. Restore saved globals / triggers / groups
5. Trigger eventGameLoaded()

I tried reading the C++ in qtscript.cpp but it melted my brain.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO

Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3755
Joined: 03 Aug 2006, 19:39

Re: Load sequence

Post by Per » 07 Feb 2013, 10:59

It is not easy to follow, I know, but the above interpretation seems correct.

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Load sequence

Post by aubergine » 07 Feb 2013, 13:18

After the load sequence completes, will the next check for triggers (eg. queued function calls) be at gameTime of the save point, or gameTime+1 or gameTime+100?

I'm trying to backport eventGameLoaded() to Warzone 3.1.

I've got an accessor property, the value of which will be stored in save games. When the game loads, the value is restored triggering the setter function of the accessor property. In that setter function, I want to queue a call to eventGameLoaded() in such a way that it will happen immediately after the load cycle completes (I assume events are disabled during that process) and ideally before any other saved triggers start kicking in.

So, I'm wondering if queue("eventGameLoaded", 0) will do the trick in my setter function, or whether there would be some better way to do it?

Another approach would be to queue("eventGameLoaded", 0) as soon as my script instantiates, then removeTimer("eventGameLoaded") if eventGameInit() gets called.

Both approaches rely on a queue() using 0 delay. However, I noticed in qtscript.cpp this comment:

Code: Select all

struct timerNode
....
// implement operator less TODO
Which makes me worry that if I specify 0 ms in my queue() call, that my queued call won't trigger if the timer system kicks in after the restored gameTime has changed (although I believe it only increments in blocks of 100ms so that might not be a problem?).
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Load sequence

Post by aubergine » 07 Feb 2013, 13:35

BTW, here is code I have so far:

Code: Select all

	global.addAccessor(
		"__",
		function saving() {
			// make sure this isn't a script poking around
			if (!arguments.callee.caller) {
				Native.queue("eventGameSaved", 0); // call eventGameSaved() after save cycle
				eventGameSaving(); // call eventGameSaving() now
			}
			return now;
		),
		function loading(val) {
			if (val < now) {
				// clear saved eventGameSaved trigger
				Native.removeTimer("eventGameSaved");
				// make sure eventGameLoaded is first event triggered after load cycle completes
				Native.queue("eventGameLoaded", 0);
			}
		}
	);
Where:
* global = pointer to global object (like engine->globalObject() in C++)
* Native === global
* .addAccessor() adds a hidden, non-configurable getter/setter property to an object
* now is a global accessor property that returns (new Date()).getTime()
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO

Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3755
Joined: 03 Aug 2006, 19:39

Re: Load sequence

Post by Per » 07 Feb 2013, 15:13

I think script makers should not make assumptions about the order of events, or exactly in which frame events are fired, since these things may change. I'm not sure what will happen in your scenario above, nor am I sure if 3.1 and master will behave identically.

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Load sequence

Post by aubergine » 07 Feb 2013, 15:20

That code is wrapped in a check so that it only gets invoked on Warzone 3.1 branch (no need for it on 3.2 as the events are natively supported). It's basically a backport of the WZ 3.2 save/load events, so that I can use WZ 3.2 features on 3.1 code, which in turn is so I can get NullBot 3 running on WZ 3.1.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO

Post Reply