Lua Functions



S2D_RunScript

FunctionDescription
S2D_RunScript(string)Attemps to run the passed in lua script. It takes a string for the script path. Returns true if successful, false otherwise. There will also be a log of why the script failed.

Example:


-- Run the script list table
if not S2D_RunScript("scipts/script_list.lua") then 
	S2D_error("Failed to run lua script")
end

S2D_LoadScriptTable

FunctionDescription
S2D_LoadScriptTable(sol::table)This will attempt to run a lua table filled with the path of lua scripts to run. If running a script fails, it will log an error with the reason for failing.

Example:


-- Script List Example
ScriptList = 
{
	"game/scripts/assetDefs.lua",
	"game/scripts/utilities.lua",
	"game/scripts/player.lua",
	-- ... More scripts here
}

-- Run all the scripts in the table 
S2D_LoadScriptTable(ScriptList)

S2D_MeasureText

FunctionDescription
S2D_MeasureText(string text, string font)Takes in a string of text and the desired font and returns the width of the text as a float.

Example:


-- Script List Example

local transform = entity:get_component(Transform)
local text = entity:get_component(TextComponent)
local textWidth = S2D_MeasureText(text.textStr, text.fontName)

-- Center the entity to the window width 
transform.position = (S2D_WindowWidth() * 0.5) - (textWidth * 0.5)

Logging Functions

These logging functions use the common placeholder and formatting options:

PlaceholderDescription
%sPlaceholder for a string.
%cPlaceholder for a character.
%dPlaceholder for an integer number.
%fPlaceholder for a float number.
%.nfDesignates the number of decimal places. Where 'n' is the number of points.
%xPlaceholder for a hexidecimal number.

S2D_log

FunctionDescription
S2D_log(string, ...)The string to be formatted in the log followed by the variadic arguments.

Example:


-- Logging Example

S2D_log("My name is %s. I am %d years old", "Scion", 2)

S2D_log

S2D_warn

FunctionDescription
S2D_warn(string, ...)The string to be formatted in the log followed by the variadic arguments.

Example:


-- Warning Example

Names = 
{
	"Jack", "Jill", "Steve"
}

local nameToFind = "Scion"
local bFound = false 

for _, v in pairs(Names) do 
	if v == nameToFind then 
		bFound = true 
		break
	end 
end 

if not bFound then
	S2D_warn("This name [%s] could not be found.", nameToFind)
else
	S2D_log("This name [%s] was found successfully.", nameToFind)
end

S2D_warn

S2D_error

FunctionDescription
S2D_error(string, ...)The string to be formatted in the log followed by the variadic arguments.

Example:


Assets = 
{
	Textures = 
	{
		{ name = "hero", path = "./assets/textures/hero.png", bPixelArt = true },
		-- ... More Textures
	},
	Sounds = 
	{
		-- Add sounds
	}
}

-- Error Example

for k, v in pairs(Assets.Textures) do
	if AssetManager.add_texture(v.name, v.path, v.bPixelArt ) then 
		S2D_log("Added texture [%s] successfully", v.name)
	else
		S2D_error("Failed to add texture [%s] at path [%s].", v.name, v.path)
	end
end 

S2D_error

Physics Functions

S2D_EnablePhysics

FunctionDescription
S2D_EnablePhysics()Enables Box2D Physics.

Example:


-- Enable Box2D Physics

S2D_EnablePhysics()

S2D_DisablePhysics

FunctionDescription
S2D_DisablePhysics()Disables Box2D Physics.

Example:


-- Disable Box2D Physics

S2D_DisablePhysics()

S2D_IsPhysicsEnabled

FunctionDescription
S2D_IsPhysicsEnabled()Check to see if Box2D physics is enabled. Returns true if enabled, false otherwise.

Example:


-- Enable Box2D Physics

S2D_DisablePhysics()

Render Collider Functions

S2D_EnableCollisionRendering

FunctionDescription
S2D_EnableCollisionRendering()Enables the rendering off all colliders. Colliders are usually hidden in game. Enabling is usually for debugging purposes.

Example:


-- Show Box/Circle Colliders

S2D_EnableCollisionRendering()

S2D_DisableCollisionRendering

FunctionDescription
S2D_DisableCollisionRendering()Disable the rendering off all colliders. Colliders are usually hidden in game.

Example:


-- Hide Box/Circle Colliders

S2D_DisableCollisionRendering()

S2D_CollisionRenderingEnabled

FunctionDescription
S2D_CollisionRenderingEnabled()Checks to see if collision rendering is enabled.

Example:


-- Check to see if Collision rendering is enabled
if S2D_CollisionRenderingEnabled then 
	S2D_DisableCollisionRendering()
end