Position are incorrect after screen resize

Hi everybody!

I have a problem with getting position of node after screen resize.
On current node starts animation (I set the position of the animation by gui.get_screen_position() and I have read that this is wrong, but get_position isn’t fit here too) and after resizing this animation shifts… How get the actual position of node after resizing?
Thanks!

gui.get_position() should always give you the position of the node in the dimensions set in game.project where 0,0 is bottom left corner and width,height (as specified in display width and height in game.project) it top right corner.

If you position a node in the top right corner and set node anchor and pivot appropriately the node should remain in the top right corner regardless of how you resize the window and the position should always be the same.

Are you trying to sync gui node positions with game objects or something?

I’m trying to play animation on the node after resizing :slight_smile: Cause it’s shifted after resizing far from node.

“Position should be same” - here I kind of confused. Defold has get_screen_position() which, as I understood, return position of node due to resolution. For example, node has screen_position x=100, y=100. And set pos to my animation. Resolution now 700x300. And then I resize window to 1000x500. Screen pos of my anim still will be x=100, y=100. Am I right?
And get_position (again, like I’m understood) it’s a stable position in gui. If for example in gui my node places outside of game window (for example she added and positioning in window programmatically), and I set her position for my animation using set_position, my animation will be outside game window too. Am I right here?

I’ve just trying to get actual position of node in current moment, and it didn’t work anyway… ((

Yes, gui.get_screen_position(node) will give you the position of a node based on the current resolution while gui.get_position(node) will get you the position of the node translated to the resolution specified in game.project.

Example:

  • A project with Display Width and Height set to 960 and 640 respectively.

Screenshot 2021-07-05 at 23.23.12

  • A single box node positioned in top-right corner with anchor set to top-right:

  • In the update function I print both gui.get_position() and gui.get_screen_position():
function update(self, dt)
	local node = gui.get_node("box")
	print("Position: ", gui.get_position(node), "Screen position: ", gui.get_screen_position(node))
end
  • When the window is opened it prints:
DEBUG:SCRIPT: Position: 	vmath.vector3(960, 640, 0)	Screen position: 	vmath.vector3(960, 640, 0)
  • And when the window is resized (made wider) it prints:
DEBUG:SCRIPT: Position: 	vmath.vector3(960, 640, 0)	Screen position: 	vmath.vector3(1760, 640, 0)

So as you can see, gui.get_screen_position() will give you a position with the size of the current window while gui.get_position() will give you a position that always matches the size of the window as it was set in game.project.

You are animating using gui.animate() right? If yes, then you should still give position in the original window dimensions as set in game.project. You should not worry about giving a position to gui.animate() that reflects current window size.

Using which function?

2 Likes

Thank you very much for so great explanation and not giving up with me :slight_smile:
So get_position() it’s not needed variant. But if get_screen_position() returns a position in current window with it size, why it’s not match after resizing? Or it’s not proportional?

There is a complex animation.

            gui.set_position(spine_node, start_pos)
            gui.play_spine_anim(spine_node, "HandTutrialDrag_Start", gui.PLAYBACK_ONCE_FORWARD)
            gui_animate(spine_node, "position", destination_pos, gui.EASING_OUTSINE, speed)
            gui.play_spine_anim(spine_node, "HandTutrialDrag_Finish", gui.PLAYBACK_ONCE_FORWARD)

And in second animation I should give it position one of my node as a start_pos and other node as s destination_pos

local spine_node = gui.get_node("spine")
local start_pos = gui.get_screen_position(gui.get_node("item"))
local dest_pos = gui.get_screen_position(gui.get_node("c_queue1"))

What if you use gui.get_position() here instead?

Update: it’s not outside, it’s in (0:0). So I assumed that this coords are local to parent. So I need a function, that calculates this coords as local for different parents.

I’d try it, but in this case animation placed outside of game screen. (( That’s why I’m looking for some alternatives which will work correctly.

Could you please create a small example in an empty project where you reproduce issue and share with us.
That will make it easier to check what’s going on and it helps to find a solution.
Thank you!

1 Like