Area 2D Question If ANybody Can Help

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By StsDevSquad

So basically I’m working on a fuel system , basically I have a area 2d some where in the scene , and when the player goes there it brings the fuel back up at a certain speed. I have it working but for some reason its only when I enter the area the fuel goes back up , but if Im in the area nothing happens . Ill post codes below

This is the code thats on my fuel station
extends Area2D

func _on_Gas_body_entered(body):
if (body.has_method(“fillFuel”)):
body.fillFuel()

and inside my Player I have a function called fillFuel the code is

func fillFuel():
fuel += 50 * get_process_delta_time ( )

The answer could be simple , and Im just beat , but any help is appreciated!!!

hey, just for future reference, you can format your code by selecting it and pressing the {} icon above the text box. It makes it a lot easier to read. Good luck with your game development!

Millard | 2020-12-05 20:35

:bust_in_silhouette: Reply From: jgodfrey

So, you want the fillFuel() function to be called in every frame as long as the player inside the Area2D? Here’s a few ideas to do that:

  • In the fuel station code, wire both the entered and exited signals
  • In the entered handler, set a flag to indicate that you’ve entered the area
  • In the exited handler, unset the flag, indicating you’ve left the area
  • in the _process() function, call the fillFuel() method if the flag is set

With that, the fuel station is responsible for continually calling fillFuel. You could also let the Player code handle the continuous fill with the fuel station only indicating when the fill should start and when it should stop.

To do that…

  • Add a boolean property to the Player indicating whether it should be filling the fuel (say fill_fuel)
  • In the Player’s _process function, if fill_fuel is true, call fillFuel()
  • Wire the same entered and exited events in the fuel station as above
  • In the entered event, set the Player’s fill_fuel flag
  • In the exited event, unset the Player’s fill_fuel flag

Either should work. Really, it’s just a difference in which script is responsible for continuing to call fillFuel().

Thank you for the response , I’m kind of new to programming in godot , is it possible if you can provide me with some sample code?

StsDevSquad | 2020-12-05 20:51

Untested, but something like this:

In the Player script

  • Add a global variable to track the fueling request
    var fuel_fill = false
  • In the _process(delta) function:
    if fuel_fill:
        fillFuel()

In the Fuel Station script

func on_Gasbody_entered(body):
    if "fuel_fill" in body:
        body.fuel_fill = true

func on_Gasbody_exited(body):
    if "fuel_fill" in body:
        body.fuel_fill = false

Be sure to wire both the entered and exited signals on the Area2D, and connect them to the above 2 methods respectively.

With that, when something with a fuel_fill property enters the Area2D, it’ll set the body’s fuel_fill property to true. In turn, that’ll cause the fillFuel() method to be called from the Player’s _process() function.

And, when something with a fuel_fill property exits the Area2D, it’ll set the body’s fuel_fill property to false. That’ll cause the fillFuel() fuction to stop being called in the Player.

You may need to season to taste, but that should get you close to something that works.

jgodfrey | 2020-12-05 21:19

Wow so simple your a genius , I highly appreciate your knowledge!!!

StsDevSquad | 2020-12-05 21:33