Global Functions

Miscellaneous

Functions

LogMsg

nil LogMsg (string message)

Adds a message to the log.  You can see it by bringing up the console, or checking the log.txt file.  Lua’s native “print” function is routed to use LogMsg also.

Parameters

messageAny text you want to display or log.

LogError

nil LogError (string message)

Like LogMsg but forces the console to pop-up and prints in red.  Should be used for serious errors that you want to know about.

Logged to log.txt.

Parameters

messageAny text.  Note that it prepends the “Error: “ to your message automatically.

Exists

nil Exists (string funcOrVarName)

Allows you to check the global namespace to see if a variable or function name exists.  To improve readability, you should probably use FunctionExists or VariableExists instead.

If you’d like to check a specific entity namespace, use <Entity:FunctionExists> and <Entity:VariableExists> instead.

Parameters

funcOrVarNameThe exact name of the function or variable.

FunctionExists

nil FunctionExists (string funcName)

Allows you to check the global namespace to see if a certain function exists.

If you’d like to check a specific entity namespace, use <Entity:FunctionExists> instead.

Parameters

funcNameThe exact name of the function.

VariableExists

nil VariableExists (string varName)

Allows you to check the global namespace to see if a certain function exists.

If you’d like to check a specific entity namespace, use <Entity:VariableExists> instead.

Parameters

varNameThe exact name of the variable.

CreateEntity

Entity CreateEntity(Map map, Vector2 vPos, string scriptFileName)

Creates an entity on the specified map at vPos and inits it using the script file name specified.

There is an Entity::CreateEntity version that is identical except it allows relative paths (relative to the entity’s script) to be used.

Parameters

mapThe map to initialize the entity on.  If nil is passed in, the currently active map will be used.
vPosThe position to create the entity at.
scriptFileNameThe script it should be initialized with.

Returns

A handle to the Entity created.

RunScript

boolean RunScript (string name)

Loads a lua script.  If loaded by an entity, keep in mind the script will only exist in the entity’s name space, not globally.

Because lua’s other native script loading functions are disabled for security reasons this is the only way to load a script within a script.

Like images, sounds, and other data, scripts are searched for in mounted worlds in reverse order, so “mods” can override scripts if needed.

Usage

RunScript("system/sound.lua"); //load the sound module so everybody can use its functions

Parameters

namePath and file name.  Paths trying to reference outside of the world directory tree will be invalid.

Returns

True if the script was found and loaded.

Schedule

Schedule (number deliveryMS, number targetID, string message)

Schedules a line of lua script to be run after a delay.

If an entity ID is specified, the script is run from its namespace.

Internally, App::GetGameTick is used, so messages are paused/modified by the current game speed.

You can use backslashes to escape quotes or apostrophes if needed.

If an entity ID is specified and it doesn’t exist at the time of delivery, the message is discarded.

Usage

//delete the entity in 100 milliseconds
Schedule(100, this:GetID(), "this:SetDeleteFlag(true);");

//add the fade out and delete brain in 800 ms to "ent"
Schedule(800, ent:GetID(), "this:GetBrainManager():Add(\"FadeOutAndDelete\",'');");

Parameters

deliveryMSHow long to wait before delivering in milliseconds.  500 would mean half a second later.
targetIDEntity ID of who should run the script, or C_ENTITY_NONE for the global namespace.
messageThe line of lua code that should be executed.

CreateEntitySpecial

BaseEntity CreateEntitySpecial(string name, string parms)

Special kinds of entities like dialogs must be created with this function.

For simplicity, their real class types are not made available to lua, only the <BaseEntity> interface is accessible.

So how can you control them?  Primarily through <BaseEntity::Send> with text.

List of special entity types

  • ChoiceDialog
  • WorldChooseDialog

Uh..  Scan the example script files for these words for examples.

Parameters

nameThe kind of special entity you want to create.
parmsSame as using <BaseEntity::Send> but gets sent during initialization.

Returns

