Interfaces

Overview

Interfaces in BattleScript define the structure of objects and provide type safety for complex data structures. They are similar to TypeScript interfaces or C++ structs, allowing you to define custom data types with specific properties.

Defining Interfaces

Interfaces are defined using the interface keyword, followed by the interface name and a block of property definitions:

interface Player {
    int id;
    string name;
    int health;
    int score;
    bool isActive;
}

Each property in an interface has a type and a name, separated by a semicolon.

Interface Inheritance

Interfaces can extend other interfaces using the extends keyword, inheriting all properties from the parent interface:

interface Event {
    string eventName;
}

interface Event_Button_Pressed extends Event {
    int buttonId;
    int pressType;  // Short press, long press, etc.
}

In this example, Event_Button_Pressed inherits the eventName property from Event and adds its own properties.

Using Interfaces

Interfaces are used to define the types of variables, function parameters, and return values:

// Declare a variable with an interface type
Player player1;

// Initialize the player
player1.id = 1;
player1.name = "Player 1";
player1.health = 100;
player1.score = 0;
player1.isActive = true;

// Function that takes an interface as a parameter
void displayPlayerInfo(Player player) => {
    std::console.log("Player: " + player.name);
    std::console.log("Health: " + player.health);
    std::console.log("Score: " + player.score);
}

// Call the function with our player
displayPlayerInfo(player1);

Interface Arrays

You can create arrays of interface types to manage collections of structured data:



// Create an array of players
Player[] players;

// Add players to the array
Player newPlayer;
newPlayer.id = 2;
newPlayer.name = "Player 2";
newPlayer.health = 100;
newPlayer.score = 0;
newPlayer.isActive = true;

players.push(newPlayer);

// Access array elements
Player firstPlayer = players[0];
std::console.log("First player: " + firstPlayer.name);

Common Interfaces

BattleScript includes several built-in interfaces for common data structures:

  • Event - Base interface for all event types
    interface Event {
        string eventName;
    }
  • Data_IrData - Structure for infrared data
    interface Data_IrData {
        int sensorId;
        int playerId;
        int teamId;
        int bulletTypeId;
        int hitValue;
        int isCritical;
    }
  • GameConfig - Configuration for game settings
    interface GameConfig {
        int gameMode;
        int timeLimit;
        int maxPlayers;
        bool teamBased;
        int[] availableWeapons;
    }

Related Topics