This barely scratches the surface. There are a ton more commands than just this, and there's a lot more you can do with Lua than is covered in this guide. All this does is just get you into a very tiny bit of what Lua can do.
Also note that there are multiple ways to do Lua functions! My way isn't the only way. It's probably much more improper my way as I am starting out myself, so I recommend NOT using this as a de facto guide for Lua!
Keep in mind that I'm also fairly new to Lua, so if you find an error in my coding, please point it out.
NOTE: It's Lua, not LUA. Lua is not an acronym!
Lua is a simplified programming language that was introduced to SRB2 in version 2.1. This is a guide for beginner Lua users.
This guide is mostly just me having basic examples and explaining part by part.
The newest version of SLADE now has and automatically selects the SRB2 Lua language by default.
Reference Links
Lua Scripting in SRB2 2.1 by JTE
Lua basics guide (VERY helpful!)
Monster Iestyn's Lua documentation thread by Monster Iestyn
Monster Iestyn's Lua documentation (Skybase mirror)
SRB2 wiki's article on Lua
The Guide
Here are important keywords you'll need to know:
do
if
else
for
function
or
View Monster Iestyn's Lua documentation thread for more keywords and information.
Comments are signified by these symbols:
-- comment
--[[ comment ]]
// comment
/* comment */
Here is a simplified version of one of my Lua examples:
Code:
addHook("ThinkFrame", do
for player in players.iterate
if player.mo.state==S_PLAY_STND
or (player.mo.state>=S_PLAY_TAP1 and player.mo.state<=S_PLAY_TAP2)
hud.enable ("rings")
hud.enable ("time")
hud.enable ("score")
hud.enable ("lives")
else
hud.disable ("rings")
hud.disable ("time")
hud.disable ("score")
hud.disable ("lives")
end
end
end)
addHook: Adds a hook to the game. ThinkFrame is specified essentially for modifying objects mid-level. do tells the game to, well, do what comes after it.
for: For the player (players.iterate)
if: If the player state (player.mo.state) is equal to (==) S_PLAY_STND (player standing state), or if the player.mo.state is greater than or equal to S_PLAY_TAP1 and is less than S_PLAY_TAP2...
Then the game will execute the following functions underneath, which are hud.enable ones.
else: Otherwise...
The game will execute the other commands underneath, which are the hud.disable ones.
end: Closes the commands: addHook, for, if
Lua Examples
Example of how to make the hud disappear during certain states (with full comments):
Code:
addHook("ThinkFrame", do // ThinkFrame initation
for player in players.iterate // For players only
if (player.mo.state==S_PLAY_STND // If player is standing...
or (player.mo.state>=S_PLAY_TAP1 and player.mo.state<=S_PLAY_TAP2) // Or if the player is idling...
or (player.mo.state>=S_PLAY_TEETER1 and player.mo.state<=S_PLAY_TEETER2) // Or if the player is teetering on an edge (first state)...
or (player.pflags & PF_NIGHTSMODE)) // Or if we're in NiGHTS / Special Stage mode (for stability), the hud will...
hud.enable ("rings") // Have rings enabled.
hud.enable ("time") // Have time enabled.
hud.enable ("score") // Have score enabled.
hud.enable ("lives") // Have lives enabled.
else // Otherwise...
hud.disable ("rings") // Have score disabled.
hud.disable ("time") // Have score disabled.
hud.disable ("score") // Have score disabled.
hud.disable ("lives") // Have lives disabled.
end // Closes if.
end // Closes for.
end) // Closes addHook.
Example of how to make the hud toggleable with custom buttons:
Code:
addHook("ThinkFrame", do
for player in players.iterate
if (player.cmd.buttons & BT_CUSTOM1) // If Custom Button 1 is pressed, execute the following.
hud.enable ("rings")
hud.enable ("time")
hud.enable ("score")
hud.enable ("lives")
end
end
end)
addHook("ThinkFrame", do
for player in players.iterate
if (player.cmd.buttons & BT_CUSTOM2) // If Custom Button 2 is pressed, execute the following.
hud.disable ("rings")
hud.disable ("time")
hud.disable ("score")
hud.disable ("lives")
end
end
end)
More to come.