Variable to reference another variable?

Godot Version

v.4.2.2

Question

I’m trying to make a random cards system, I need the card to set 2 values to random values, and then set text to those variables + values. Right now it’s just

A. set VARIABLE to random PROPERTY from array of props (reference)
B. set value of PROPERTY stored in the VARIABLE to a random number
C. text = PROPERTY.VARIABLE.variable’s name + PROPERTY.VARIABLE

I’m very stuck

Some of the code (I deleted some that wasn’t working)

What’s not working? The code looks OK.

GDScript makes copies for basic data types (strings, ints, floats, Vectors) and references for everything else Object-y (if it has a .duplicate() function). There is no way around this, you may have to store the index to your array if you want to edit one value from it, or turn your array’s contents in to a custom Object type.

What I’m trying to do is have a bunch of parameters that get randomly chosen from the list of them when the card is instantiated, and then remember what parameters (variables like extrajump and extrasize) were chosen so I can tell the card what to display ("Makes you jump higher by x1.5, but makes you 0.8x slower). I tried using an array to store the parameters but couldn’t get it to work right. I tried storing a variable to an array and checking where the variable was, but I think the code didn’t do it right

var Prop = 0
var ArrayP = [Prop0] (ArrayP has 0 in it, not the variable)

CProp = Get a random prop from ArrayP
(set Prop to a random value between 1-50) (ArrayP still has a 0) (Cprop is still storing 0)

Show (ArrayP.find CProp (supposed to mean Prop)) (Value of CProp’s variable)

I think a dictionary might work tho

Could you show that code? I think knowing your specific use case would help. You want make cards with randomized effects, the difficult part is displaying this information?

1 Like

I figured it out actually, dictionaries worked

    tname = narray.pick_random()
extrajump = randi_range(1,50)

dictp = {
	"extrajump":extrajump
}

var size = dictp.size()
var random_key = dictp.keys()[randi() % size]
var prop = dictp[random_key]


text = str(tname) + str(random_key) + str(prop)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.