0 votes

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 onGasbodyentered(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 * getprocessdelta_time ( )

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

in Engine by (28 points)

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!

1 Answer

+3 votes
Best answer

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().

by (10,928 points)
selected by

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?

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.