+1 vote

I'm having trouble when I try to access a dictionary key using a for loop.
The dictionary is stored in a singleton: (global.gd)

var active_party = ['mc']
var  mc = {LEVEL=1,EXP=0,MAX_HP=200,HP=200,MAX_MP=50,MP=50,STR=5,DEF=5,SPD=5,EVA=5,ACC=5,MAG=5,MDF=5,LCK=5}

And inside the scene:

var speed
for character in global.active_party:
    speed = global.character.SPD

I get "Invalid get index 'character' (on base: 'Node (global.gd)'). "
Is there a way to do this?

in Engine by (21 points)

Hi,
character isn't in global in the code you shared. It is local to the for loop scope. Shouldn't it be speed = character.SPD without the global?

I don't get why you would reset the speed var everytime a character is called?

I tried that, p7f, and got "Invalid get index 'SPD' (on base: 'String')."

'character' is iterating the 'active_party' array, so at that moment it is the same as 'mc', and I confirmed that in the dubugger.

Cause you are iterating over an array active_party that only has an string 'mc' .See my answer below and try that and tell me pls.

man, i changed the code.. have you tried with the exact code that is in the answer? Cause it's working in my pc.

1 Answer

+1 vote
Best answer

hi:
I did this and worked in my pc:

var  mc = {LEVEL=1,EXP=0,MAX_HP=200,HP=200,MAX_MP=50,MP=50,STR=5,DEF=5,SPD=5,EVA=5,ACC=5,MAG=5,MDF=5,LCK=5}
var active_party = [mc]

and then:

for character in global.active_party:
    speed = character.SPD

The thing is that when you iterate, you were getting the string 'mc', not an object. Now you get the object. This also works for me:

var  mc = {LEVEL=1,EXP=0,MAX_HP=200,HP=200,MAX_MP=50,MP=50,STR=5,DEF=5,SPD=5,EVA=5,ACC=5,MAG=5,MDF=5,LCK=5}
var active_party = {'mc': mc}

Similar but with dictionary, so you can get element by key.
and

for character in global.active_party:
    speed = global.active_party[character].SPD
by (3,505 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.