How do i only create a function after an if statement?

Hello, this is my first post here, i’m a beginner in godot and i’m making a simple game that whenever you kill an enemy they drop some dust and u have to clean it up, but when i try to send a signal that i’m holding a broom, the dust doesnt capture it.

So like, i wanna know what can i do? My problem is that i want to create a function only after i check if the player has the broom, and is inside the ‘CleanArea’, but i don’t know how can i do such a thing.

I have the broom as a characterbody2d and code on both the broom and the dust, but i can’t ‘connect’ those two, to make the dust disappear. Thx in advance

This desired outcome is difficult to understand. In general, it’s a good idea to share code when asking questions as this lets others get an idea of what the goal is. (Whitespace has meaning in GDscript, so place three backticks (i.e. ```) before and after the code to preserve formatting.) What’s also unclear to me is whether the broom is the player character, as the script attached to it inherits from CharacterBody2D, which is nominally used for something the player controls directly. At any rate, you probably don’t want to create functions at runtime. (I don’t even know if GDScript can do that, and the possibility of it being able to scares me.)

Assuming the broom isn’t the protagonist, the solution could be something like this:

  • Make the dust heap scene. Its root node should be an Area2D. Its script can be comething like this:
class_name DustHeap extends Area2D
  • Make the player character scene. Its root node should be a CharacterController2D. Add an Area2D node as a child and rename it CleanArea2D. Add the following script to CleanArea2D:
class_name CleanArea2D extends Area2D

var is_active : bool = false

func _ready()->void:
    area_entered.connect(_on_area_entered.bind())
    MessageBus.broom_active.connect(_on_broom_active.bind())

func _on_area_entered(area : Area2D)->void:
    if area is DustHeap and is_active:
        area.queue_free()

func _on_broom_active(active_state : bool)->void:
    is_active = active_state
  • Communicate activating/deactivating the broom state via a singleton. Add the script below to your project, and make it an autoload via the project settings tab:
@warning_ignore("unused_signal")
signal broom_active(active_state : bool)

You can now activate or deactivate the broom from any function anywhere in the code by using this call:

MesageBus.broom_active.emit(true)
1 Like

Thank you so much, in the meantime of the post till now, i figured it out, i put an area2d in the dust and in the broom, and the code checking if the player was standing on top of the broom, and if the broom was with the player, was on the player scene, each time the player was on top of the dust a signal was emitted, and if the player was with the broom and clicked the button, the dust would queue_free().

I was reading the post and I made a poorly job to explain what i wanted to, so sorry about that and thank you so much!