Handling 'data' in games

Hey guys, so I am very confident in making a game with gameobjects that do things.

I can make, say, a game of asteroids easy! with the player ship, other asteroids, and bullets all interacting. But where I have a problem is games which rely more heavily on data. For example, strategy games. Where does it all fit?

Maybe a more concrete example is needed. Imagine a game where you develop towns and each town has an income X that depends on the buildings built in the town. Imagine these towns as cities in Civilization. They are not the primary focus of the game, yet they need to keep track of data like income that contributes to the overall game. Would each building in the city have a script attached called ‘Generate Gold’, and the ‘city’ script would query all its children buildings to see how much gold is generated? there is this annoying separation between DATA and VISUALS that i really can not get my head around.

Anyway, an insight into all this would be great.

“the issue then, is, how do i associate:…”

I hate dragging things in to the editor, it seems cheap. I often sit one “model” whatever it is, under the master game object. I just get the model and duplicate it, and customize it for each one. In this trivial code below, there’s an array with names like “Spaceship” “Lasercannon”. all the sprite names happen to be pSpaxeship, pLasercannon and so on.

The code in the For Loop carefully builds all the 10 or so buttons. Now, the whole ‘model button’ system, is self-contained and has everything it needs inside it. Note that it’s calling setup functions and so on in the individual buttons.

So once again, a common approach. You might have “City” mega object. in there you will have ONE “building”. Name it ‘StarterBuilding’ or ‘Model Building’. In itself, that could be a huge system with many subcomponents and scripts. In your city script, find the “StarterBuilding” below you, and instantiate 200 buildings, or however many. For each one, do all necessary specific startup.

You can freely work on ‘Model Building’ whenever you need to do so.

So that’s perhaps part of what you’re asking. I like to have a “model” building, button, or whatever it is. the thing over it, takes the one named “model” and makes many of them. Note the very handy “FindUnderHereNamed” fragment.

#pragma strict
import System.Collections.Generic;

private var myProjectiles:List.<bouncyButton>;
private var kPL:int;

function _buildButtonsAtLaunchTime()
 {
 myProjectiles = new List.<bouncyButton>();
 
 // NOTE
 var modelButton:GameObject
    = FindUnderHereNamed("modelButton").gameObject;
 // NOTE
 modelButton.transform.localPosition = Vector3(0, 3, 0);
 
 var widthModel:float = modelButton.collider.bounds.size.x;
 var heightModel:float = modelButton.collider.bounds.size.y;
 
 var widthGap:float = widthModel * 0.100;
 
 kPL = candc.projectileNames.length;
 for ( var i:int=0; i<=kPL; ++i )
 {
 var newy:GameObject = Instantiate( modelButton );
 newy.transform.parent = transform;
 newy.name = "b" + candc.projectileNames*;*

var newAcross:float = ( widthModel + widthGap ) * i;
newy.transform.localPosition = Vector3(newAcross, 0, 0);

newy.GetComponent(bouncyButton).setThePridTKImage(
i, “p”+candc.projectileNames );
myProjectiles.Add( newy.GetComponent(bouncyButton) );
}

Debug.Log(“built buttons. didn’t use any damn editor dragging”);
}

//////////////////////////////////////////////

function FindUnderHereNamed(name:String):Transform
{
var transforms = transform.GetComponentsInChildren(Transform);
for (var t : Transform in transforms)
{
if (t.name == name) return t;
}
return null;
}

//////////////////////////////////////////////

What you need to do is build standard classes that represent you game elements - this is possible in both Unity Script and C#. You can fully benefit from inheritance if you go down that route and if you make your classes Serializable (by decorating them with the Serializable attribute) then you can still edit instances of them with the inspector, when they are variables on behaviours of your game objects.