SDK Syntax Question

Quornix

Warlord
Joined
Dec 27, 2006
Messages
134
I've got a question about the syntax on pulling in info from the XML. I've just finished adding in a pure Attack and Defense bonus for promotions (rather than the +strength/-defense on each terrain method I've seen a couple of times), and it's pretty involved. Basically, I copied most of it from the Combat bonuses, and changed things in ~50 places. It works though!! Proper combat odds display and everything!

Then I see this:

if ((pPlot->getFeatureType() != NO_FEATURE) && (pPlot->getFeatureType() != (FeatureTypes)GC.getInfoTypeForString("FEATURE_FLOOD_PLAINS")))

This is the Flood Plain fix by Mongoose. This is the only line of the Flood Plain fix by Mongoose.

Could someone explain how the GC.getInfoTypeForString function works? Because that sure seems a lot simpler. Are there a number of similar functions? I came to understand what was going on on the complicated one, since I had about 50 points of data to work from, but with just this line, I don't really understand how it works.

Thanks, all!
 
I've got a question about the syntax on pulling in info from the XML. I've just finished adding in a pure Attack and Defense bonus for promotions (rather than the +strength/-defense on each terrain method I've seen a couple of times), and it's pretty involved. Basically, I copied most of it from the Combat bonuses, and changed things in ~50 places. It works though!! Proper combat odds display and everything!

Then I see this:

if ((pPlot->getFeatureType() != NO_FEATURE) && (pPlot->getFeatureType() != (FeatureTypes)GC.getInfoTypeForString("FEATURE_FLOOD_PLAINS")))

This is the Flood Plain fix by Mongoose. This is the only line of the Flood Plain fix by Mongoose.

Could someone explain how the GC.getInfoTypeForString function works? Because that sure seems a lot simpler. Are there a number of similar functions? I came to understand what was going on on the complicated one, since I had about 50 points of data to work from, but with just this line, I don't really understand how it works.

Thanks, all!

In XML, most infos have, amongst their other elements, one special element called the "Type". It's like the name for that info. For example, the "Type" of the flood plain feature is "FEATURE_FLOOD_PLAIN".

Each different feature is given a number at the beginning when all the XML is read into the game. Since it's faster to compare numbers rather than strings, you typically just want to be able to take a string (something easy for a human to recognize), turn it into a number using one call (so that it's in the form that a computer can recognize) and use that number for the rest of the computations.

In this example, GC.getInfoTypeForString("FEATURE_FLOOD_PLAIN") returns the number that is specific to flood plains. If you have 5 different features, then this number would be between (and including) 0 and 4. Each type of feature would have one number that identifies it amongst the other feature. If you want to see if a plot has a feature, you check to see what number it has.

In order to be type-safe (that is, to know that when you're passing a number that should be a feature type rather than a number that is a terrain type, which would mean you probably coded something wrong), the number has to be cast into a FeatureTypes enumerated variable. That's why you see the (FeaturesTypes) before the GC.getInfoTypeForString(). getInfoTypeForString returns an integer, whereas getFeatureType returns a FeatureTypes enumerated value. If you tried to accidentally compare a feature type and a terrain type, there would be a syntax error, rather than having to try to discover the problem during run-time. Same thing if you compare a FeatureType with an integer. It just makes for being able to catch your mistakes earlier.

What this code basically does is make sure that there is a feature on the plot..

Code:
if ([B](pPlot->getFeatureType() != NO_FEATURE)[/B] && (pPlot->getFeatureType() != (FeatureTypes)GC.getInfoTypeForString("FEATURE_FLOOD_PLAINS")))

..and, if that's true, check to see if the plots feature is a flood plain.

Code:
if ((pPlot->getFeatureType() != NO_FEATURE) && [B](pPlot->getFeatureType() != (FeatureTypes)GC.getInfoTypeForString("FEATURE_FLOOD_PLAINS")[/B]))

Hope this helps.

Edit: Also, if you want to fool around to see what GC.getInfoTypeForString does, there's a python equivalent call...

gc.getInfoTypeForString()

Note the lowercase gc

If you open the python screen in game (I use ctrl+shift+~) you can try that command out.
 
Top Bottom