Archive for the ‘Our OS Classes and snippets’ Category

XML driven object instancing and Array pushing

Friday, January 4th, 2008

I used similar XML  to make instances of class described in typing atributte of ogroup node.

<root>

 <ogroup typing=”ifs.ifs3D.Camera” arrayName=”camArray” >
  <ent camPos=”200,200,800? camTar=”0,0,0? camRot=”0,0,0? fov=”90? focus=”100? name=”maincam” />
 </ogroup>

</root>

 With small addaptation you can use following function (when XML data is already loaded and passed to function as input:XML).

output is refference to container object.
arrayName XML attribute telling to function in which array to push instanced object.

That means that your output object must have Array named as declared in XML attribute.

function loadObjectArrays(input:XML, output):void {

   var countArrays:Number;
   var countObjects:Number;
   var ogroups:Number;

   // creating array
   var tempArray:Array = new Array();

   // ogroups loop
   for each (var property:XML in input.ogroup) {

    var objectTyping:String = property.@typing;
    var arrayName:String = property.@arrayName;

    // if inserting successfull
    if (output.hasOwnProperty(arrayName)) {
    
     // instancing blank array
     output[arrayName] = new Array();
     
     // object loading loop
     for each (var entry:XML in property.ent) {

        var tmpClass:Object = getDefinitionByName(objectTyping);
        
        var tempObj:Object = new tmpClass();
        
        // checking if class has particular properties and if it has, applying values from XML
        if (tempObj.hasOwnProperty(”iParent”)) {
         tempObj.iParent = output;
        }
        if (tempObj.hasOwnProperty(”fromXMLentry”)) {
         tempObj.fromXMLentry(entry);
        }
        
        output[arrayName].push(tempObj);
      }
     }
     // object loading loop end

   }
}

hasOwnProperty is very usefull method. In my case, almost all of classes that I’m instancing have iParent property and fromXMLentry():void method (this method sets other object’s properties from XML node (<ent />), but not all. In case where I havn’t used hasOwnProperty():Boolean method (inherited from Object Class, that means that every class has it) I’ll have runtime error when class declared in typing=”" attribute is without mentioned property/method.


tesseractstudio.org | all rights reserved 2005-2008. | tesseractstudio ltd, Serbia
omnetwork.net | tutorialautomated.info | osgamer.org | freeflashscripts.com | tesslabs.com | thefirstexodus.com | thefirstexodus.com