Error with str2var: "Unexpected identifier: 'global'"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By antoinor
:warning: Old Version Published before Godot 3 was released.

Hi

  1. Probleme:
    I dont know how to fix the error.

  2. What I Expect:
    the texture of my “Sprite” should change to the one from the variable new_drone_sprite

  3. My Variables In global.gd:
    var sprite_drone1 = preload('res://images/drones/drone_01_16p.png')
    […]
    var sprite_drone2 = preload('res://images/drones/drone_02_16p.png')

  4. Function In drone.gd From drone.tscn:

var my_string = 'global.sprite_drone'
print(my_string) → global.sprite_drone

var rand_nbr = 2 # will later be replaced by randi()%11+1
print(rand_nbr) → 2

var new_string = str(my_string, rand_nbr)
print(new_string) → global.sprite_drone2

var new_drone_sprite = str2var(new_string)
print(drone_sprite) → Debuger: Error calling built-in function ‘str2var’: Parse error at line 0: Unexpected identifier: ‘global’.

get_node("Sprite").texture = new_drone_sprite

  • Additionnal Info:
    get_node("Sprite").texture = global.sprite_drone2 # this work as intended
    Godot v3.0.beta.build1 | W7.64 # probably both unrelated to the error

How can i make the function str2var work as i want ? or is there anothere way to achieve what i want ?