Godot 4.4.1
Question
I’m working on a UI layout for a card game. I have a sub viewport with my board. The board is a node2D that allows for cards to be dragged around. When the board wasn’t in a sub viewport, it worked fine, but now I think the global mouse position is translating incorrectly to the subviewport.
Specifically, my raycast is getting a card to the left of my mouse click. It then snaps to my actual mouse position and words as intended. Here are some methods in my board node (which contains a camera2D used for the sub viewport).
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
var card = raycast_check_for_card()
if card and card.active:
card.select()
card_being_dragged = card
var mouse_local = get_viewport().get_mouse_position()
drag_offset = mouse_local - self.to_local(card.global_position)
if card is FormationCard:
_3d_view.set_drag_overlay("formation")
elif card is ActionCard:
_3d_view.set_drag_overlay("action")
func raycast_check_for_card():
var space_state = get_viewport().world_2d.direct_space_state
var parameters = PhysicsPointQueryParameters2D.new()
var mouse_pos = self.to_local(get_viewport().get_mouse_position())
parameters.position = mouse_pos
parameters.collide_with_areas = true
parameters.collision_mask = 1
var result = space_state.intersect_point(parameters)
if result.size() > 0:
var card = result[0].collider.get_parent()
if card is Card:
return card
return null
Am I approaching this incorrectly? I also have “handle input locally” checked on my sub viewport. My sub viewport within the board (for a 3D scene) is working perfectly still.
Thanks in advance!
What problem were you solving with the SubViewport?
Hm.. That’s a great question. I guess my thought was that I would need to put that 2D scene into some kind of container to scale it into an HBoxContainer. But now you’re making me question the decision to use sub viewport. Would a regular container or aspectratiocontainer do the job? I was actually able to fix my problem last night, but there is probably a lot of unnecessary translating if a regular container would work the same.
For a bullet hell game, yes you will. But since you typically spawn enemies outside the screen anyway, why does it matter if the player can see the whole screen as long as they stay centered in it?
As far as I know, SubViewportContainer is the only Control that can do that for you.
This is not a bullet hell shooter. It is a card game with a board. So there are two hands of cards and a board of pieces in the viewport (therefore it’s really important to see all of the board). So you’re thinking sub viewport is the right choice?
Sorry I confused this with another thread about a SubViewport and a bullet hell game.
No, I think you should see if you can do it all with Control nodes. No need for Node2D nodes at all. Alternately, you can just make everything in the game except the UI Node2D nodes. But combining the two, while possible is going to be more complicated.
I agree it would be great to use all control nodes and not mix with 2D, but my understanding was that I wouldn’t be able to drag around cards and use tween to animate the hand adjusting its spacing (when a card is played) if I were to use control nodes. Is that assumption wrong?
You can definitely tween Control nodes and drag-and-drop them.
So I did some more research and I see what you’re saying about the drag-and-drop that is built-in. However, it seems a little clunky. Using the built-in system, I can drag-and-drop, but my card will remain in its initial location until I drop it (from what I understand there will be a ghost of the node following the mouse). This breaks the illusion that I’m actually dragging the card (in my mind). Ideally, the hand will reposition each card when the card being dragged exits the area. I’m starting to see how I could achieve this functionality with built-in dragging, but I’m still not sure if using 2d nodes or control nodes would be better (i.e. less work).
Furthermore, if I’m, say, using an hboxcontainer for my hand, removing a node would instantly reposition my cards. I’m struggling to see how a tween could ease that movement. Thanks for all of your replies btw, this is very helpful
1 Like
The first game I tried to make in Godot like 3 years ago was a card game. I was doing it for a game jam and had been using Godot a week or so. It was a nightmare. I know Godot a lot better now, but I still haven’t gotten around to tackling a card game again.