Godot: GDScript parser does not allow "self[x]" with x a String variable

Created on 18 Jan 2019  路  13Comments  路  Source: godotengine/godot

Godot version:
3.1 Beta

OS/device including version:
Win 10

Issue description:
Previous you could easily use self[x] in any class, including custom, but now it returns "Can't index on a value of type 'self'".

i.e.

var foo = 0
var bar = 1

func _init():
    for i in ['foo', 'bar']:
          self[i] += 1`

Steps to reproduce:
Make any function in GDNative including "self[x]" in it.

bug gdscript

Most helpful comment

I use it primarily in custom classes to modify multiple variables in one go. I.e.

class hero:
var strength = 1
var agility = 1
var intelligence = 1

func increase_stat(stat = 'strength'):
     self[stat] += 1

func increase_stats(array):
    for i in array:
          self[i] += 1

It's also kinda mandatory if you want to trigger setgetter, as from inside it only triggers with self

All 13 comments

My question is, what would self[x] do? It doesn't really... make much sense to me, unless self is an array of some sort.

@afftor Are you sure you don't want this instead?

var foo = 0
var bar = 1

func _init():
    for i in ['foo', 'bar']: # <<< Note the quotes here
          self[i] += 1`

I use it primarily in custom classes to modify multiple variables in one go. I.e.

class hero:
var strength = 1
var agility = 1
var intelligence = 1

func increase_stat(stat = 'strength'):
     self[stat] += 1

func increase_stats(array):
    for i in array:
          self[i] += 1

It's also kinda mandatory if you want to trigger setgetter, as from inside it only triggers with self

@timoschwarzer yeah, that.

@afftor I think the clean way to do this is using set():

var foo = 0
var bar = 1

func _init():
    for i in ['foo', 'bar']:
          set(i, 1)

@timoschwarzer either way it was behaving fine in 2.1 and 3.0, don't see a reason why change it now.

So what are we discussing now?

    for i in [foo, bar]:
          self[i] += 1`

Is invalid code, that's the same as for i in [0, 1]:, so i takes values 0 and 1.

Or this?

    for i in ["foo", "bar"]:
          self[i] += 1`

which should work a priori, if not it's likely a bug.

If we're talking about the latter, please update the OP as it's confusing currently.

Alright, the problem is with self[x] indeed. Clearer test case:

var x = 0

func _ready():
    var y = "x"
    print(self["x"])  # Works
    print(self[y])   # Parse error: Can't index on a value of type 'self'.
    print(self.get(y))  # Works
    print(self[str(y)])   # Parse error: Can't index on a value of type 'self'.

Error message:

SCRIPT ERROR: GDScript::reload: Parse Error: Can't index on a value of type 'self'.
   At: res://Main.gd:14.
ERROR: reload: Method/Function Failed, returning: ERR_PARSE_ERROR
   At: modules/gdscript/gdscript.cpp:574

I don't know if this is related, but I was trying to do this:

onready var animation_tree : AnimationTree = $AnimationTree

func play_anim(anim : String) -> void:
    animation_tree["parameters/%s/active" % anim] = true

But I get the error Can't index on a value of type 'AnimationTree'. If I change the var to animation_tree : Object or just get rid of the typing completely then it works fine.

I was using self[x] for checking various skills (i.e. vars), which are given as function parameter (to avoid having to hardcode a list of them), and I just ran into this in 3.1.

For myself, I was doing weapon upgrade system and my use case was I wrote the name of the member in a json file and I was modifying the member directly, the system wasn't exactly perfect but it was doing the job to be not complicated and this is the code I was using

func loadUpgrade(arrayUpgrade):
    for upgrade in arrayUpgrade:
        var Name = upgrade["Name"]
        var IncrementValue = upgrade["IncrementValue"]
        var upgradeLevel =  upgrade["Level"]
        self[Name] += (IncrementValue * upgradeLevel)
        pass
    pass

Can we please please get a fix for this regression? It worked fine before 3.1....

Was this page helpful?
0 / 5 - 0 ratings