I need help understanding and using "return"

Godot Version

4.1.1

Question

I am trying to crate a function that would return different results based on an array shuffle, the function would shuffle the array, select the first item and return said item, or a value corresponding to the item. I do not know how I could return to get that value.


var ability_list: Array = ['speed','health','damage']

func decide_ability():
	randomize()
	ability_list.shuffle()
	var displayed_ability = ability_list[0]
	print (displayed_ability)
	
	%"Label Ability 1".text = displayed_ability
	
	if displayed_ability == 'speed':
		return 1
	elif displayed_ability == 'health':
		return 2
	elif displayed_ability == 'damage':
		return 3


func _on_level_up_visibility_changed():
	decide_ability() #I would need to get the value returned by the function in another node
	

You can use ability_list.find('speed') to return the place of the speed string in your array. Array — Godot Engine (stable) documentation in English

You can use ability_list[0] to return the first value in the array.

Thank you, I didn’t know that but it does not solve my problem. I’m looking for a way to return the value in the first position of the array because I would need to use that information outside of the function (and the node it is being called in). But thank you for the information

ability_list[0] should return the first value in the array.

This does not solve your problem?

No, sorry. I managed to find a way to use it

var ability_list: Array = ['speed','health','damage','liar','knock','middle']

func decide_ability():
	randomize()
	ability_list.shuffle()
	var displayed_ability = ability_list[0]
	print (displayed_ability)
	
	%"Label Ability 1".text = displayed_ability
	
	if displayed_ability == 'speed':
		return 0
	elif displayed_ability == 'health':
		return 1
	elif displayed_ability == 'damage':
		return 2
	elif displayed_ability == 'liar':
		return 3
	elif displayed_ability == 'knock':
		return 4
	elif displayed_ability == 'middle':
		return 5


func _on_level_up_visibility_changed():
	if decide_ability()==0:
		print("0 it works")
	elif decide_ability()==1:
		print("1 it works")
	elif decide_ability()==2:
		print("2 it works")
	elif decide_ability()==3:
		print("3 it works")
	elif decide_ability()==4:
		print("4 it works")
	elif decide_ability()==5:
		print("5 it works")

It is ugly and most probably not optimal at all, but it works at least. Here my problem was how to use the value of the function outside of it, I didn’t know I could just use “decide_ability()==5” (for example) as a way to check its value. Thanks a lot for trying to help me

How about

var ability_list: Array = ['speed','health','damage','liar','knock','middle']
var displayed_index: int = 0

func decide_ability():
	displayed_index = randi_range(0, ability_list.size()-1)
	var displayed_ability = ability_list[displayed_index]
	print (displayed_ability)

func _on_level_up_visibility_changed():
	decide_ability()
	print("%s it works" % [displayed_index])

Depends on if you really need to shuffle the array.
In that case perhaps you could use an additional array

var ability_index_list: Array = [0,1,2,3,4,5]

and shuffle the index list.

Is it intended, that in your code, you shuffle the array up to six times during a single _on_level_up_visibility_changed() call? I can see cases, where in your code, the function _on_level_up_visibility_changed() doesn’t print any “it works” string.

1 Like

Functions with return
Your function returns the value - so you can save in a new var to use it in another function.

Like so:

func _on_level_up_visibility_changed():
  var random_ability = decide_ability()
  print("Returned ability is " + random_ability)
1 Like

Thank you all a lot, I will surely test all these options to see if they work for my project. I will post again to keep you all updated

Not intended at all, I just didn’t know how to fix it and saw it as a minor problem

1 Like