Root UI nodes to a 2d Static Position
Asked Answered
M

11

0


Been clueless as what to google for this- no results so far so here we go.

Looking for alternative ways to "staple" or "root" control nodes, in this case buttons, to a static location in a 2D scene. The way I'm doing it in this game is to use the process function to move the group of buttons every frame the opposite direction the camera moves. I need buttons to stick around a tower in this tower defense game. My method of course has all sorts of problems:

  • it's gross as it uses the _process function
  • it needs more code if I want it to work with the camera changing at different zoom intervals
  • it needs even more code to know if the camera is stuck

Am I overlooking a much simpler alternative? Thanks in advance.

Marble answered 10/4, 2023 at 19:33 Comment(0)
M
0

So a canvas layer is fine if you don't need to click on buttons. I can get the buttons in the same place by having them as children under a node2D. They keep their position and scale, and if you place it further down the tree the buttons can appear over a canvaslayer above on the tree.

Marble answered 11/4, 2023 at 0:1 Comment(0)
S
0

You need to use a CanvasLayer as the parent.

Stefanistefania answered 10/4, 2023 at 20:12 Comment(0)
C
0

And if you want to keep them constant size (in respect to screen, regardless of zoom), intercept the zoom change and just scale each control individually to 1/zoom

Clitoris answered 10/4, 2023 at 20:35 Comment(0)
M
0

Cacilie Rounds Thanks guys. This is so cool! Without tons of code the button's parent node can be relocated to the tower using the canvas layer's offset parameter. Then in a circle I can place the buttons using a local position instead of a global position in relation to the tower. The "follow viewport" parameter on the canvas layer needed to be enabled.
With this solution the buttons scale with zoom and stay put on camera panning! Now the only mystery for me to solve is why on earth the actual click region has been relocated far away (the red dot is where the mouse needs to be to select that blue info button).

Marble answered 10/4, 2023 at 22:49 Comment(0)
C
0

Why do you need the offset thing? You could just put all button parents on the same canvas layer, position them exactly at tower coordinates, and let the "follow viewport" do the rest.

Clitoris answered 10/4, 2023 at 23:14 Comment(0)
M
0

Clitoris Yes I had the same thought so I did exactly that- I removed the offset and set the buttons to the exact position. I still have some ghost giving the actual "mouse over" area another offset. Silly ghosts.

Edit: I guess this is an unresolved issue that isn't something anyone has felt the need to fix, I guess there's an easy workaround I've yet to see.

Marble answered 10/4, 2023 at 23:21 Comment(0)
C
0

Marble Try it with some fresh vanilla button.

Clitoris answered 10/4, 2023 at 23:36 Comment(0)
C
0

Marble It's working fine in 4.0

Clitoris answered 10/4, 2023 at 23:57 Comment(0)
M
0

So a canvas layer is fine if you don't need to click on buttons. I can get the buttons in the same place by having them as children under a node2D. They keep their position and scale, and if you place it further down the tree the buttons can appear over a canvaslayer above on the tree.

Marble answered 11/4, 2023 at 0:1 Comment(0)
M
0

Clitoris Yes I saw they fixed it for 4.0 and for the 3.x they pushed it off to 3.6.

Edit: Got it working! Hurray!

Marble answered 11/4, 2023 at 0:2 Comment(0)
C
0

Marble So the solution to your problem was to just put the buttons into the scene? Who would have thought it. 🙂

Clitoris answered 11/4, 2023 at 0:29 Comment(0)
M
0

Clitoris lmao stop man I'm gunna cry.

Marble answered 11/4, 2023 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.