Can I concatenate a variable and a string(or int)?

Godot Version

4.6.1

Question

As the title says, would it be possible to concatenate a variable and a string (or int)? For example, I have var_1, var_2, and var_3. Is there a way to do something like this?: var_ + number_selected

Why do you want to do that? What’s your end goal?

2 Likes

You can use get and set to read and modify variables by their name as strings, but it’s usually best to re-work into an array and/or enum

var MyVar1: int = 123
var MyVar2: int = 456
print(get("MyVar%d" % 1)) # 123

set("MyVar%d" % 2, 987)
print(MyVar2) # 987
1 Like

An easier way to set a variable with variables that change constantly. For example, if the player clicks on a button or sprite that has the var_1 identifier, it changes the variable from var_2 to var_1. Another example would be with a global/autoload variable—same thing, but with a global/autoload variable.

I can give some better examples using actual code if you want me to

Please do.

2 Likes

This is the idea of it, what the function does is if the interaction button is clicked and selected is _1 then we print that and set the global variable to be var_1 if selected isn’t _1 and it’s _2 we set the global variable to be var_2

if event.is_action_pressed("interact"):
		if selected != null:
			var _1
			var _2
			if _1 == selected:
				print("var_"+ selected)
				Global.var + selected = true
			elif _2 == selected:
				print("var_"+ selected)
				Global.var + selected = true

I’m not sure this is a real example, using actual code; what’s the big picture? what are you trying to create that would require this style of code.

The sample you posted doesn’t work on a conceptual level, and the variable names are so enigmatic there’s no way to tell what this even might be trying to do.

3 Likes

I think the OP might be asking for C-style pointers :smiley: … or just a dictionary.

Yeah, @d.lNh1tpMe, still not clear what you want to achieve. Post the working code that does what you want (in whatever roundabout way), or describe a non-abstract problem you’re hoping to solve with this “variable concatenation” idea.

1 Like

I get what OP is trying to do. They want to build the name of a variable at runtime based on a condition and then access the value of the real variable of that name.

For example var_name = "var_2" and then change the value of an existing variable called “var_2”. So yeah, basically like a pointer to a variable.

@d.lNh1tpMe take a look at the Expression class, that should probably be enough to do what you want.

But before you go and shoot yourself in the foot: Don’t. Find a simpler solution to this trivial problem. You are overcomplicating things.

2 Likes

If a name maps to a variable, then it’s a dictionary.

1 Like

I mean, there are a million ways to do this. But what OP wants to do is basically this, I think:

var var_2 := 0
"var_2" = 1       # var_2 is 1 now
"var_" + "2" = 2  # var_2 is 2 now

Clearly there are better ways to do this.

1 Like

Yeah… like dictionary :smile:

2 Likes

Oh I am not disagreeing with you. :laughing:

Chances are that all of this stems from a weird design decision in the first place.

2 Likes

No.

That’s not how code works. You have a fundamental misunderstanding about how code compilation and interpretation work.

Imagine you’re turning in a school paper, and you say to the instructor, well, I have two different endings, and I’d like to give you the one I think is appropriate based on how you feel about the first part of my essay. So as your essay, you turn in the first half on the day of the deadline.

A week later you enquire as to whether the instructor has read your first half and could you discuss it so you can give them the appropriate second half. They tell you that since you didn’t complete the assignment, you got an F. Because a fundamental misunderstanding of how homework and deadlines work is not the instructor’s problem.

You cannot create variable names on the fly, because your code has already been turned into computer-executable byte code. The variable names have already been turned in once you press play.

Now, with GDScript, you can indeed change a variable name while the game is running and the next time it executes the script, it will immediately have an effect. But to do what you want, the interpreter would have to have two copies of the file, and call them based on an outside evaluation. And that would be unnecessary effort, because that is what a function is for.

Here are 4 solutions that would accomplish the same logical idea you present - without requiring a re-write of a core way programming languages work.

Match

var selected: int = 0

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact"):
		match selected:
			1:
				Global.var_1 = true
			2:
				Global.var_2 = true

If/Else

var selected: int = 0

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact"):
		if selected == 1:
			Global.var_1 = true
		elif selected == 2:
			Global.var_2 = true

Dictionary

var selected: String = "var1"

var global: Dictionary[String, bool] = {
	"var1": false,
	"var2": false,
}

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact"):
		global[selected] = true

Array

var selected: int = 1

var global: Array[bool] = [false, false, false]

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact"):
		global[selected] = true
1 Like