Providing Addon Functionality
This guide will show you how to make your addon actually do something.
The rest of this guide assumes you followed Your First Addon. If you haven't, this page might not make much sense to you.
Calling In-Game Functions
In Stormworks Addon Lua, interacting with the game is done by calling functions in a server table. For example, to send a message to the chat, a typical Stormworks Lua addon would use server.announce("Title", "Message").
PythonToSW allows us to do the same through Python, just like so:
# ...
def on_start():
"""
Called when the addon starts (connects with in-game addon).
"""
addon.call(CallEnum.ANNOUNCE, "Title", "Message")
# ...The first argument to addon.call represents the server function we want to call in-game. Every single callable server function is present in the CallEnum enum.
The rest of the arguments represent the arguments we want to pass to the server function. You can see what arguments every server function takes in over at this link.
What About Functions That Return Something?
PythonToSW handles that too! If you call an in-game function that returns something, the in-game addon will relay it back to us. Take a look!
In-Game Callbacks
You probably noticed that we imported CallbackEnum in Your First Addon. This is where we use it.
PythonToSW also allows you to connect to in-game callbacks and trigger a function whenever an in-game callback is triggered. For example, if you want to listen for players joining, you'd do:
You can connect to callbacks whenever, whether it's before your addon starts or even during runtime. Ideally you should connect to them straight away before your addon starts however.
onTick cannot be connected to and is not apart of CallbackEnum. This is because it is called faster than HTTP can keep up with. However, as mentioned in Your First Addon, the Addon class comes with a custom on_tick event we can connect to as an alternative, like so:
Injecting Custom Lua Code
Sometimes HTTP can be too slow, or you just want to write some Lua code, PythonToSW supports this and makes it easy for you to do so!
There are two methods you can use:
addon.attach_lua_file(path): Reads the Lua file atpathand injects the code within into the addon.addon.attach_lua_code(code):Injects the provided code into the addon.
For example, if you had a file called helper.lua:
You can inject it into your addon like so:
This will append the code to the end of your addon. Your code can also interact with SWToPython, the core part of the in-game addon that bridges your Python addon with the game. Additionally, there's also Noir you can interact with, a framework that provides many utilities.
This MUST be done before addon.start() is called!
Calling Custom Functions
In the previous section, we created a function in foo.bar called myFunction, but that would be pointless if we couldn't call it. Luckily, we can.
To call a custom function, we can use addon.call_function instead of addon.call. We then provide a path to the function instead of a CallEnum.
Of course, we can also access anything the function returns. In the Lua code, the function returns 1, let's use that.
Easy!
Last updated
Was this helpful?