Game Development Community

XML Features

by Stanley D Chatman · in Torque Game Builder · 03/09/2008 (2:42 pm) · 10 replies

In the common/gameScripts/properties.cs file is a function called _loadGameConfigurationData that uses XML. I have searched the documentation but I cannot find any XML features listed. Where should look to find out the available XML methods?


function _loadGameConfigurationData( %projectFile )
{
   %xml = new ScriptObject() { class = "XML"; };
   if( %xml.beginRead( %projectFile ) )
   {
      if( %xml.readClassBegin( "TorqueGameConfiguration" ) )
      {
         $Game::CompanyName    = %xml.readField( "Company" );
         $Game::ProductName    = %xml.readField( "GameName" );

         $Game::Resolution     = %xml.readField( "Resolution" );
         $Game::FullScreen     = %xml.readField( "FullScreen" );

         $Game::CommonVersion  = %xml.readField( "CommonVer" );

         $Game::ConsoleBind    = %xml.readField( "ConsoleKey" );
         $Game::ScreenshotBind = %xml.readField( "ScreenShotKey" );
         $Game::FullscreenBind = %xml.readField( "FullscreenKey" );

         $Game::UsesNetwork    = %xml.readField( "UsesNetwork" );
         $Game::UsesAudio      = %xml.readField( "UsesAudio" );

         $Game::DefaultScene   = %xml.readField( "DefaultScene" );
         %xml.readClassEnd();
      }
      else
         _defaultGameConfiguration();
         
      %xml.endRead();
   }
   else
      _defaultGameConfigurationData();
   
   // Delete the object
   %xml.delete();
   
   // set the product and game names
   setCompanyAndProduct($Game::CompanyName, $Game::ProductName);
}

#1
03/09/2008 (3:15 pm)
XML.cs in the same folder as the properties.cs file.
#2
03/09/2008 (4:17 pm)
Thanks
#3
03/09/2008 (5:23 pm)
You may also find the following useful.... so far these were 'neccessary' extensions that I included in my current game. I simply just include this file and it gets added to the built in XML class.


//---------------------------------------------------------------------------------------------
// GE_XML_ADDONS.cs
// Copyright (C) Gellyware, Inc.
// Created: 20080203
// Last Modified: 20080203
/* Changes:
   


*/
//
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// getElementValue()
// returns xml value at the current internal xml index
//---------------------------------------------------------------------------------------------

function XML::getElementValue( %this )
{
   %value = "";
   if( %this.fileObject.pushChildElement( %this.index ) )
   {
      %value = %this.fileObject.elementValue();
      %this.fileObject.popElement();
   }

   return %value;
}


//---------------------------------------------------------------------------------------------
// getAttributeExists(%attribute)
// %attribute = attribute name to see if exists
// i.e. <xmlclass foo="bar"> text </xmlclass>
// getAttributeExists(foo); //returns 1
//---------------------------------------------------------------------------------------------

function XML::getAttributeExists(%this, %attribute)
{
   %value = "";
   if( %this.fileObject.pushChildElement( %this.index ) )
   {
      %value = %this.fileObject.attributeExists(%attribute);
      %this.fileObject.popElement();
   }

   return %value;   
}

//---------------------------------------------------------------------------------------------
// getAttributeName(%index)
// %index = 0..n 
// i.e. <xmlclass foo="bar"> text </xmlclass>
// getAttributeName(0); //returns "foo"
//---------------------------------------------------------------------------------------------

function XML::getAttributeName(%this, %index)
{
   %value = "";
   if( %this.fileObject.pushChildElement( %this.index ) )
   {
      %value = %this.fileObject.firstAttribute();
      
      if(%index > 0)
      {
         for(%i=0; %i<%index; %i++)
            %value = %this.fileObject.nextAttribute();
      }
      
      %this.fileObject.popElement();
   }
   return %value;   
}

//---------------------------------------------------------------------------------------------
// getAttributeValue(%attribute)
// %attribute = attribute name to get value for
// i.e. <xmlclass foo="bar"> text </xmlclass>
// getAttributeValue("foo"); //returns "bar"
//---------------------------------------------------------------------------------------------

function XML::getAttributeValue(%this, %attribute)
{
   %value = "";
   if( %this.fileObject.pushChildElement( %this.index ) )
   {
      %value = %this.fileObject.attribute(%attribute);
      %this.fileObject.popElement();
   }

   return %value;   
}

//---------------------------------------------------------------------------------------------
// readNextField()
// get next field value within an xml document
/* i.e.
   <xmlclass>
      <a>1</a>
      <b>2</b>
      <c>3</c>
   </xmlclass>
   
   if the current field is set to <b>
   readNextField() returns 2 (I believe, maybe need to verify)
*/
//---------------------------------------------------------------------------------------------

function XML::readNextField( %this)
{
   %value = "";
   if( %this.fileObject.pushChildElement( %this.index ) )
   {
      %value = %this.fileObject.getText();
      %this.fileObject.popElement();
   }
      
   return %value;
}

//---------------------------------------------------------------------------------------------
// nextIndex()
// sets the xml internal index to ++
//---------------------------------------------------------------------------------------------

function XML::nextIndex(%this)
{
   %this.index++;
}

//---------------------------------------------------------------------------------------------
// prevIndex()
// sets the xml internal index to --
//---------------------------------------------------------------------------------------------

function XML::prevIndex(%this)
{
   if(%this.index > 0)
      %this.index--;
   else
      %this.index = 0;
}
#4
03/09/2008 (6:31 pm)
What's up?

I was wondering what the point of XML is? What does it stand for? Is it any more beneficial than just using, say, $pref::Options::windowResolution?
#6
03/09/2008 (9:41 pm)
Anyone know why this doesn't create an actual xml file "common/test.xml" ??

function test2()
{
   echo("<<<<<<< test 2 begin <<<<<<<<<<<<<<<<");
   echo("<<<<<<< test 2 begin <<<<<<<<<<<<<<<<");
   
   %xml = new ScriptObject() { class = "XML"; };
   %projectFile = "common/test.xml";
   
   %xml.beginWrite( %projectFile );
      %xml.writeClassBegin( "testclass" );
         %xml.writeField( "value1", "1" );
         %xml.writeField( "value2", "2" );
         %xml.writeField( "value3", "3" );
      %xml.writeClassEnd();
   %xml.endWrite();     

   
   echo("<<<<<<< test 2 end <<<<<<<<<<<<<<<<");
   echo("<<<<<<< test 2 end <<<<<<<<<<<<<<<<");


   %xml.delete();
   
}
test2();
#7
03/09/2008 (9:57 pm)
When you write a file, it saves it into your AppData/roaming/... folder, not your game's directory. Read up on File Input/Output changes from 1.5+.
#8
03/10/2008 (1:20 am)
I see, thanks Phillip.


I've found a handful of files and folder in the %userprofile%/application data/ ... directories and I had no idea so many were being generated.

So how can the xml be controlled to save and load from the game directory? Or is that not possible now?
#9
03/10/2008 (1:38 am)
Because of the way Vista is architected writing to the game directory is not allowed.
Here is the TGB File IO Wiki that explains this tdn.garagegames.com/wiki/TGB/FileIO
#10
03/10/2008 (4:54 am)
Oh, is that why that changed?

I've successfully saved to the game directory using FileStreamObjects, but I am on XP.

Thanks Chip, that linked explained everything, lol, though I'm still not sure why TGB started using them -- just because they are "universal"?