How to play different functions inside another function?

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

Greets!

Meaning i got different function like _ready, _physics_process and _roll_a_dice, but to be executed only in my on_area2D_body_entered. How could i manage that please?

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

jgodfrey | 2020-02-15 22:21

:bust_in_silhouette: Reply From: jgodfrey

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.

Blocked. Look my try with the four func i want to include:

func _on_Area2D_body_entered(body):
	_ready()
	    randomize()
	_physics_process(delta)
	    if Input.is_action_just_pressed("ui_right"):
		   _on_action_pressed()
	_on_action_pressed()
	    roll_a_dice(1, 100)
	_roll_a_dice(minimum, maximum)
	    var roll = randi() % (maximum-minimum+1) + minimum
	    print(roll)

But i got the identifier ‘delta’ wich isn’t declared in the current scope. How do i declare it?

Syl | 2020-02-15 22:47

You definitely have some misunderstandings here, but I don’t really understand what you’re trying to do.

A specific node in Godot can have a single script attached to it. In that script, there can be a number of “built-in” functions the engine will call automatically if they exist, as well as any number of custom, User-created functions.

For example _ready, _process, and _physics_process are a few of those built-in functions. If your script has those functions, the engine will call the automatically at the appropriate times. If your script doesn’t have them, that’s fine too - they’re simply not called.

But, a single script can only have one, single _ready function, or _process, or any other built-in function.

You can’t create another copy of those functions inside other functions, as you seem to be attempting to do in the posted code.

Maybe start by explaining the problem you’re trying to solve. That’ll likely lead to a proper solution, that may be very different than what you’re currently thinking.

jgodfrey | 2020-02-15 23:02

Ok, here’s the misunderstanding: thought that built-in function could also be included in other function like the custom you wrote as your exemple.
But how could i include them so that i got a dice roll in my area2D_body_entered function?

Syl | 2020-02-15 23:12

Also, the code structure you posted here:

Any help to make random result? - Archive - Godot Forum

Is more what you’re looking for. How/why did you get from that to the code you posted above?

jgodfrey | 2020-02-15 23:13

Cause i wanted to include that random result in my are2D_body_entered.

Syl | 2020-02-15 23:16

In the other post, you had a roll_a_dice function. Assuming you have that function, you can simply call it from inside the _on_Area2D_body_entered() function. So, this (for example):

func _on_Area2D_body_entered(body):
    roll_a_dice(1, 100)

jgodfrey | 2020-02-15 23:16

Yes, but now, dunno why, my ‘roll_a_dice(1, 100)’ from func _on_action_pressed(), isn’t declared in the current class.

func _ready():
	randomize()

func _physics_process(_delta):
	if Input.is_action_just_pressed("ui_right"):
		_on_action_pressed()

func _on_action_pressed():
	roll_a_dice(1, 100) #Not declared, though it worked fine that way with the test in the other post.

func _roll_a_dice(minimum, maximum):
	var roll = randi() % (maximum-minimum+1) + minimum
		 
func _on_Area2D_body_entered(body):
	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")")

Syl | 2020-02-15 23:51

You’re calling roll_a_dice, but your function is named _roll_a_dice. Note the difference in the leading underscore. Those need to be the same.

jgodfrey | 2020-02-16 00:09

Oook, but now the roll isn’t declared in the current scope of area2D_body_entered, though there’s the previous func _roll_a_dice declaring it, doesn’t it?

Syl | 2020-02-16 00:32

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")")

jgodfrey | 2020-02-16 00:36

That works great! :smiley:

Many thxs to you!

Syl | 2020-02-16 00:48

Nice - glad you got it working.

jgodfrey | 2020-02-16 00:53