I'm working on my very first 2D game in Godot. Since I like merge games I'm trying to create one myself (merge two of a kind to get next better one).
So far I created a game Scene and filled it with a couple of ColorRect
to simply differentiate the header bar, main content and footer/menu bar.
- Node2D
- ColorRect (BackgroundColor)
- ColorRect (Header Bar)
- ColorRect (Merge Area)
- Control (Merge Slot 1)
- ...20 slots in total
- ColorRect (Footer Bar)
In my game script I fill these "merge slots" with instanced scene of the mergeable object:
game.gd
func draw_planets():
var planet_scene = load('res://scenes/Planet.tscn')
for i in range(20):
if (planets[i] != null):
var planet_instance = planet_scene.instance()
planet_instance.id = planets[i]
get_node("MergeArea/MergeSlot"+str(i+1)).add_child(planet_instance)
So far so good. Now I was trying to add the drag function to the Planets.tscn
script. This is the Planet scene:
- Node2D
- ColorRect (BackgroundColor)
- Label (shows id of planet)
- Area2D
I connected the input_event
of CollisionShape2D and followed a drag & drop tutorial to this point:
planet.gd
func _on_Area2D_input_event(viewport, event, shape_idx):
if Input.is_action_just_pressed('ui_touch'):
selected = true
func _physics_process(delta):
if selected:
global_position = lerp(global_position, get_global_mouse_position(), 25 * delta)
At first I was going crazy because the input event was never triggered. Then I came across some forum posts that mentioned "set colorrect mouse filter to ignore" and when I did this it worked. But I had to basically set the mouse filter of every other node to ignore which made me think: Is this the way to go? There must be a different solution to make this work.
Current result is: I can click the first instanced planet object and it follows my cursor but I can't click the other instanced scenes probably because I would have to set every planet instance to mouse filter ignore.
My goal: I would like to be able to drag and drop the instanced planets around and if a planet is dropped on a other planet it will be merged. (Example: Planet 1 is dragged on another Planet 1, they become Planet 2 and so on)
In game.gd
I habe an array that is filled planet id's or null. Tells the draw_planets function if the merge slot has a planet in it or is empty.
I would appreciate any kind of help. To get a better understanding I attached a screenshot of the current UI state. See is a very basic dummy. The green square with a 1 instead of a zero is just a test to display a different planet ID.
