Tips & Tricks

From Star Ruler 2 Wiki
Jump to: navigation, search

Finding out what type of Object is contained in an instance

This is very handy for debugging suppose you have declaration of object

Object@ myobject; 

and at one point you need to know what myobject contains you can print out a line with the type of the object. There are two ways of achieving this

print(addrstr(myobject)); //output: [Subsystem at 0xffff]

getObjectTypeName(myobject.type); //output: Ship

Fetching a variable in the script

You can define your own variables on quite a few objects. At some point you will need access to them in your scripts. Here is an example to fetch a variable from a subsystem.

I've added a variable called SpecialDamageResist to one of my subsystems and I have access to an object of this subsystem in the member subsystem. Now if I want to grab the value I can do the following

   int specialDamage = int (subsystem.variable(SV_SpecialDamageResist));

Notice that I first check that the subsystem even has the variable ? This is to avoid any unpleasantness like nullpointer exceptions. Also take notice of the prefix SV_. subsystem variables are part of a dynamic enum in this case SubsystemVariable, this enum will be appended with any variable you declare yourself on subsystems. Since the system can't know what kind of datatype my variable is , you see the deliberate cast to int there. This is a simple and effective way to fetch a variable.