0 votes

Greets!

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 (321 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 (10,932 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)
    print(roll)
    if roll <= 50:
        print("ok")
    if roll > 50 and roll <= 75:
        print("nope")
    if roll > 75 and roll < 100:
        print("okokok")")

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