WarScript API Reference

Native functions available in the global scope

Language

WarScript syntax reference — constructions, data types, and operators.

Basic Constructions

Variables

# plain types
a1 = 123
a2 = "hello world"

# class instance
left_tree_node = new TreeNode [ 1 ]
tree_node = new TreeNode [ 3, left_tree_node, new TreeNode [ 2 ] ]

# array
example_array = { 1, 2, "three", new TreeNode [ 4 ] }
empty_array = {}

Conditions

if a1 > 5 and tree_node :: value == 3
    # statements
elif a2 == "hello" or a3 == "world"
    # statements
else
    # statements
end

Print

print a1 + a2 + tree_node :: value

Import

import "lib/common.ws"

Functions

fun fibonacci_number [ n ]
    if n < 2
        return n
    end
    return fibonacci_number [ n - 1 ] + fibonacci_number [ n - 2 ]
end

Loops

# for loop
loop i in 0..10
    # statements
end

# with step
loop i in 0..100 by 5
    # statements
end

# while loop
loop health > 0
    # statements
end

# for-each
loop item in items
    # statements
end

# break and next
loop i in 0..10
    if i == 5
        break
    end
    if i == 3
        next
    end
end

Exceptions

class MyException [message]
end

begin
    raise new MyException ["Error message"]
rescue error
    print error :: message
ensure
    print "Always runs"
end

Data Types

Numeric

number1 = 1
number2 = 3.21
number3 = .543
number4 = -1

Text

text = "hello world"
print text{1}
text{1} = "a"

Logical

logical1 = true
logical2 = false

Class

class Lamp [ type, is_on ]
    fun turn_on
        is_on = true
    end

    fun turn_off
        is_on = false
    end

    fun set_is_on [ is_on ]
        this :: is_on = is_on
    end
end

lamp = new Lamp [ "Halogen", false ]

# get/set properties
lamp_is_on = lamp :: is_on
lamp :: type = "Led"

# invoke functions
lamp :: turn_off []

# inheritance
class DerivedClass: BaseClass [ property ]
end

Arrays

items = { 1, 2, "three" }

# get/set by index
value = items{1}
items{1} = 123

# append
items << 3

Null

value = null

NativeObject<T>

fun on_unit_died[unit]
    print Unit_get_position[unit]
end

Operators

Operators ordered by precedence (lowest to highest):

OperatorSymbolPrecedenceExample
Assignment=1a = 5
Append to array<<1array << "value"
Logical ORor2true or false
Logical ANDand3true and true
Parentheses( )4
Equals==5a == 5
Not Equals!=5a != 5
Greater Than Or Equals>=5a >= 5
Greater Than>5a > 5
Less Than Or Equals<=5a <= 5
Less Than<5a < 5
Addition+6a + 5
Subtraction-6a - 5
Exponentiation**7a ** 5
Multiplication*7a * 5
Division/7a / 5
Floor Division//7a // 5
Modulo%7a % 5
NOT!8!false
Class Instancenew8new Type [ value ]
Nested Instance:: new8type :: new NestedType
Property Access::8type :: value
Castas8type as Supertype
Instance Ofis8type is Supertype

Global

attach_new_script [scriptName]

Attaches and runs a new WarScript by name on the current room.

ParameterType
scriptNameText
detach []

Detaches the current script from the room and stops execution.

No parameters

get_players [] Array<NativeObject<WarmanPlayer>>

Returns an array of all players currently in the room.

No parameters

play_sound [name]

Plays a one-shot sound effect by name. Only audible if the local player is observing the room.

ParameterType
nameText
spawn_soul [position] NativeObject<EnemySoul>

Spawns a unit soul at the given world position. Returns null if position is invalid.

ParameterType
positionArray
spawn_unit [unitName, position, level, isAlly, isElite, isSummon] NativeObject<Enemy>

Spawns a unit at the given world position.

ParameterType
unitNameText
positionArray
levelNumeric
isAllyLogical
isEliteLogical
isSummonLogical

Player

Functions that operate on a Player handle.

Player_give_exp [player, exp]

Awards experience points to a player.

ParameterType
playerNativeObject<WarmanPlayer>
expNumeric

Unit

Functions that operate on a Unit handle.

Unit_apply_modifier [unit, modifierName] NativeObject<ModifierWrapper>

Applies a modifier by name to a unit. Returns null if the modifier is not found.

ParameterType
unitNativeObject<Unit>
modifierNameText
Unit_get_position [unit] Array<Numeric>

Returns the world position of a unit as an array of [x, y, z].

ParameterType
unitNativeObject<Unit>

Events

Callback functions called by the engine when implemented in your script.

event on_attach []

Called when attaching the script to a room.

fun on_attach[]
event on_player_enter_room [player]

Event when a player enters the room.

ParameterType
playerNativeObject<WarmanPlayer>
fun on_player_enter_room[player]
event on_unit_died [unit]

Event when a unit dies in the room.

ParameterType
unitNativeObject<Enemy>
fun on_unit_died[unit]
event tick [dt]

Called each simulation tick. Includes the deltaTime of the current tick.

ParameterType
dtNumeric
fun tick[dt]