0 votes


Meaning i got different function like ready, _physicsprocess and rolladice, but to be executed only in my onarea2Dbodyentered. How could i manage that please?

in Engine by (325 points)

Not sure I follow. You just want to call a custom function from inside a _on_Area2D_body_entered() function?

1 Answer

0 votes
Best answer

If you're just trying to call a custom function from within another function (such as _on_Area2D_body_entered(), there's not much to it... Here's an example:

func _on_Area2D_body_entered(body):
    custom_function() # call the custom function

func custom_function():
    pass # do stuff here...

If you want/need to pass data into the custom function, just add function args as necessary.

by (16,844 points)
selected by

Oook, but now the roll isn't declared in the current scope of area2Dbodyentered, though there's the previous func rolla_dice declaring it, doesn't it?

You need to return the value from your roll function, and then store it when you make the call. Adjusted below:

func _roll_a_dice(minimum, maximum):
    return randi() % (maximum-minimum+1) + minimum

func _on_Area2D_body_entered(body):
    var roll = roll_a_dice(1, 100)
    if roll <= 50:
    if roll > 50 and roll <= 75:
    if roll > 75 and roll < 100:

That works great! :D

Many thxs to you!

Nice - glad you got it working.

changing a var when the function is called
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 Frequently asked questions and 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 [email protected] with your username.