How To: Autocode to LunaLua
Jump to navigation
Jump to search
This is a guide for converting LunaDLL autocode to SMBX2's LunaLua code. Note that this guide is still incomplete; please refer to the LunaDLL Autocode Reference and LunaLua API for commands and functions not yet featured here.
Important Notes
- The player2 keyword can be substituted for player to apply any filters/behavior/etc. to the second player instead of the first.
- Some autocode commands do not have a LunaLua equivalent yet or do not require a LunaLua equivalent due to differences in how code is structured in the two languages.
Basic Filters
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| FilterToSmall,-,-,-,-,Active time,- | player.powerup = PLAYER_SMALL;
|
Player loses all powerups. |
| FilterToBig,-,-,-,-,Active time,- | if player.powerup ~= PLAYER_SMALL then
player.powerup = PLAYER_BIG;
end
|
Lowers player 1's powerup state down to "bigness" if they have anything better (fire flower, leaf...) If you are already small or big, you will stay small or big. |
| FilterToFire,-,-,-,-,Active time,- | if player.powerup > PLAYER_FIREFLOWER-1 then
player.powerup = PLAYER_FIREFLOWER;
end
|
Lowers player 1's powerup state down to fire flower if they have anything better (leaf, hammer suit...) If you are already small, big, or fire-flowered, you will stay that way. You can use another powerup constant in place of PLAYER_FIREFLOWER to filter to that powerup instead. |
| FilterMount,-,-,-,-,Active time,- | player:mem(0x108, FIELD_WORD, 0);
--For SMBX2 Beta 4 or later
player.mount = 0;
|
Removes your mount (yoshi, clown car, boot...) |
| FilterReservePowerup,-,-,-,-,Active time,- | player.reservePowerup = 0;
|
Removes whatever is in the reserve powerup box. Replace the 0 with another NPC ID to place that NPC in the reserve box. |
| FilterPlayer,-,P1,P2,-,Active time,- | -- Pre-v0.7.2
if player:mem(0xF0, FIELD_WORD) == P1 then
player:mem(0xF0, FIELD_WORD, P2);
end
-- 0.7.2 and up
if player.character == P1 then
player.character = P2
end
|
If the player is P1, change them to P2 instead. Replace P1 and P2 with the player constants of your choice. To filter all other characters to P2, just use the middle line. |
General Effect Commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| HeartSystem,X,Y,MAX HEARTS,-,Active time,- | Suggested library | Activates a heart tracking system for players that use hearts. Allows for more than 3 hearts and displays the current heart count at X / Y on screen. |
| InfiniteFlying,-,-,-,-,Active time,- | player:mem(0x170,FIELD_WORD,52)
|
Keeps the player's flight timer from counting down. |
| ForceFacing,NPC ID,Section,-,-,Length,- | for k,v in ipairs(NPC.get(NPC ID, Section-1)) do
v.direction = DIR_RIGHT;
if player.x < v.x then
v.direction = DIR_LEFT;
end
end
|
All NPCs of NPC ID in Section always face the player. |
Screen commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| PushScreenBoundary,Section,UDLR,-,-,Length,Speed | -- v0.7.2 and up
local spdX = #
local spdY = #
local boundaryRect = Section.get(#).boundary
boundaryRect.left = boundaryRect.left + spdX
boundaryRect.right = boundaryRect.right + spdX
boundaryRect.top = boundaryRect.top + spdY
boundaryRect.bottom = boundaryRect.bottom + spdY
|
Moves section borders. |
| ScreenEdgeBuffer,NPC ID,UDLR,Buffer Space,-,Length,- | local cam = Camera.get()[1]
local lMargin = cam.x - #
local rMargin = cam.x + cam.width + #
local tMargin = cam.y - #
local bMargin = cam.y + cam.height + #
for k,v in ipairs (NPC.get()) do
if v.x < lMargin then
v.x = lMargin
end
if v.x > rMargin then
v.x = rMargin
end
if v.y < tMargin then
v.y = tMargin
end
if v.y > bMargin then
v.y = bMargin
end
end
|
Keeps NPCs from leaving the screen. |
Text commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| ClearInputString,-,-,-,-,Length,- | Misc.cheatBuffer("")
|
Interrupts words and cheats the player is typing. |
| ShowText,-,X pos,Y pos,Font type,Length,String | Text.print(String, Font type, X pos, Y pos)
|
Shows String on the screen, at coordinates Xpos and Y pos, with specified font type. |
| ShowNPCLifeLeft,NPC ID,X pos,Y pos,Section,Length,Base health | local npc = NPC.get(NPC ID, Section)
local hpStr = "HP: "..tostring(npc:mem(0x148,FIELD_FLOAT)).."/"..tostring(Base health)
Text.print(hpStr, X pos, Y pos)
|
Displays how much life the first match for "NPC ID" in "Section" has. Displays on the screen at coordinates X pos and Y pos. Supply the correct base health for the NPC so it can calculate the correct remaining life. |
Triggers
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| Timer,Target,Event,Boolean for display,Boolean for repeat,Length,Boolean for once | See SMBX2 Beta 4's Timer Class. There's also a bulitin timer in the Level Setting without using Lua Script. | |
| Trigger,Section,Event #,-,-,Length,- | if player.section == Section-1 then
--Your code here
end
|
Section is 0-based in the Lua code. |
| TriggerRandom,Event 1,Event 2,Event 3,Event 4,Length,- | local eventid = RNG.randomInt(3)
if eventid == 0 then
--Your code here
elseif eventid == 1 then
--Your code here
elseif eventid == 2 then
--Your code here
else
--Your code here
end
|
|
| TriggerZone,Event,Top,Bottom,Left,Right,Boolean for once | if (player.x > Left and player.x < Right) and (player.y > Top and player.y < Bottom) then
--Your code here
end
|
|
| TriggerSMBXEvent,x,x,x,x,x,Event Name | triggerEvent("Event Name")
|
|
| IfNPC,NPC ID,Condition,To Section,Event,Length,Boolean for once | if NPC.get(NPC ID, Section) ~= (or ==) nil then
--Your code here
end
|
|
| IfVar,Comparison type,Value,Event,Length,Variable Name | if SaveData.VariableName (Comparision type) Value then
--Your code here
end
|
|
| OnInput,-,Button,Boolean for 1st frame,Event,Length,- | if player.keys.Button == Statement then;--Available Button Fields: up, down, left, right, jump, altJump, run, altRun, dropItem, pause
--Available Statement: KEYS_PRESSED, KEYS_UNPRESSED, KEYS_DOWN, KEYS_UP
--Your code here
end
|
|
| OnPlayerMem,Offset,Value,Comparison type,Event,Length,Data Type | if player:mem(Address, Data Type) (Comparision type, e.g. ==) Value then
--Your code here
end
|
|
| OnGlobalMem,Address,Value,Comparison type,Event,Length,Data Type | if mem(Address, Data Type) (Comparision type, e.g. ==) Value then
--Your code here
end
|
|
| OnCustomCheat,-,-,Boolean for once,Event,Delay,Cheat String | if string.match(Misc.cheatBuffer(), "Cheat String") ~= nil then
--Your code here
Misc.cheatBuffer(""); --Run-once
end
|
|
Layer commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| LayerXSpeed,Layer Number,Stop Flag,-,-,Length,Speed | local ticks = 0
local length = Length
if ticks < length then
Layer.get("Layer Name").speedX = Speed;--For non-stop layers, only use this function
ticks = ticks + 1
else
Layer.get("Layer Name"):stop()
end
|
|
| LayerYSpeed,Layer Number,Stop Flag,-,-,Length,Speed | local ticks = 0
local length = Length
if ticks < length then
Layer.get("Layer Name").speedY = Speed;--For non-stop layers, only use this function
ticks = ticks + 1
else
Layer.get("Layer Name"):stop()
end
|
|
Set commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| CyclePlayerLeft,-,-,-,-,Length,- | -- Pre-0.7.2
if player:mem(0xF0, FIELD_WORD) == CHARACTER_MARIO then
player:mem(0xF0, FIELD_WORD, CHARACTER_LINK);
else
local temp = player:mem(0xF0, FIELD_WORD) - 1;
player:mem(0xF0, FIELD_WORD, temp);
end
-- 0.7.2 and up
if player.character == CHARACTER_MARIO then
player.character = CHARACTER_LINK;
else
player.character = player.character - 1;
end
|
Cycles player character 'left'. |
| CyclePlayerRight,-,-,-,-,Length,- | -- Pre-0.7.2
local temp = math.max(1,(player:mem(0xF0, FIELD_WORD) + 1)%6);
player:mem(0xF0, FIELD_WORD, temp);
-- 0.7.2 and up
local temp = math.max(1, (player.character+1)%6);
player.character = temp;
|
Cycles player character 'right'. |
| Kill,Target,-,-,-,Length,Option | Target:kill();
|
Kills the target. |
| MemAssign,Address,Value,Operation,-,Length,Data type | mem(Address, Data type, Value)
|
Sets the internal memory of SMBX. |
| NPCMemSet,NPC ID,Offset,Value,OPERATION,Length,Data type | for k,v in ipairs(NPC.get(NPC ID, -1)) do
v:mem(Address, Data type, Value);
end
|
Sets the memory of all NPCs in the level of the given type. |
| PlayerMemSet,-,Offset,Value,OPERATION,Length,Data type | player:mem(Offset, Data type, Value)
|
Sets the memory of Player 1. |
| SetHearts,-,Hearts,-,-,Length,- | player:mem(0x16, FIELD_WORD, Hearts)
|
Sets the amount of hearts the player currently has to HEARTS. |
| SetHits,NPC ID,Section,Damage,-,Length,- | for k,v in ipairs(NPC.get(NPC ID, -1)) do
v:mem(0x148, FIELD_FLOAT, Damage);
end
|
Set all NPCs that have the given NPCID to have taken HITS amount of hits. |
| SetVar,-,Operation,Value,-,Length,Variable Name | See SaveData and GameData tables | Manipulates permanent user variables |
Audio commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| SFX,-,Sound ID,-,-,Delay,- | SFX.play(Sound ID)
--or
SFX.play(Filename)
|
Plays one of SMBX's sound effects if you specify a number or a custom sound effect from the level folder if you specify a filename string. |
| PlayMusic,-,Section,-,-,Delay,- | playMusic(Section-1)
--or
Audio.MusicChange(Section, Filename, Fade In Delay)
|
Plays the music from Section. |
Advanced/Misc commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| LunaControl,Type,Boolean,-,-,Length,- | --When type = 2
Graphics.activateHud(Boolean)
|
|
TheXTech only commands
| Details | ||
|---|---|---|
| Autocode | Lua | Notes |
| OnEvent,-,-,Boolean for once,Event,Length,Event Name | function onEvent(calledevent)
if calledevent == "Event Name" then
--Your code here
end
end
|
Attach codes in the triggered event. |
| PlaySFX,-,Index,Loops,Volume,Delay,Filename | SFX.play(Sound ID, [Volume, Loops, Delay])
|
Play a Sound Effect with more parameters like Loops, Volume and Delay. |
| StopSFX,-,-,-,-,Delay,Filename | local a = SFX.play(Filename)
a:stop()
|
Stop a Sound Effect. |
| RunCheat,-,-,-,-,Delay,Cheat String | Misc.cheatBuffer("Cheat String")
|
Set a Cheat Code. |
| ShowLevelName,-,X,Y,Font Type,Delay,- | Text.print(Level.name()[,Font Type],X,Y)
|
Display level name at the specific coordination. |
| ShowLevelFile,-,X,Y,Font Type,Delay,- | Text.print(Level.filename()[,Font Type],X,Y)
|
Display level filename at the specific coordination. |