Accessing Local Var from other Functions

Hey everyone, I have a question…

I am looking to access a Local Variable located in function on_input from my function update to do some math.

after a bit of code gymnastics and googling I have arrived at using debug.getlocal(), but am a bit unsure of how to utilize it or if its the best way to go about it. Is this the jam, or is there a better way?

thanks!
-Loroko

The self parameter can be used as a table to store whatever data you want between each of the lifecycle functions:

function init(self)
    self.space_pressed = false
end

function update(self, dt)
    if self.space_pressed then
        print("Space!")
    end
end

function on_input(self, action_id, action)
    if action_id == hash("key_space") then
        if action.pressed then
            self.space_pressed = true
        else
            self.space_pressed = false
        end
    end
end
5 Likes

That might be just what the doctor ordered. thank you very much! I will let you know how it works out.

1 Like

Right on! That worked perfectly! Thank you very much, that will no doubt open up many possibilities for me.

One last question, is it possible to pass Variables from one script component to another (without resorting to using Global Variables)?

Thanks again,
-Loroko

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)

image

Read more:

9 Likes

Great response. Should be linked against for future questions regarding global data! :+1:

4 Likes

Thanks for breaking all this down, and for giving use case examples for context! I’m sure this will help move things along and open up many possibilities with my game. Very appreciated!

-Loroko

1 Like