uniTM API
Usage Example

Example of implementing a scoring system in a simple distance-based jumping challenge.

The goal is to jump as far as you can, and use the distance as a record value.

#include "uniTestManagerApi.asc"
const int JUMP_START_POS_X = 14 * 32;
void onLevelLoad()
{
// Check if uniTM is exposing the interface
jjPUBLICINTERFACE@ pi = jjGetPublicInterface("uniTM.mut");
if (pi !is null)
{
// Get an instance of TestManagerApi
@tm = cast<UniTM::ITestManagerApi>(pi);
// Set up custom scoring rules. Call configureCustomRanking() inside OnLevelLoad() and only as a server.
if (tm !is null && jjIsServer) tm.configureCustomRanking(
true, // higher distance is better, therefore true
UniTM::DISPLAYFORMAT::DECIMAL2, // we will use DECIMAL2 to display record values with 2 decimal spaces (enough to represent a fraction of a tile)
"tile", // we will measure distance in tiles, therefore our unit is a tile
"tiles", // same but plural
false, // this isn't a time based level, so let's leave timer disabled
1 // we only have one "level" if we can call it that
);
}
}
/* The following functions trigger when a player runs into a text event with a corresponding ID and its angelscript property set to 1.
Text events with ID 0 trigger onFunction0, ID 1 triggers onFunction1 and so on. */
/* Let's start with a function that initializes the test session.
We will place these text events around the spawn point, and around the warp target that takes us back to the start after a jump.
This ensures that the player will always be in [ini] state before attempting the next jump. */
void onFunction0(jjPLAYER@ player)
{
// initTest() assigns the [ini] tag
if (tm !is null) tm.initTest(player);
}
/* Next up, let's ensure the player begins a test as they jump.
We will place these text events right at the "cliff" to sync the start of the test with the jump. */
void onFunction1(jjPLAYER@ player)
{
// beginTest() assigns the [1] tag, and in our case also begins ghost run recording (can be configured with the recordGhost param)
if (tm !is null) tm.beginTest(player);
}
/* Finally, at the bottom of the level we place these text events to detect that a player has landed and finished the jump.
We will use this opportunity to calculate the distance, submit a record and warp the player back to the start. */
void onFunction2(jjPLAYER@ player)
{
// Check if the player isn't already warping
if (player.warpID == 0)
{
// Calculate travelled distance. Since the display format is DECIMAL2, multiply the number of tiles by 100 to represent the distance with two decimal places.
int record = (int(player.xPos) - JUMP_START_POS_X) * 100 / 32;
if (tm !is null)
{
// finishTest() assigns the [win] tag, submits the record, stops the ghost recording and in this case, announces the record only if we beat our best score.
tm.finishTest(player, record, UniTM::ANNOUNCERECORD::ON_NEW_PERSONAL_BEST);
// Optionally display the record to the player. formatRecord() will display it using the formatting and units previously set in configureCustomRanking()
jjAlert("|||||||Your score: ||||" + tm.formatRecord(record));
}
// Warp the player back to the start.
player.warpToID(0);
}
}
const uint8 ON_NEW_PERSONAL_BEST
Definition uniTestManagerApi.asc:290
const uint8 DECIMAL2
Definition uniTestManagerApi.asc:267
Definition uniTestManagerApi.asc:14