This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Shows correct position, but node ends up at different position

jasonnandujasonnandu Posts: 3Member
edited June 2020 in GUI

I'm trying to make a card game using Godot 3.2. I have a "tabletop" scene, which has a ColorRect as its root node, and a bunch of MarginContainers, VBoxContainers and HBoxContainers as children holding my UI elements. I've instanced this scene on top of a CanvasLayer in my "main" scene (Node2D), and I'm having my cards (Area2D's) instance on top of that as they are drawn one after the other.

To set the positions of the spawned cards, I'm using $node.rect_position on the control nodes, and when I draw the cards this is working just fine. Here is an example:

drawncard.position = $gui/CanvasLayer/tabletop/.../MarginContainer.rect_position drawncard.position += $gui/CanvasLayer/tabletop/.../MarginContainer/TextureRect.rect_position drawncard.position += Vector2($gui/CanvasLayer/tabletop/.../MarginContainer/TextureRect.rect_size.x/2, $gui/CanvasLayer/tabletop/.../MarginContainer/TextureRect.rect_size.y/2)

This spawns my card right in the center of my play area (TextureRect), which is exactly where I want it.

When a certain condition arises, I want a pair of cards from the main node to be reparented to another node (TextureRect), and moved to the center of the new node. I'm using signals, remove_child(), add_child() and the exact same code above, but pointing to a different node. Here is where the problem arises. When I run the code, I get the following:

  • screen_size = OS.get_window_size() correctly returns 750x1334 px, which is my target resolution,
  • drawncard.position correctly returns (0, 507) px, which is where I want it moved,
  • but the card is at a completely different position, looks to be around (0, 1050)px, too far towards the bottom of the game window.

WHY IS THIS HAPPENING? I'm getting the same results on an iPhone 8 running iOS 13.5, built using Xcode 11.5. Debugger prints out (0, 507), but that definitely is not where the card's at.

Further more, if I set card.position = OS.get_window_size(), it does NOT end up at the right bottom corner of the screen.

What am I doing wrong? I'm very new to Godot Engine and game development in general, so I would appreciate it if y'all could point me in the right direction. My guess is that using control nodes to set positions of non-control nodes is not the best practice.


  • jasonnandujasonnandu Posts: 3Member

    I'm just attaching for reference a card set to card.position = OS.get_window_size()

    print(card.position) prints out (750, 1334), which it clearly is not.

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin
    edited June 2020

    What does card.global_position return?
    Additionally, in the project settings, what is your aspect/stretch mode set to? Maybe something there is causing the issue.

    I have not had this issue myself, but I mostly do 3D game development. My guess is that either something is not scaling correctly, the pivots/origins are getting messed up when the position changes, or something else. Given OS.get_window_size() is also returning strange results, I'm thinking it may be related to the aspect ratio and/or window scaling mode.

    Edit: Also, welcome to the forums :smile:

  • jasonnandujasonnandu Posts: 3Member

    Hey, thanks for replying!

    card.global_position returns (10, 1024) after moving . The position of the control node this value is being drawn from is certainly 0 on the y. You may be right about pivots/origins getting messed up.

    Aspect/stretch mode was set to 2d/Expand. I was using the control node ColorRect as the root node so the UI layer to expand to the user's screen size. I tried Viewport/Keep, Viewport/Ignore, 2d/Keep and 2d/Ignore. Still the same error.

    I set the position of the initial drawn cards to OS.get_window_size(), for ease of debugging. OS.get_window_size() still returns the correct value, but the cards spawn about 3/4th of the way to screen size.

    Is there nothing else to do but start over? But I've made so much progress already, I'd hate to waste all that time.

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    Hmm, I don't know what is going on then. Can you make a minimum replication project? A simple and barebones project that shows the error? That way, maybe we can figure out what is going on and fix it without needing to reset/restart the project.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.