Detecting rockets outside of the screen

It is OK to learn how the signal connect things works with the visible notifier, thanks.
But, as long as the rocket only goes from left to right, maybe it is simpler to do just a position check?

func _process(delta):
    var width = get_viewport_rect().size.x
    if (global_position.x >= width + 100):
        queue_free()

Thanks!

1 Like

I am a beginner but I think signals should be more optimized.

Using the _process(delta), you are:

  • getting the viewport’s size
  • checking if the rocket’s x position is greater or equal than the viewport’s x size + 100

And you are doing this in every frame, with every bullet.

Because signals are a built-in feature I assume they are faster.

1 Like

Hi @Pityke, thanks for your answer.

I think that an area2D have to check every frame for collision according to its mask to emit the event at the right time.

It should be more CPU demanding than just a float comparison and a viewport size read IMHO.

Honestly I don’t know, I’m a professional developper but I’m new to Godot so I’m just guessing :slight_smile: Only a benchmark would tell the truth ^^

Thanks

1 Like

I think that both approaches are valid, but for the speed, I’m not sure what is the best because:

  • using a signal is more complex (as an “internal process”) than a float comparison, but as it’s an engine feature, it’s coded and compiled in C++ so the result code will be faster than using GDscript
  • the float comparison is more simple , but it’s a GDscript, so not as fast as compiled C++

For the code readability, I’d like the float comparison
For the “best coding practices” , I’d like the signal approach, because signals are an essential part of Godot and should always be preferred when possible

Privacy & Terms