0 votes

Is it possible to use enum values as dictionary keys in GDScript? I have some code that does the following to setup directional movement. However, when I try to access a value in the dictionary, I get the error "Invalid get index 'RIGHT' (on base: 'int')." Is this just not allowed? Thanks!

enum Direction {
    RIGHT,
    LEFT,
    UP,
    DOWN
}

var movementVectors = {
    Direction.RIGHT: Vector2.RIGHT,
    Direction.LEFT: Vector2.LEFT,
    Direction.UP: Vector2.UP,
    Direction.DOWN: Vector2.DOWN
}

var direction = Direction.RIGHT

# Gives me the error Invalid get index 'RIGHT' (on base: 'int').
var movementVector = movementVectors[direction]
asked May 20 in Engine by arnath (16 points)

1 Answer

0 votes

This code works perfectly fine. The error says "on base: 'int'" - that's nothing to do with dictionary keys - it means you're trying to use [] on an integer variable.

Maybe you wrote movementVector[direction] (leaving off the "s")?

answered May 20 by kidscancode (17,004 points)
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 How to use this Q&A? before posting your first questions.