A handle to the <BaseEntity> created.

ScheduleSystem

nil Schedule (number deliveryMS, number targetID, string message)

Identical to Schedule except <GetTick> is used for timing.  This means messages will be delivered after the delay takes place, regardless of game speed or if the game is paused for dialog.

GetEntityByWorldPos

Entity GetEntityByWorldPos (Vector2 vPos, Entity entityToIgnore, boolean bPixelAccurate)

Returns the Entity handle at the specified position.  If there is more than one, it returns the one “on top”.

This function uses all visible layers and the active map for its check.

Usage

local pt = ScreenToWorld(GetInputManager:GetMousePos());
local ent = GetEntityByWorldPos(pt, this, true); //get all entities but ignore ourself, be pixel accurate

if (ent != nil) then
LogMsg("The mouse is over entity # " .. ent:GetID());
else
LogMsg("Didn't find anybody.");
end

Parameters

vPosThe point in the world we should check,
entityToIgnoreIf this isn’t nil, we’ll ignore this entity during our checks.
bPixelAccurateIf true, we’ll ignore entities that are transparent at the pixel location clicked.  (slower)

Returns

The Entity found, or nil if nothing was found.

GetEntityByID

Entity GetEntityByID (number entityID)

Converts an entityID to an Entity handle.  Very fast.

Usage

LogMsg("This entity's ID is " .. GetEntityById(ent:GetID()):GetID()); // a ridiculous example

Parameters

entityIDEvery entity has a unique number it can be referenced by.  (unique to that gaming session, not saved/loaded)

Returns

The Entity found, or nil if the ID is invalid.

GetEntityByName

Entity GetEntityByName (string name)

Converts a name to an Entity.  Very fast.  The entity’s map will be loaded if needed.

Usage

local entGun = GetEntityByName("Gun"); //if an entity is named Gun anywhere in the world, we'll get it.

Parameters

nameThe name set in the editor or with Entity::SetName.  Names are always unique.

Returns

The Entity found, or nil if the entity can’t be located.

GetEntityIDByName

EntityID GetEntityIDByName (string name)

Like GetEntityByName except it returns the entityID instead of the entity.

Usage

local entID = GetEntityIDByName("player");
if (entID != C_ENTITY_NONE) then
LogMsg("Found entityID " .. entID .. ". His name is " .. GetEntityByID(entID):GetName());
end

Parameters

nameThe entity’s name set in the editor or with Entity::SetName.  Names are always unique.

Returns

The entity’s ID if found, or <C_ENTITY_NONE> if the entity can’t be located.

GetSpecialEntityByName

BaseEntity GetSpecialEntityByName (string name)

Certain non-entity objects can exist and be manipulated at times using the <BaseEntity> handle.

Usage

local ent = GetSpecialEntityByName("editor");
if (ent != nil) then ent:SetDeleteFlag(true); end; //kill the editor window if it exists

if (GetSpecialEntityByName("ChoiceDialog") != nil) then LogMsg("A dialog window is active."); end;

Parameters

nameSpecial entities usually have pre-defined names.

Returns

The <BaseEntity> found, or nil if nothing is found.

ShowMessage

nil ShowMessage (string title, string msg, boolean bForceClassicStyle)

Quick way to pop-up a message box to show the user something.  The user must hit OK to continue.

Usage

ShowMessage("Error", "You shall not pass!", true);

Parameters

titleThe text at the top of the message box
msgThe main text.  Use \n to add a line feed
bForceClassicStyleNormally the style will uses the active game dialog style, but by passing true here you can force the boring silver style.

Lerp

number Lerp (number originalNum, number targetNum, number lerpAmount)

Lerp (a quasi-acronym for linear interpolation) is a method to make one number change into another number over time.

If you want an effect where a player’s score “counts up” as he gets points, but don’t want it to take two hours when he scores a million, lerping is for you!

Usage

displayScore = Lerp(displayScore, actualScore, 0.1); //lerp by ten percent, call every frame
LogMsg("You have " .. displayScore .. " Points!"); //watch as the number counts up

