PDA

View Full Version : Associative Array nesting?



sami
12-09-2012, 11:52 PM
I've been using arrays as elements of arrays before, but can I have an associative array as an element of another associative array? So nesting them (hopefully a few levels deep?) kind of a nice way (if possible) for object storage etc. But when I try it as below - I crash Modeler. Any thoughts?



var mainThing;
var subThing;

main
{
subThing = $ "mapName", "GunnerMap", "numUVs", 3 $;

mainThing = $ "name", "myArrayThing", "numCharacters", 7, "numPoints", 42343, "percent", 144.34524, "sub" , subThing $;

info(" Answer = numPoints =" + mainThing["numPoints"] + ", percent = " +mainThing["percent"] + ", subName = " + mainThing["sub"]["mapName"]);
}

As usual, Mike Green's site has great info, but he doesn't quite touch on this on this page (http://www.pixsim.co.uk/mike_green_lscript_reference/Style_Test.html).

sami
12-12-2012, 11:04 AM
I ended up giving up on nesting associative arrays for now, but still would like to hear from anyone who has successfully done it. Seems like associative arrays are just a one level deep hashmap with name key value pairs - heck I couldn't even get an element of an associative array to contain an array without crashing or giving errors as if that element with the array assigned to it was screwy!

However, I tried a different approach using arrays of nested arrays and sequences (enumerations) to simulate properties, so coding-wise it feels like a structure or a bit OOPish if I name my functions nicely as well. I can get several layers deep and have "collections" of objects stored in array properties as well and that let's me feel like I'm encapsulating things in a logical way. I do it kind of like this: (if anyone is interested) (you may need to scroll the code block)



// "property" definitions of FunkyCar
@sequence {
FC_CAR_ID,
FC_CAR_NAME,
FC_MESH,
FC_WHEELS,
FC_POLY_COUNT,
FC_COLOR_EXTERIOR
}

// "properties" for the FunkyWheel object
@sequence {
FW_WHEEL_ID,
FW_TREAD_WIDTH,
FW_HUBCAP_TEXTURE_NAME,
FW_WHEEL_DIAMETER
}

var myFunkyCar; // as FunkyCar

main
{
myFunkyCar = FunkyCar_create(1, "SuperCar1", Mesh(0), 4, <255, 0,0>);


// maybe make rear wheels wider ...
myFunkyCar[FC_WHEELS][3][FW_TREAD_WIDTH] = myFunkyCar[FC_WHEELS][1][FW_TREAD_WIDTH] * 1.5;

myFunkyCar[FC_WHEELS][4][FW_TREAD_WIDTH] = myFunkyCar[FC_WHEELS][1][FW_TREAD_WIDTH] * 1.5

}

FunkyCar_create: whatId, whatName, whatMesh, numWheels, whatColor
{
// "constructor" of the FunkyCar

var tempCar;

tempCar[FC_CAR_ID] = whatId;
tempCar[FC_CAR_NAME] = whatName;
tempCar[FC_MESH] = whatMesh;
tempCar[FC_POLY_COUNT] = whatMesh.polygons.size();
tempCar[FC_WHEELS] = FunkyCar_loadWheels(tempCar, numWheels);
tempCar[FC_COLOR_EXTERIOR] = whatColor;

return(tempCar);
}

FunkyWheel_create: whatId, whatTreadWidth, whatDiameter, whatHubcapTextureName
{
var tempWheel;
tempWheel[FW_WHEEL_ID] = whatId;
tempWheel[FW_TREAD_WIDTH] = whatTreadWidth;
tempWheel[FW_WHEEL_DIAMETER = whatDiameter;
tempWheel[FW_HUBCAP_TEXTURE_NAME] = "";
return(tempWheel);
}

// for "method" names I have to pass in the actual object since LScript doesn't have any automatic way of getting a "this" object
FunkyCar_loadWheels: whatFunkyCar, numWheels
{
var tempWheels = nil;
var i;
for(i=1;i<numWheels+1,i++)
{
var myTempWheel;
// in this example set the hubcap texture to empty and later create a loader method for that, etc
myTempWheel = FunkyWheel_create(i, DEFAULT_TREAD_WIDTH,*DEFAULT_WHEEL_DIAMETER, "");
tempWheels[i] = myTempWheel;
}
return(tempWheels);
}


You get the idea. This helps me make cleaner code and can nest way deeper than my basic example here. It helps you store your data logically in Lscript at least a bit more than independent variables and arrays (even though that's what this is essentially) Thoughts or other approaches or a fix to the nesting of associative arrays are welcome! :)

evenflcw
12-14-2012, 03:53 PM
Yes. I believe this is a good way of doing it when you need to organize and handle alot of data. Using (initialized) arrays, indexed using constants/sequences, and functions to manipulate everything for consistent safe testable handling of the data structures. It's a good example of advanced lscripting. Cool.

sami
12-14-2012, 04:46 PM
Thanks! Was just wondering if me getting to this was ideal for Lscript or if there was something I was missing with associative arrays or another approach. I'll stick with this approach then since it seems to be working fine even with lots of data filled into my "objects".