How do you access values from other scripts? (SOLVED)

if my script A has a variable

variable="hello"

how can I access that from another script? is using messages? that aproach is slower than something like

print(gameobject1.variable)

?
thanks

edit, and how do you access to another object functions?

You can use a Lua module to exchange data like this fast. So you store the data in the module instead of in the script’s self.

For shared functions, you also want to be using a Lua module.

Make a .lua file and require it

local my_module = require("path.to.my_module")
print(my_module.my_value)
my_module.my_function()

my_module.lua

local M = {}
M.my_value = 7
function M.my_function()
  M.my_value = M.my_value + 1
  print(M.my_value)
end
return M

There are still many cases where you want to be using message passing instead.

4 Likes

but if the 2 objects need info of the other object (variables changing each frame, for example) then , the only aproach is message passing? ( the function on_message will be huge )

I see the gameobject properties , but I cant set a string property

thanks

Game object properties is a clean way of exposing values on scripts that are modifiable from the editor and at run-time as well as accessible using go.get().

But yes, you can’t use strings. What do you specifically want to do? What kind of string would you like to read? I’m sure there is a good solution to your problem.

just learning/testing. if object A has a string and you need in object B, how can I do it?

A few options:

  1. You can pass it via message
  2. Use a hash property and do lookup
  3. Share state data somewhere, like a module

in the second option, how can I get the string from de hash?

I think I have an example somewhere… ah, yes, here:

Lines of interest:

Creating a string, hashing it and adding it to a look-up table:

Look-up table:

And going the other way from a hash to a string again:

5 Likes

nice, I understand the aproach now. Thanks