Documentation

Printing the amount of planets

Note: This tutorial is only really valuable in Maniaplanet, since Trackmania and Turbo do not make use of planets as a currency. Check out speeding up the menu music for another nice beginner's tutorial!

As an example, we will print to console how many planets you have. The amount of planets happens to be in the CGamePlayerProfileChunk_AccountSettings class. If you take a look at CGameCtnApp, you will find that the path from game app to planets is: app.CurrentProfile.AccountSettings.OnlinePlanets

The most basic code to print this value would thus be:

void Main()
{
  CGameCtnApp@ app = GetApp();
  uint planets = app.CurrentProfile.AccountSettings.OnlinePlanets;
  print("You have " + planets + " planets.");
}

If you reload this script ingame, it will print the amount of planets:

[   ScriptRuntime] [13:08:14] You have 438818 planets.

Of course, this script isn't great - it will throw a null reference exception if you just start the game, because CurrentProfile is null until you log in. So, to solve that, we should do a simple null check first:

void Main()
{
  CGameCtnApp@ app = GetApp();
  if (app.CurrentProfile !is null)
  {
    uint planets = app.CurrentProfile.AccountSettings.OnlinePlanets;
    print("You have " + planets + " planets.");
  }
}

Now it will only print the amount of planets if you reload when logged in. Of course, we can make this even better, by waiting until the player has logged in. Due to the nature of yieldable functions, we can do this pretty easily with a while loop:

void Main()
{
  CGameCtnApp@ app = GetApp();

  while (app.CurrentProfile is null) {
    yield();
  }

  uint planets = app.CurrentProfile.AccountSettings.OnlinePlanets;
  print("You have " + planets + " planets.");
}

Note that we're using is here, and not ==. That's because CurrentProfile is a handle type, which is denoted by the @ sign after the type name. Since Angelscript has no pointer dereference operator like C++ has with ->, we'll have separate operators for handles and handle values. For this reason, we separate the is and == operators when working with handles.

For example, if we use obj == null on a handle type, it will actually be rewritten by Angelscript internally as obj.opEquals(null) so that the == operator can be overloaded by the object's class. Therefore, if obj is already null here, it will throw a null reference exception. To actually check if the handle itself is null, we use the is operator, like obj is null.


Page updated 4 years ago by Miss(Trusted developer)