Vik
January 12, 2019, 12:08pm
1
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?
Pkeod
January 12, 2019, 1:22pm
2
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
Vik
January 12, 2019, 2:12pm
3
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
britzl
January 12, 2019, 6:41pm
4
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.
Vik
January 12, 2019, 6:52pm
5
just learning/testing. if object A has a string and you need in object B, how can I do it?
Vik
January 12, 2019, 8:46pm
7
in the second option, how can I get the string from de hash?
britzl
January 12, 2019, 10:15pm
8
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:
local M = {}
local values = {}
function M.add(key, value)
assert(key, "You must provide a key")
assert(value, "You must provide a value")
values[key] = value
end
function M.remove(key)
assert(key, "You must provide a key")
values[key] = nil
end
function M.get(key)
assert(key, "You must provide a key")
return values[key]
end
This file has been truncated. show original
And going the other way from a hash to a string again:
go.property("url", msg.url())
go.property("color", vmath.vector4(1, 1, 1, 1))
go.property("rotation", vmath.quat())
local lookup = require "factory_and_properties.lookup"
local width = tonumber(sys.get_config("display.width"))
local height = tonumber(sys.get_config("display.height"))
function init(self)
print("My name is " .. lookup.get(self.key))
print(self.cool and "I'm cool" or "I'm not cool")
go.animate("#sprite", "tint", go.PLAYBACK_LOOP_PINGPONG, self.color, go.EASING_INOUTCIRC, 2)
--go.animate(".", "position", go.PLAYBACK_LOOP_PINGPONG, self.move_to, go.EASING_INOUTCIRC, self.move_speed)
go.animate(".", "rotation", go.PLAYBACK_LOOP_PINGPONG, self.rotation, go.EASING_INOUTCIRC, 2)
msg.post(self.url, "thing_created")
self.direction = vmath.vector3(math.random(-10,10) / 10, math.random(-10,10) / 10, 0)
end
5 Likes
Vik
January 12, 2019, 10:25pm
9
nice, I understand the aproach now. Thanks