How gui node get same exact position in game object

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

is there something wrong with my code?

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.

so that’s the problem. So that’s mean I must use gui script in function on_message() to write that code to get the result?
sorry noob question

is there any video tutorial or web page tutorial how i can manipulate entire component in defold?

Yes. In your .gui_script file which you attach to your .gui you add code in on_message() to respond to the message you send from a .script file.

What do you mean by “manipulate entire component”? Components in general are described in this manual:

You also have specific manuals per component type (sprite, particle fx, gui etc). See the menu to the left.

2 Likes

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.

There’s also a good manual on message passing which I recommend that you read:

It covers an example of sending a score update from game objects to a gui, similar to what you are trying to do.

2 Likes

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.

2 Likes

Hello, I am already using your advice

lua module

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>

anyone have advice for this?

You need to require card_prop first, e.g.

local card_prop = require "path.to.card_prop"

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.

3 Likes

hmm i got this error

ERROR:SCRIPT: card/cardglue.lua:14: bad argument #2 to ‘vector4’ (vector4 expected, got nil)
stack traceback:
[C]:-1: 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

Which line of code is that? I would print the values before that line, e.g. card_id_pos. What do they say?

It’s in lua module

gui_node = gui.get_node(gui_node)
	gui.set_position(gui_node, card_id_pos)

You haven’t answered my questions. I assume it’s this line causing the issue.

gui.set_position(gui_node, card_id_pos)

The error states that it is expecting a vector, but the value provided is nil. Are you sure card_id_pos is not nil?

oh, sorry sir i don’t understand I thought the code line where is an error
yes, it’s not nil

What is the value of card_id_pos just before gui.set_position? You can check by using print(card_id_pos).

yes the value is nil

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>

but why I am already declare it in go script?

Can you show the go script?

cardglue.lua

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