0 votes

I have the following UI with playing cards being dealt as shown by the red arrow:

I’m trying to make them be dealt relative to the “Deal” button towards the center of the screen. So I have something like this on button click:

    deck.add_child(card)

    var pos1 = deal_btn.rect_position + Vector2(-200, 0)
    print_debug(deal_btn.rect_position)
    print_debug(pos1)
    print_debug(deck.rect_position)
    tween.interpolate_property(card, "rect_position",
            card.rect_position, pos1, 0.5,
            Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
    tween.start()

The deck (which is the green square), is giving me a position of (0,0), whereas the deal_btn is giving me a reasonable value of 819.200012, 512 and pos1 is being calculated relative to it at (619.200012, 512).

The problem seems to be with deck being at (0,0). card is added as it’s child and is also at (0,0)... so I’m animating from (0,0) to (619.200012, 512) - off screen!

deck is a child of a control node… maybe that’s the reason? (as child nodes of control nodes don’t get to decide their position). See same link above - 2nd image for scene tree.

How can I get the “absolute” position of the deck in the screen (not relative to where the control node decides to put deck)? I assume I can then use this to do some vector math and get the right position.

(Added screenshot of Deck's parent's anchors).

asked Jun 14 in Engine by justin (25 points)
edited Jun 14 by justin

1 Answer

+1 vote
Best answer

How can I get the “absolute” position

Use card.rect_global_position instead of card.rect_position. It's the same for Node2D: global_position is relative to the world origin (i.e. usually the top-left corner of your window), position is relative to the parent node.

answered Jun 14 by njamster (8,874 points)
selected Jun 14 by justin

Thanks njamster!

I had found rect_global_position shortly after posting this but after trying it - I ended up with no cards showing up ... so I assumed I was setting a position way off the viewport.

Turns out, I missed changing the tween's second argument to also be "rect_global_position". So animating from the deck to the deal_btn (almost what I want) is:

    tween.interpolate_property(card, "rect_global_position",
    card.rect_global_position, deal_btn.rect_position, 0.5,
    Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)

Just a matter of adding some offset now.

Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.