How can i set string to variable?

Godot Version

v4.2.2

Question

is it possible to make…?

for first i have variable gun1= get_node(“/root…”), gun2, gun3(they are texture node)

and i have variable type =“gun”
iconindex = (some index)

and i created add = type + str(iconindex)

now, when i write gun1.texsture = …
it works
but if i write add.texsture =… it isn’t works

so, i have add with value “gun1”
what can i make to set texture with add…?

Can you share a short, complete, script showing what is working and what isnt (and notate what you expected it to do)?

1 Like

Try get(add) instead of just add.

Invail set index ‘texture’ with value of type string

You only need the name of the node. Unique names will not help here though, all of your guns need be a child of the same node.

var gun_parent: Node2D = %Gun1.get_parent()
var gun_name: String = "Gun" + str(add)

var new_texture = load("res://assets/icons/"+add+lvl+".jpg") # must load resources
gun_parent.get_node(gun_name).texture = new_texture

for reference get_node("Gun1") is the same as $Gun1


Make sure to paste your scripts, it will be formatted correctly if between three ticks like so

```
type or paste code here
```

2 Likes

Would you have to use load or preload to get the texture?

Like this

Or this

1 Like

it works, thank you🙏