💾 Archived View for gemini.spicyporkbun.online › text › godot_tips.gmi captured on 2022-03-01 at 15:03:52. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

Godot quick tips

Quick drag in Godot

Saw a lot of over-complicated draggable item implementations. Here is the simplest I could come up with.

If your item is/inherits from Control

Ensure that your Control takes mouse input (look in Mouse > Filter).

func _on_Control_gui_input(event):
  if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
    rect_position += event.relative

If your item is/inherits from Node2D

Add a KinematicBody2D and CollisionShape2D.

On the KinematicBody2D, tick "Pickable".

func _on_KinematicBody2D_input_event(_viewport, event, _shape_idx):
  if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
    position += event.relative

Resizing a Control element from the center

Normally, if you update a Control's rect_size, the element will grow from the top-left corner. I could not find a way to stop this, even by setting the anchors, pivot or grow direction.

To force it to appear as if the resize point is at the center of the object, you must update the margins when you scale to compensate:

func zoomBy(zoomSpeed):
  rect_size += Vector2(zoomSpeed, zoomSpeed)
  margin_left -= zoomSpeed
  margin_top -= zoomSpeed

Disabling an entire instance at start of runtime

You might want to set up a node in the scene tree but not actually instantiate until some trigger is activated. Hiding it is not enough, as you want to completely disable collision, scripts etc until the trigger.

You can do this by instancing the scene at runtime, but this means you'll have to set certain properties via script (e.g. the instance's position). This is less convenient than doing it in the editor.

Instead, you can set up the instance as normal in the scene tree. Right-click it and tick "Load as Placeholder".

Then your trigger can fill in the placeholder to fully spawn the instance:

func trigger():
  $placeholder.replace_by_instance()
  $placeholder.connect("some_signal", self, "some_func")

Note that the placeholder cannot hold signals; you'll have to set up connections via script.

The placeholder will run its _ready() script only on full instantiation.