let’s get the point
I have game object with gui inside it . The real question it how the node to get position in same as game object. I already have the script on my game object but nothing happen.
function init(self)
msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
self.card_layout = gui.get_node("card_front","card_back")
local game_obj_pos = go.get_position("card")
gui.set_position(self.card_layout, game_obj_pos)
end
Yes, you cannot mix go.* and gui.* in the same file. If you need to position a gui node based on the position of a game object you need to send that go position as a message to the gui.
I mean like manipulate between component like game object to gui or vice versa. By the way many big thanks for your answer. I already search in youtube there are many tutorial about messaging in defold.
You can also use a lua module as a glue between gui and scripts.
Save your position in a variable of that module and require it in both gui and script.
local card_prop = {}
-- define game object and gui prop
local gui_node
local card_id
local card_id_pos
function card_prop.size(gui_node, card_id)
gui.get_node(gui_node)
go.get_id(card_id)
card_id_pos= go.get_position(card_id)
gui.set_position(gui_node, card_id_pos)
return gui_node, card_id
end
return card_prop
game object script
local obj_prop = "card/cardglue"
function init(self)
msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
card_prop.size("card_front", "card_back", "/card/card.go")
end
gui script
local gui_prop = require "card/cardglue"
function init(self)
end
this is the error
ERROR:SCRIPT: card/card_spawn.script:5: attempt to index global ‘card_prop’ (a nil value)
stack traceback:
card/card_spawn.script:5: in function <card/card_spawn.script:3>
I would also caution that the design you’ve shared still won’t solve your problem. Whenever you call a module function, it’s as if you are calling it in the current script. So card_prop.size() if called in a go script will still yield the same gui-related errors.
It would need to look something like this:
local card_prop = {}
-- define game object and gui prop
local gui_node
local card_id
local card_id_pos
function card_prop.store_card_id_pos(card_id)
card_id_pos = go.get_position(go.get_id(card_id))
end
function card_prop.size(gui_node, card_id)
gui_node = gui.get_node(gui_node)
gui.set_position(gui_node, card_id_pos)
return gui_node, card_id
end
return card_prop
You would need to call card_prop.store_card_id_pos() in the game object script, and card_prop.size in the gui script.
ERROR:SCRIPT: card/cardglue.lua:14: bad argument #2 to ‘vector4’ (vector4 expected, got nil)
stack traceback:
[C] in function vector4
card/cardglue.lua:14: in function size
card/card.gui_script:4: in function <card/card.gui_script:3>
looks like in go and gui have different vector properties
ERROR:SCRIPT: card/card_spawn.script:5: attempt to call field ‘store_card_id_pos’ (a nil value)
stack traceback:
card/card_spawn.script:5: in function <card/card_spawn.script:3>
local card_prop = {}
-- define game object and gui prop
local gui_node
local card_id
local card_id_pos
function card_prop.store_card_id_pos(card_id)
card_id_pos = go.get_position(go.get_id(card_id))
print(card_id_pos)
end
function card_prop.size(gui_node, card_id)
gui_node = gui.get_node(gui_node)
gui.set_position(gui_node, card_id_pos)
return gui_node, card_id
end
return card_prop
game object script
local card_prop = "card/cardglue"
function init(self)
msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
card_prop.store_card_id_pos("/card/card.go")
end