Gui.get_screen_position() returns the wrong position if called after init() (SOLVED)

I’ve run into a strange issue where gui.get_screen_position() works if it’s called on init() and then re-used. But if it’s called after init(), and the screen has been resized, it returns the wrong position.

Please see a minimal example: Archive.zip (38.1 KB)

Scenario 1:

  1. Click the screen
  2. The rectangle travels from and to the right place

Scenario 2:

  1. Resize the window
  2. Click the screen
  3. The rectangle travels from and to the wrong place

On init() I store the get_screen_position() of the rectangles, and if they are re-used it works(!). I demonstrate this by firing a rectangle automatically after 3 seconds, which always move from and to the right place, regardless of screen resize.

I noticed that @AGulev has posted something that might solve this here, but I thought I’d see if this is expected behaviour or if I’m missing something.

If you wanna set screen position to a node, you need to convert screen position into node local space position.
Unfortunately, for now, we don’t have API method for a simple set of the screen position, that’s why I wrote this temporary set_screen_position() solution, this method convert screen position to local position taken into account all transformation matrix that you need to apply.

3 Likes

So this is what I’ve bumped into? How can the very strange behaviour above be explained, though; it WORKS if the original positions are used, rather than the current ones.

Because before you change screen size most of transformation matrix values == 1 and

any_number * 1 = any_number

(of course, this is a simplification, but a sense the same)

So to fix it it would be safest to use your temporary solution? I was considering storing all positions on init() and re-using them. Don’t hurt me.

My solution will work and that’s what I recommend to use in your case.

I’ve tried, and failed, to use it in the example project I posted above. This is what I’m trying to do:

  1. Send position from a GUI in Collection1.
  2. Receive position in a GUI in Collection2 and use it to position a cloned node.

Is this possible? Or would I have to have access to all nodes? gui.set_screen_position() doesn’t seem to work.

The project in case you want to have a look:
GuiScreenPosition.zip (828.1 KB)

No matter what I do, gui.set_screen_position() and gui.screen_pos_to_node_pos() return
vmath.vector3(inf, inf, 0).

Could you please explain what should I see in your example? I open it and it works, but no reaction to clicks.

Thanks! Check out the first example I posted above. A rectangle node should move from the position of another rectangle node in a different collection. The reason nothing happens in the most recent project is that the rectangle is set to a position of vmath.vector3(inf, inf, 0), wherever that may be! :slight_smile:

The first project does not use set_screen_position() module, I don’t know how to solve your problem without this module.

That makes two of us! So what I’m trying to achieve is currently impossible? The common use case here is:

  1. Currency is rewarded from a node in GUI1
  2. GUI2 creates a coin from GUI1’s node position and sends it to the coin stash in GUI2

When I use your module the position always comes back as vmath.vector3(inf, inf, 0), which is why it doesn’t work.

As I told you, it is possible using set_screen_position() module. Could you pls create an example where I can reproduce your problem?
I can’t reproduce (inf, inf, 0) issue in GuiScreenPosition.zip example

Okay, coming right up.

This example only tries to position a cloned node in the same place as a node from a different collection. The resulting position of the cloned node is (inf, inf, 0).

GuiScreenPosition2.zip (827.8 KB)

The expected result would be a cloned blue rectangle moving on top of the red rectangle.

1 Like

Thank you for your report. I’ve made fixes for the set_screen_position module (sorry for that inconvenience).
Now it should work.

2 Likes

It does indeed! Thanks so much for the speed at which you operate. Amazing. :raised_hands:

2 Likes