Game Development Community

Objects inside Objects

by Chris "Hyena" Vogel · in Torque Game Builder · 08/13/2009 (10:25 pm) · 3 replies

I've read through a hundred forum posts and tutorials and docs on OOP in Torque and Simsets and everything else and what I'm trying to do I think should be easy, but I can't get it right! Hopefully someone can help me out.

Before I go any further let me say that most of my objects are just in script, they are objects for organization only, not showing up on the screen or interacting with physics or anything like that.

For my game design, I want to have a global GameData object that will hold all the game's player data.

under Gamedata I want to have a Party object as well as a few other member variables.

The Party Object I want to have 4 Player objects and some other member variables as well.

The Player Objects will have a bunch of member variables defining them, as well as a few objects.


To do this, I think that I want to use SimSets for all the container objects, and ScriptObjects for all the objects that won't hold other objects. Does this sound right?

How do I make that work in Script? I want my game and player data accesible through any scene, and I need to know how to set it up and access it.

Here is what I tried that didn't work.

function GameConfig()
{
	$GameData = new Simset();
	Party = new Simset();
	Data = new ScriptObject();
	$GameData.add(Party);
	Party.Add(Data);
	Party.Data.gold = 100;
}

This gives me an error on the Data = new ScriptObject line. It also seems that the Party.Data.gold line isn't going to work.. If I have a layout such as this.. How do I access (or add) a member variable of the ScriptObject Data which is inside the SimObject Party which is inside the SimObject GameData?

If you can help me get this all straight in my head I would be very appreciative.


#1
08/14/2009 (9:07 am)
Both Party and Data need % prepended to them (making them %party and %data, respectively), because otherwise they're not recognized as the variables that they are. So, your code should look like:

function GameConfig()
{
    $GameData = new Simset();
    %Party = new Simset();
    %Data = new ScriptObject();
    $GameData.add(%Party);
    %Party.Add(%Data);
    %Party.Data.gold = 100;
}

You should use % for locally accessible variables, and $ for global variables.
#2
08/14/2009 (12:37 pm)
ok Thanks Ted, somehow I missed that, think I was programming too late at night again.

One question... If I make $GameData global, and make Party and Data local. and put party and Data inside Gamedate. then close a scene and create a new one, switch to new functions and all that... I assume Gamedata as global is still accessible, but what about the others?
#3
08/14/2009 (12:54 pm)
Ted, your code as listed doesn't quite work, but it helped me clear up some problems at least.

I don't think I need to use Simsets. The objects I'm working with don't need to be grouped like that.

$GameData = new ScriptObject();
	$GameData.Party = new ScriptObject();
	$GameData.Party.gold = 100;

That's all I was trying to do. I just wasn't getting the format right. I think I got it now. Thanks for the help all.