Parameters

originalNumThe number to start with
targetNumthe number we want to eventually end up with
lerpAmountthe percent of the difference to modify the number by

Returns

The modified number.

Conversion

Functions

ScreenToWorld

Vector2 ScreenToWorld (Vector2 vScreenPos)

Converts a screen position (0,0 being the upper left of your monitor) to a world position based on the active map and camera position.

Usage

//figure out where the mouse is pointing, in world coordinates
local vPos = ScreenToWorld(GetInputManager:GetMousePos());
LogMsg("The mouse is currently hovering over " .. tostring(vPos) .. " in world coordinates.");

Parameters

vScreenPosThe screen position you need converted to world coordinates.

Returns

The position converted to world coordinates.

WorldToScreen

Vector2 WorldToScreen (Vector2 vWorldPos)

The opposite of ScreenToWorld.

Parameters

vWorldPosThe world position you want converted to screen coordinates.  May be negative or larger than your monitor size.

Returns

The position converted to screen coordinates.

FacingToVector

Vector2 FacingToVector (number facing)

Converts a directional constant ( C_FACING_CONSTANTS ) into a unit vector.

Usage

LogMsg("The down direction is unit vector " .. FacingToVector(C_FACING_DOWN));

Parameters

facingOne of the C_FACING_CONSTANTS constants.

Returns

A Vector2 object containing a unit vector of the direction.

VectorToFacing

facing VectorToFacing (Vector2 vDirection)

Converts a unit vector into a C_FACING_CONSTANTS facing.

Parameters

vDirectionA Vector2 object containing a unit vector.

Returns

The C_FACING_CONSTANTS that bests fits the direction.

ColorToString

string ColorToString (Color color)

Converts a Color to a string format.  Useful for saving it through <Entity:Data>.

Usage

local colorString = ColorToString(Color(255,255,255,255));
LogMsg("Neat. In string format, that color is " .. colorString);
local myColor = StringToColor(colorString);
LogMsg("We converted it back!");

Parameters

colorA Color object.

Returns

A string formatted with the color data.

StringToColor

Color StringToColor (string colorText)

Converts a color formatted like “255 255 255 255” to a Color object.

Parameters

colorTextColors in a string.

Returns

A Color object.

VectorToString

string VectorToString (Vector2 v)

Converts a Vector2 to a string format.  Useful for saving it through <Entity:Data>.

Parameters

vA Vector2 that you want converted.

Returns

A string containing something like “343.2343 54.23”

StringToVector

Vector2 StringToVector (string textVector)

Converts a string like “23.4 5” to a Vector2.

Parameters

textVectorA string with two numbers in it.

Returns

A Vector2 object.

RectToString

string RectToString (Rect r)

Converts a Rect to a string format.  Useful for saving it through <Entity:Data>.

Parameters

rA Rect that you want converted.

Returns

A string containing something like “0 0 100 100”

StringToRect

Vector2 StringToRect (string textRect)

Converts a string like “0 0 10 10” to a Rect.

Parameters

textRectA string with four numbers in it.

Returns

A Rect object.

Related Constants

C_FACING_CONSTANTS

C_FACING_NONE

Means no facing at all.

C_FACING_LEFT

C_FACING_UP_LEFT

C_FACING_UP

C_FACING_UP_RIGHT

C_FACING_RIGHT

C_FACING_DOWN_RIGHT

C_FACING_DOWN

C_FACING_DOWN_LEFT

C_FACING_COUNT

How many directions we support, this will be 8.

C_ORIGIN_CONSTANTS

C_ORIGIN_TOP_LEFT

C_ORIGIN_CENTER_LEFT

C_ORIGIN_CENTER

C_ORIGIN_CENTER_RIGHT

C_ORIGIN_BOTTOM_CENTER

The Entity object.
The Vector2 object.
The color object stores R G B A color information.
The Rect object.