sm64coopdx/docs/lua/lua.md
djoslin0 bf3127b3df
Some checks are pending
Build coop / build-linux (push) Waiting to run
Build coop / build-steamos (push) Waiting to run
Build coop / build-windows-opengl (push) Waiting to run
Build coop / build-windows-directx (push) Waiting to run
Build coop / build-macos-arm (push) Waiting to run
Build coop / build-macos-intel (push) Waiting to run
Add bytestring packets for mods to use (#866)
Adds network_send_bytestring(), network_send_bytestring_to(), and
HOOK_ON_PACKET_BYTESTRING_RECEIVE

This is as close to raw bytes as you can get in lua. It allows you
to efficiently pack as much data as possible into each packet.

The existing network_send() was built for ease of use, but is quite
inefficient when you want to send a lot of data. Each number or int
field in the table amounts to 9 bytes for the key and 9 bytes for the
value. (amounting to 18 bytes per value). Whereas with the new method
you can pack that information into 1, 2, 4, or 8 bytes depending on
your type's size.

---------

Co-authored-by: MysterD <myster@d>
2025-06-30 11:24:39 +10:00

3.1 KiB

Lua Reference

The Lua scripting API is in early development.

Expect many more things to be supported in the future.


How to install Lua mods

Lua scripts you make can be placed either the mods folder in the base directory, or in <SAVE FILE LOCATION>/mods

Save file locations:

  • Windows: %appdata%/sm64ex-coop
  • Linux: ~/.local/share/sm64ex-coop
  • MacOS: ~/Library/Application Support/sm64ex-coop

Tips

  • When developing Lua mods, run the game from a console. Lua errors and logs will appear there, but only if the game is launched with the --console launch parameter.
  • When a function requests a time parameter, it is almost if not always in frames.
  • You can use the print() command when debugging. Your logs will show up in the console.
  • You can create a folder within the mods folder containing multiple lua scripts as long as one script is called main.lua. Dynos actors can be placed inside this mod folder under <your mod folder>/actors/.

Sections

Guides

Important notes on player indices

Something important to realize is that the localIndex for each player is different (unfortunately).

So the order of gMarioStates[], gNetworkPlayers[], and gPlayerSyncTable[] is different for each player.

Luckily gPlayerSyncTable[] will automatically translate the player indices, so setting gPlayerSyncTable[0].example = 1 will set it for the correct player for everyone.

The globalIndex of each player is consistent among everyone connected. So if you absolutely need to sort things in order you will have to grab it from gNetworkPlayers[<LOCAL INDEX HERE>].globalIndex.

All of this is a holdover from when there were only two players. It was a reasonable idea back then.


Example Lua mods (small)

Example Lua mods (large)