Game Development Community

Switch Missions via Script

by Phoenix Online Studios · in Torque Game Engine · 08/16/2005 (11:21 am) · 8 replies

I'm trying to switch missions in-game via TorqueScript, but for some reason this function only works when I type it in the console. When I try to call it in script, the engine crashes. I am not destroying or recreating the server connection.

To call this, I would have: switchMissions("mission.mis");

// Input: %misFile = *.mis file (i.e. "stronghold.mis");

function switchMissions(%misFile)
{
	%curMission = $Server::MissionFile;

	%misPath = "<mod>/data/missions/" @ %misFile;
	
	//dissallow mission switching before server is created
	if (%curMission $= "") {
		echo("Error: You haven't loaded a mission yet.");
		return false;
	}
	//trying to switch to the mission you're already in??
	else if (%curMission $= %misPath) {
		echo("Error: cannot switch to a mission you are already in: " @ %misFile);
		return false;
	}

	//check if mission file exists
	%file = new FileObject();
	if (!%file.openForRead(%misPath)) {
		%file.delete();

		echo("Error: Mission file does not exist: " @ %misPath);
		return false;
	} 
	//all is good, let's switch!
	else {
		%file.delete();

		//exit current mission
		exitMission();

		//initialize next mission
		initMission(%misPath);
		
		echo("Mission switch successful!  You are now in: " @ %misPath);
		return true;
	}
}

function exitMission() {

	// Clear misc script stuff
	HudMessageVector.clear();


	// Terminate all playing sounds
	alxStopAll();
	if (isObject(MusicPlayer))
		MusicPlayer.stop();


	//Torque FPS Crap
	LagIcon.setVisible(false);
	PlayerListGui.clear();
      	clientCmdclearBottomPrint();
	clientCmdClearCenterPrint();


	//Show Loading screen
	Canvas.setContent(LoadingGui);


	// Dump anything we're not using
	clearTextureHolds();
	purgeResources();
}

function initMission(%misPath) {

	%mission = %misPath;

	// used in game.cs to load the appropriate player script
	$playerCSFile = $playerFiles[$currentDTS];

	loadMission(%mission, false);
}


Does anyone know why it crashes the engine when called in script, but it works fine when typing it in the console?

#1
08/16/2005 (12:09 pm)
One possibility is client vs server. I know you're probably only testing this in a single process, but Torque still has a client/server relationship between the client and the server even in single process games (i.e. single player). Even if you're not making a multi-player game, it's still important to respect the client/server distinction.

Where is the script located that's switching the mission? Is it in the GUI script or something that's called from the GUI script, or is it in a script that's located in the server directory?

Although it might work sometimes, you should never cross the client/server script boundary... everything that resides in the server script directory should not call anything that resides in the client directory and vice versa. If you need to do this, look at how commandToServer and commandToClient work.

Just a thought, but I think that might be what's causing your problem.
#2
08/16/2005 (12:50 pm)
You're correct that our game is a single-player game with both the server and client residing on the same computer.

The script that contains the above code is located in "/server/scripts/switchMissions.cs". It is being exec()'d in "/server/init.cs".

Do I need to use commandToServer when the script calling switchMissions() is already on the server side?
#3
08/16/2005 (1:33 pm)
You might consider using the code /code tags (add square brackets). Makes code MUCH easier to read.

Scripts don't run in explicitly server or client space. It's just that some things aren't going to exist when you're running in a dedicated client or dedicated server configuration, and if you try to use them in that case things will break.

Do you get a particular crash (ie, in a particular subroutine or area of the engine)? Understanding the nature of the crash will point you to finding the right way to implement the desired feature.
#4
08/16/2005 (1:44 pm)
If you are calling switchMission("mission.mis"); from within a script... I would recommend that you try to use a schedule call instead.

schedule(0,0,switchMission,"mission.mis");
#5
08/16/2005 (1:48 pm)
Sorry, I've edited the first post to use code tags now. :)

The engine gets extremely slow and then the crash is just like any other WinXP crash and says:

".exe has encountered a problem and needs to close. We are sorry for the inconvenience.

If you were in the middle of something, the information you were working on might be lost.

Please tell Microsoft about this problem.
etc"
Debug Send Error Report Don't Send


Here's what the console.log says:

*** ENDING MISSION
*** LOADING MISSION: /data/missions/IoC_Island.mis
*** Stage 1 load
*** Stage 2 load
Executing /data/missions/IoC_Island.mis.
*** Mission loaded
*** Sending mission load to client: /data/missions/IoC_Island.mis
Mission switch successful! You are now in: /data/missions/IoC_Island.mis


But, the mission does not end and the new mission does not load, despite what the console.log says.
#6
08/16/2005 (1:53 pm)
Whoa! Thanks Harold! :)

Once I scheduled the function call it worked! :D
#7
08/17/2005 (10:02 am)
TGE is single threaded... using the schedule forces Torque to wait until all other commands are finished and the engine isn't in the middle of any functions before calling the switchMission function.
#8
04/21/2009 (12:41 pm)
Espero poder aplicar el script, muchas gracias