how does CivIV and the SDK handle global and local variables?

Kailric

Jack of All Trades
Joined
Mar 25, 2008
Messages
3,100
Location
Marooned, Y'isrumgone
How does the SDK handle global and local variables... thats something I am gonna look into and figure out.. cause I know from past experience global and local variables allow you to do awesome things. Like can local variables be attached to Units, Buildings, etc, in the Code itself. That is, without having to add a new tag in XML.

Like for instance...

pAttacker = this
pAttacker set local variable "nameofvarable" to value "value"

Same for global variables.

Like for example, if you want to track the cities a unit enters and record what the culture value was of each city. You would add a local variable array to the unit, each time it entered a city it would add that city to the array, then add the cities culture. Then later on you could call up the unit if you had it assigned to a global varable, check its array, and do what ever.

I am not trying to do that example but was just wondering if such above is possible with the SDK?
 
Well, you can get pointers to every object in the game. For example, if a Unit enters a city, you can get the city by accessing pointer to the plot object the unit is on.
Ex: pCity = pUnit.plot().getPlotCity()
iCulture = pCity.getCulture(pUnit.getOwner())

You could add new variables to the unit in the SDK if you like. Like that array you spoke of. Or you can store scriptdata in the pUnit object through python and the pickle module. (Some modders has written nice modules for this).
 
Well, you can get pointers to every object in the game. For example, if a Unit enters a city, you can get the city by accessing pointer to the plot object the unit is on.
Ex: pCity = pUnit.plot().getPlotCity()
iCulture = pCity.getCulture(pUnit.getOwner())

You could add new variables to the unit in the SDK if you like. Like that array you spoke of. Or you can store scriptdata in the pUnit object through python and the pickle module. (Some modders has written nice modules for this).

Thanks man.. I added my first working Global variable yesterday.. it was sweet!

I have another question .. what are the load orders to the files in SDK, I guess you would need to know this so that your Global variables are declared before a call is made to them. So, if I had globals what file should they be declared in... CvInfos probably?
 
Well, the globals in GlobalDefinesAlt.xml don't need to be added to the SDK if they are just an int, float, or other numeral. They are used with the GC.getGlobalDefineINT("NAME")

But if the Global is a text like this one:

Code:
    <Define>
        <DefineName>WEAPON_REQ_BONUS_TIER1</DefineName>
        <DefineTextVal>BONUS_COPPER</DefineTextVal>
    </Define>

Then you need to define it in CvXMLLoadUtilitySet.cpp like this:

Code:
SetGlobalDefine("WEAPON_REQ_BONUS_TIER1", szVal);
idx = FindInInfoClass(szVal);
GC.getDefinesVarSystem()->SetValue("WEAPON_REQ_BONUS_TIER1", idx);

Then of course, there is the CvGlobals.cpp and CvGlobals.h


CvInfos is a file that loads all variables from basically all info xml files, Civ4UnitInfos.xml for example. This is the file you need to modify if you add new tags to those xml files.

There is lots of threads on this topic, and tutorials on how to do specific things. I would recommend doing some of them so you familiarize yourself with the SDK.
 
I'm a little confused by the term "global variable." There are global variables, of sorts, in Civ IV, but they aren't the same kind of globals you'd have in a programming language like (Flash) ActionScript.

Most of the variables in Civ IV come in the form of what are sometimes called class 'properties.' These are values set up at the individual class level that define the way some type of object works. For example, a unit object has s number of hit points, the number of moves it has remaining, and so on, defined as properties of the unit class. Each object of that type gets its own copy of the variable, so thats how its possible for 2 units of the same type to have different current HP.

There are also some unchanging values, like enums, that are similar to global variables, except that their value doesn't vary once its defined (tho, confusingly, it can change from mod to mod). These are defined at a global level in the sense that you don't normally need to provide a class path to use them. Stuff like COMMERCE_GOLD, YIELD_PRODUCTION, OPTION_NO_CITY_FLIPPING and NO_PLAYER are examples.

There are also Info classes which consist of data read straight out the XML. Traits, for example, never change, so they are simply loaded into the game and stored in the GC object (which stands for Global Context, I think) so that you can reference them from anywhere. In a sense, these values are global.

There is also a class out there called CvGame that holds all of the attributes of the current game that is being played. The values stored here are global to the current game but are different from info classes or enums because they represent the current state of the game being played. Many of these variables are also written to the file when the player saves. An example of something like would be a variable that stores who the person to found a religion is. It's "global" in the sense that only 1 player can do it per game, but when you start a new game or load a different one it could be someone else.
 
Back
Top Bottom