Stateless loading
NAME: onLoadXML ( sName )
TYPE: Handler
DESCRIPTION: A handler that will load any variables you want from an XML file you specify. For example purposes I am using the previous save functions saved variables for loading. You must know what values you are loading from the file (like variable names). We will be reading files that are saved to the users documents directory.
INPUT: sName -> The name of the file you want to load in XML format
OUTPUT: none (It will load up the variables)
REQUIREMENTS: You need to have an XML variable in your main AI called hXML (Variable Type: XML, Init Value: Empty)
Code
--------------------------------------------------------------------------------
-- Handler.......... : onLoadXML
-- Author........... : Game Scorpion Inc.
-- Description...... : Version 1.0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function intro_ai.onLoadXML ( sName )
--------------------------------------------------------------------------------
-- Try and obtain the file sName and place the root node in this.hXML ( )
xml.receive ( this.hXML ( ), "file://"..system.getDocumentsDirectory ( ).."/"..sName )
-- Get the loading status -> 1 = SUCCESS, anything LESS THAN 0 is a FAILURE
local nStatus = xml.getReceiveStatus ( this.hXML ( ) )
-- Check status to see if we have a SUCCESSFUL LOAD
if ( nStatus == 1 ) then
-- Get the Root node of XML
local hRoot = xml.getRootElement ( this.hXML ( ) )
-- ===============================
-- BEGIN LOADING OF VARIABLES HERE
-- ===============================
-- We will load the examples from our save function.
-- MAKE EDITS AND CHANGES TO anything below to suit the variables you wish to load
-- Get a NUMBER value -> string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "NAME_OF_VARIABLE_FROM_XML_FILE" ) ) )
application.setCurrentUserEnvironmentVariable ( "nTotal", string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "nTotal" ) ) ) )
application.setCurrentUserEnvironmentVariable ( "nDT", string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "nDT" ) ) ) )
-- Boolean Values require an IF/THEN check to convert to true/false
if xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "bDead" ) ) == "true" then
application.setCurrentUserEnvironmentVariable ( "bDead", true )
else
application.setCurrentUserEnvironmentVariable ( "bDead", false )
end
if xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "bAlive" ) ) == "true" then
application.setCurrentUserEnvironmentVariable ( "bAlive", true )
else
application.setCurrentUserEnvironmentVariable ( "bAlive", false )
end
if xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "bActive" ) ) == "true" then
this.bActive ( true )
else
this.bActive ( false )
end
if xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "bButton0Down" ) ) == "true" then
this.bButton0Down ( true )
else
this.bButton0Down ( false )
end
application.setCurrentUserEnvironmentVariable ( "nPlayerPositionX", string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "nPlayerPositionX" ) ) ) )
application.setCurrentUserEnvironmentVariable ( "nPlayerPositionY", string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "nPlayerPositionY" ) ) ) )
application.setCurrentUserEnvironmentVariable ( "nPlayerPositionZ", string.toNumber ( xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "nPlayerPositionZ" ) ) ) )
-- Text Values do not require any conversion and can be read by using -> xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "sNameOfXMLField" ) )
application.setCurrentUserEnvironmentVariable ( "sTest", xml.getAttributeValue ( xml.getElementAttributeWithName ( hRoot, "sTest" ) ) )
-- WE HAVE SUCCESSFULLY LOADED ALL VALUES FROM THE SAVE HANDLER EXAMPLE
-- CHANGE/EDIT ANY OF THE ITEMS ABOVE AND LOAD UP YOUR OWN VARIABLES
-- ========================
-- END LOADING OF VARIABLES
-- ========================
else
if ( nStatus < 0 ) then
log.message ( "LOAD FAILED, FILE DOES NOT EXIST OR FILE ERROR" )
end
end
-- HOW TO CALL HANDLER:
-- user.sendEvent ( application.getCurrentUser ( ), "AI_MODEL_NAME", "onLoadXML", "YOUR_FILE_NAME.xml" )
-- OUTPUTTED FILE CONTENTS WILL INCLUDE ALL THE ABOVE ITEMS
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------