XML saving – ShiVa Engine

XML saving

NAME: onSaveXML ( sName )
TYPE: Handler
DESCRIPTION: A handler that will save any variables you want to an XML file you specify. For example purposes I have included various variable types being saved. Please keep in mind that XML requires output to be in STRING format so everything MUST be converted properly on SAVE. The next function I will write after will be for loading and it will require us to convert the strings back to regular variables. Files are saved to the users documents directory.
INPUT: sName -> The name of the file you want to save in XML format
OUTPUT: none (It saves a file to the documents folder)
REQUIREMENTS: You need to have an XML variable in your main AI called hXML (Variable Type: XML, Init Value: Empty)

--------------------------------------------------------------------------------
--  Handler.......... : onSaveXML
--  Author........... : Game Scorpion Inc.
--  Description...... : Version 1.0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function intro_ai.onSaveXML ( sName )
--------------------------------------------------------------------------------
    -- Create a local variable that holds the current XML variable
    local hXMLVar = this.hXML ( )
    -- Make sure the current XML variable is empty
    xml.empty ( hXMLVar )
    -- Create another variable called hRoot to store the root element
    local hRoot = xml.getRootElement ( hXMLVar )
    -- ======================
    -- BEGIN SAVING VARIABLES
    -- ======================
    -- REPLACE BELOW VARIABLES WITH WHATEVER VARAIBLES YOU WISH TO SAVE.  FOR EXAMPLE PURPOSES WE HAVE PROVIDED 10 SAMPLES.
    -- FORMAT IS xml.appendElementAttribute ( hRoot, "VAR_NAME", string.format ( "%s", ACTUAL_VARIABLE and ACTUAL_VARIABLE or "ZERO_VALUE_OR_EMPTY_STRING" ) )
    -- Example of ENVIRONMENT VARIABLES
    xml.appendElementAttribute ( hRoot, "nTotal", string.format ( "%s", application.getCurrentUserEnvironmentVariable ( "nTotal" ) and application.getCurrentUserEnvironmentVariable ( "nTotal" ) or "0" ) )
    xml.appendElementAttribute ( hRoot, "nDT", string.format ( "%s", application.getCurrentUserEnvironmentVariable ( "nDT" ) and application.getCurrentUserEnvironmentVariable ( "nDT" ) or "0" ) )
    -- Example of BOOLEAN VARIABLES
    xml.appendElementAttribute ( hRoot, "bDead", string.format ( "%s", application.getCurrentUserEnvironmentVariable ( "bDead" ) and "true" or "false" ) )
    xml.appendElementAttribute ( hRoot, "bAlive", string.format ( "%s", application.getCurrentUserEnvironmentVariable ( "bAlive" ) and "true" or "false" ) )
    -- Example of AI BOOLEAN VARIABLES
    xml.appendElementAttribute ( hRoot, "bActive", string.format ( "%s", this.bActive ( ) and "true" or "false" ) )
    xml.appendElementAttribute ( hRoot, "bButton0Down", string.format ( "%s", this.bButton0Down ( ) and "true" or "false" ) )
    -- Example of PLAYER POSITION
    local nX, nY, nZ = object.getBoundingSphereCenter ( application.getCurrentUserSceneTaggedObject ( "obj_player" ) )
    -- Example of NUMERIC VARIABLES
    xml.appendElementAttribute ( hRoot, "nPlayerPositionX", string.format ( "%s", nX and nX or "0" ) )
    xml.appendElementAttribute ( hRoot, "nPlayerPositionY", string.format ( "%s", nY and nY or "0" ) )
    xml.appendElementAttribute ( hRoot, "nPlayerPositionZ", string.format ( "%s", nZ and nZ or "0" ) )
    -- Example of STRING VARIABLES
    xml.appendElementAttribute ( hRoot, "sTest", string.format ( "%s", "HELLO" and "HELLO" or "" ) )
    -- ====================
    -- END SAVING VARIABLES
    -- ====================
    -- Do save file
    xml.send ( hXMLVar, "file://"..system.getDocumentsDirectory ( ).."/"..sName )
    -- HOW TO CALL HANDLER:
    -- user.sendEvent ( application.getCurrentUser ( ), "AI_MODEL_NAME", "onSaveXML", "YOUR_FILE_NAME.xml" )
   -- OUTPUTTED FILE CONTENTS WILL INCLUDE ALL THE ABOVE ITEMS
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------



Need more answers?

  • slackBanner