How can I store a variable in a variable?

Godot Version



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
    return TagRangemin.SMALL
    if rand < float(TagPercent.NORMAL) / 100:
    if is_max:
    return TagRangemax.NORMAL
    return TagRangemin.NORMAL
    if rand < float(TagPercent.EXTREME_NORMAL) / 100:
    if is_max:
    return TagRangemax.EXTREME_NORMAL
    return TagRangemin.EXTREME_NORMAL
    if rand < float(TagPercent.BIG) / 100:
    if is_max:
    return TagRangemax.BIG
    return TagRangemin.BIG
    if rand < float(TagPercent.EXTREME_BIG) / 100:
    if is_max:
    return TagRangemax.EXTREME_BIG
    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
			return min_return

func chooseTagValue(is_max: bool, is_space: bool):
	var rand = rng.randf()
	if is_space:
		for i in range(thresholds.size()):
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():

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?