How can I store a variable in a variable?

Godot Version

4.2.1

Question

I am new at programming. And I have got a problem, I am making a function but I want it to be shorter because the function is basically repeating itself. That might be confusing so here is the scipt:

  • func chooseTagValue(is_max: bool, is_space: bool):
    rand = rng.randf()
    if is_space:
    if rand < float(TagPercent.SMALL) / 100:
    if is_max:
    return TagRangemax.SMALL
    else:
    return TagRangemin.SMALL
    if rand < float(TagPercent.NORMAL) / 100:
    if is_max:
    return TagRangemax.NORMAL
    else:
    return TagRangemin.NORMAL
    if rand < float(TagPercent.EXTREME_NORMAL) / 100:
    if is_max:
    return TagRangemax.EXTREME_NORMAL
    else:
    return TagRangemin.EXTREME_NORMAL
    if rand < float(TagPercent.BIG) / 100:
    if is_max:
    return TagRangemax.BIG
    else:
    return TagRangemin.BIG
    if rand < float(TagPercent.EXTREME_BIG) / 100:
    if is_max:
    return TagRangemax.EXTREME_BIG
    else:
    return TagRangemin.EXTREME_BIG
var thresholds=[TagPercent.SMALL,TagPercent.NORMAL,TagPercent.EXTREME_NORMAL,TagPercent.BIG,TagPercent.EXTREME_BIG]
var min_ranges=[TagRangemin.SMALL,TagRangemin.NORMAL,TagRangemin.EXTREME_NORMAL,TagRangemin.BIG,TagRangemin.EXTREME_BIG]
var max_ranges=[TagRangemax.SMALL,TagRangemax.NORMAL,TagRangemax.EXTREME_NORMAL,TagRangemax.BIG,TagRangemax.EXTREME_BIG]

func check_rand(_rand,threshold,max_return,min_return,_is_max):
	if _rand < float(threshold) / 100:
		if _is_max:
			return max_return
		else:
			return min_return

func chooseTagValue(is_max: bool, is_space: bool):
	var rand = rng.randf()
	if is_space:
		for i in range(thresholds.size()):
			check_rand(rand,thresholds[i],max_ranges[i],min_ranges[i],is_max)
1 Like

thank you, but can you explain it?

basically store all the repeating variables use into arrays with correct order
then iterating it with the size of it
make a function that takes the repeating variables from iterated index values

1 Like

Okay, now I understand it but does chooseTagvalue return somthing?

I tried It and with a little work I made the script return the value I needed. And thanks for helping!

But I still have a question, can you change a variable through another variable? Here is an example:

var rand : int
var texture_type : string

func _ready():
  Randomize_var(rand,texture_type)

func Randomize_var(var1, var2):
  var1 = 256
  var2 = "normal"

That was an example I dident try but would it work and if not how can I make something like it?