How to pass variable from gui_script to main function? (SOLVED)

Hello, I’m new here and I’m wondering if it’s possible to pass some variable from the gui_script to the main script.

My ideia is to make a tetris like game, and when the user hits start, the game starts, but I’m having trouble finding a trigger to make the pieces start falling after switching guis. So I was wondering if it was possible to pass some variable from one script to another, something like a : start = true passed from the gui_script to my main script when the start button is clicked

Hi and welcome!

This is probably easiest made with message passing. You could also maybe use a shared Lua module, but this is what message passing is for!

Given that your script is called main and is in a game object named go (like the image)

You can then do msg.post("/go#main", "start") in your .gui_script

And in your .script you would check for it in the on_message

function on_message(self, message_id, message, sender)
  if message_id == hash("start") then
    -- Game should start
  end
end
3 Likes

Thank you. It worked and now I think I got a hold of message passing.