Examples

Hello World and Console

The console functions accept any type.



std::console.log("Hello, BattleScript!");
std::console.warn("Careful now...");
std::console.error("Something went wrong");

Variables and Constants

Declare typed variables and constants. Assign and use them in expressions.



const int MAX_HEALTH = 100;
int health = 75;
bool isAlive = health > 0;

std::console.log("Health:", health, "/", MAX_HEALTH);
std::console.log("Alive:", isAlive);

See also: Variables and Data Types

If / Else If / Else

Control the flow of your program with conditions.



int health = 22;

if (health <= 0) {
  std::console.log("Eliminated");
} else if (health < 30) {
  std::console.log("Critical");
} else {
  std::console.log("Healthy");
}

Your Own Functions

Define functions with parameters and return values, then call them.



int add(int a, int b) => {
  return a + b;
}

void greet(string name) => {
  std::console.log("Hello, "+name+"!");
}

int sum = add(5, 7);
std::console.log("Sum:", sum);

greet("Player1");

See also: Functions

Using the Standard Library

You can use utility libraries like std::math and std::console (see API reference for the full list).


// Random between min and max (inclusive of min, exclusive of max)
float r = std::math.randomFloat(0.0, 1.0);

// Simple damage calculation
int base = 10;
float critChance = 0.25;
bool isCrit = std::math.randomFloat(0.0, 1.0) < critChance;
int damage = base + (isCrit ? 10 : 0);

std::console.log("Random:", r);
std::console.log("Damage:", damage);

More: Math UtilitiesTime UtilitiesArray Utilities

Arrays, Loops, and Search

Work with arrays using the standard utilities and loop constructs.




string[] fruits = ["apple", "banana", "cherry"];

// Iterate by index
for (int i = 0; i < fruits.length; i = i + 1) {
  std::console.log(i, fruits[i]);
}

// Find an item
int idx = std::arrays.indexOf(fruits, "banana");
if (idx >= 0) {
  std::console.log("Found at index", idx);
} else {
  std::console.log("Not found");
}

Where next?