This is an old revision of the document!


Add save and load option to triggers

Game Version: FS15 and later
GE Version: 6.x and later


Modding Index | Misc. Modding Tutorials


This tutorial will show one way how you can save and load values in your trigger This is an extension to this tutorial - http://fs-uk.com/forum/index.php?action=articles;sa=view;article=74

Skill level needed is more then basic.

This tut will show one way how you can save and load values in your trigger.

Skill level needed is more then basic.

The first thing we want to do is to add the trigger to the “gameSaveTable” and this we do like this

function triggerBase:load(id)
	g_currentMission:addOnCreateLoadedObjectToSave(self)
	return true;
end;

Next up is the removal from the “gameSaveTable” once the game is shutdown

function triggerBase:delete()
	g_currentMission:removeOnCreateLoadedObjectToSave(self)
end;

Okay so now we have “activated” the save and load function for this trigger and now we need one load function and one save function too.

We start with the save

function triggerBase:getSaveAttributesAndNodes()
	local attributes = 'example="' .. tostring(self.testBool) .. '"';
	local nodes = "";
	return attributes, nodes;
end;

In this example I want to save an bool value an thus I need to convert it to an string so that it can be stored if you do not do this for bool values then you will get an error in your log file when saving, you do not need to add tostring if you have an number based value. If you got more values that you want to save then simply keep building on the attributes line with an space between the last “ preferable, like this.

local attributes = 'example="' .. tostring(self.testBool) .. '" example2="' .. tostring(self.testBool) .. '"';

You can also save an complete table if you want to and that is were “nodes” comes in but I wont show that now but you basically loop your table and save all entrys.

That should cover something of the save function.

Now to load function

function triggerBase:loadFromAttributesAndNodes(xmlFile, tag)
	local testBool = getXMLBool(xmlFile, tag .. "#example");
	if testBool ~= nil then
		self.testBool = testBool;
	end;
	return true;
end;

Okay this is what we got when we want to load what we saved in the save function. Something that you must note when loading is what sort of result are we expecting? is it an string? an bool? check on this link for explanation on getXMLBool and other types that you can use

http://gdn.giants-software.com/documentation_scripting.php#runtime_function_reference_XML

Moving on.. local testBool now holds the result from example but we can never be sure if it has an value and that's why we got this

if testBool ~= nil then

it makes sure that we got an value and if so then it update self.testBool with that value.

Too make as much sense as possible then you will have to compare the save and load function and see what did he do there etc


Original Author: Xentro | Date: July 06, 2013

Modding Index | Misc. Modding Tutorials