There are a couple options for that:
Message passing
You can define your own custom messages with an ID and a table of data sent with it. This is good for when you just need to set or change some value without needing a response:
-- In some script
msg.post("player", "add_points", { amount = 100 })
-- In player script
function on_message(self, message_id, message, sender)
if message_id == hash("add_points") then
self.score = self.score + message.amount
end
end
Read more:
Lua modules
This is essentially a shared global table that any script can import and use. It’s also commonly used just to store common functions that lots of scripts need:
-- In /game/my_module.lua
local M = {}
M.player_name = "Potota"
function M.square(x)
return x * x
end
-- locals are private and can't be directly accessed by scripts
local score = 2
function M.get_score()
return score
end
function M.square_score()
score = M.square(score)
end
return M
-- In some script
local my_module = require("game.my_module")
print(my_module.square(5))
my_module.square_score()
print(my_module.get_score())
my_module.player_name = "BlackFlyPress"
print(my_module.player_name)
Read more:
Script properties
Scripts can define their own custom properties that appear in the editor and can be read and written with go.get()
and go.set()
. They’re mainly used to change initial settings, like the strength of an enemy or the FOV of a camera.
-- In /main/can.script
go.property("health", 100)
go.property("target", msg.url())
function init(self)
print(self.health)
end
-- In some script
local current_health = go.get("/can#can", "health")
go.set("/can#can", "health", current_health - 10)
Read more: