Skip to content

Getting Started

Preface

The following should be considered before you start using the SDK to make Lua scripts:

  • There is a forum called "General" in "SDK" where you are free to ask for help. Do not be afraid to ask questions or ask for help.
  • The SDK Lua module is very different from other providers. Copy and pasting Lua scripts will only lead to disaster. If you are pasting a Lua script yet do not understand why/how it does what it does, you will have a hard time.
  • There are a ton of disabled scripts that come in your latest.zip file. There are also releases found on the forum. Reading through these pre-made scripts is the best way to learn.

My First Script

Every fantasy.moonlight script must:

  • Have a local table named exactly as the file name.
  • Return the local table at the end of the file.

For example, this is example.lua:

-- Our label table.
local example = {}

-- Returning our local table.
return example

Notice how the local table is called example and the file is also called example.lua. Try not to confuse the SDK.

Whenever a fantasy.moonlight script is successfully loaded, it will call PostInitialize. Therefore, to handle this callback, we will create a function for it.

-- Our label table.
local example = {}

-- PostInitialize Callback
function example.PostInitialize()
    moonlight.log( "Hey! I am alive! I am an example.\n")
end

-- Returning our local table.
return example

As you can see, I created a receiver for the callback and inside the callback I called the function moonlight.log. When you load this script, you will see the following in your console box:

Hey! I am alive! I am an example.

Congratulations! You made your first script.

Creating an Empire

fantasy.moonlight scripts can access multiple callbacks at once within a single script file. All you need to do is create the receiver for the callback. Let's expand our script from earlier as an example:

-- Our label table.
local example = {}

function example.PostInitialize()
    moonlight.log( "PostInitialize called!\n")
end

function example.OnEventFired( name, data )
    moonlight.log( "OnEventFired called!\n")
end

function example.OnCreateMove( ecx, edx, input, cmd  )
    moonlight.log( "OnCreateMove called!\n")
end

function example.OnEntityCreate( entity )
    moonlight.log( "OnEntityCreate called!\n")
end

function example.OnEntityDelete( entity )
    moonlight.log( "OnEntityDelete called!\n")
end

function example.OnOverrideView( fov )
    moonlight.log( "OnOverrideView called!\n")
    return fov
end

function example.OnMoonlightTick( fov )
    moonlight.log( "OnMoonlightTick called!\n")
end

-- Returning our local table.
return example

Again, 1 script can create a huge empire of features. Check Callbacks for a list of callbacks and how they work.

Debugging/Testing

If you press [HOME] ingame and click on the fantasy.cat logo, there is an ingame menu that will allow you to reload scripts you are working on.

You can edit your script in your IDE (Visual Studio Code, Notepad++, etc..), then reload your script ingame to see the changes immediately.

If your script is loading but doesn't seem to be working, you can plant debug messages in between code. For example:

-- Our label table.
local example = {}

-- PostInitialize Callback
function example.PostInitialize()
    print( "1" )
    moonlight.log( "Hey! I am alive! I am an example.\n")
    print( "2" )
    moonlight.vars.add( "Hello World", 1337 )
    print( "3" )
end

-- Returning our local table.
return example

This script will print 1, 2, 3 after each transaction. Doing something like this an easy way to debug where your code stops at.

If there is an error that the Lua compiler will pick-up, it will notify you in the console box. In any other case, you will have to debug the code on your own.