Skip to content

Custom Lua scripts when entering Somniel

DogeThis edited this page Nov 29, 2024 · 6 revisions

Cobalt can load .lua files at the root of your mod and execute them when entering the Somniel (more specifically, when MapOpening is executed).

For example, if you have a function called addUnits, then you need to put it in a file called addUnits.lua in your mod folder.

These files need to be unbundled, and the filename of your script will be used to call the function. The function HAS to be present, with the same capitalization, otherwise, the game will crash.

Warning

Try to come up with a unique name, as conflicts with other mods are not allowed/handled. Make sure to pick a unique name that has low risks of conflict with other mods, because the Lua system won't support two functions with the same name.

Include() and Log and all that stuff are functional and can be used as normal. Separate functions in the file being called by your main function are allowed.

The script should only have functions it uses (no duplicating the original script, this is not a file swap), with the function to be called having the same name as your file.

Example: I have addUnits.lua in my mod, therefore the Lua function that will be called by Cobalt is function addUnits().

Tips and tricks

Save you and your user's ears

If you are adding a lot of new units or emblems at the same time, consider using the WaitTime function such as

In addUnits.lua:

function addUnits()
        UnitJoin( "PID_スタルーク")
	WaitTime(1)
	UnitJoin( "PID_シトリニカ") 
	WaitTime(1)
        UnitJoin( "PID_ラピス" )
end

or use 'UnitJoinSilent' instead of 'UnitJoin'

In addUnits.lua:

function addUnits()
        UnitJoinSilent( "PID_スタルーク", "PID_シトリニカ", "PID_ラピス" )
end
Clone this wiki locally