-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Slight polish at enum example #8615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Tested and it does print |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above, this works as advertised in my testing, please test yourself, and the way it works here helps with indicating how it continues counting from the last value, but the correction in print(State) is good
This is the output when running the code in the documentation currently:
5
["STATE_IDLE", "STATE_JUMP", "STATE_SHOOT"]
{ "STATE_IDLE": 0, "STATE_JUMP": 5, "STATE_SHOOT": 6 }
[0, 5, 6]
| # Is the same as: | ||
| const State = {STATE_IDLE = 0, STATE_JUMP = 5, STATE_SHOOT = 6} | ||
| const State = {STATE_IDLE = 0, STATE_JUMP = 5, STATE_SHOOT = 7} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Named enums can be used as type hints, unlike constant dictionaries, so they are not the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the wording above should be improved too, as it says it's exactly a dictionary.
godot-docs/tutorials/scripting/gdscript/gdscript_basics.rst
Lines 1220 to 1222 in ca93dda
| If you pass a name to the enum, it will put all the keys inside a constant | |
| :ref:`Dictionary <class_Dictionary>` of that name. This means all constant methods of | |
| a dictionary can also be used with a named enum. |
| print(State.keys()) | ||
| # prints '["STATE_IDLE", "STATE_JUMP", "STATE_SHOOT"]' | ||
| # prints '{ "STATE_IDLE": 0, "STATE_JUMP": 5, "STATE_SHOOT": 7 }' | ||
| print(State) | ||
| # prints '[0, 5, 6]' | ||
| # prints '[0, 5, 7]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order is reversed, print(State) prints a dictionary, and print(State.keys()) prints an array of keys (here the values).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's print(State.values()), it's above the line that has the command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's:
print(State.keys()) # prints '["STATE_IDLE", "STATE_JUMP", "STATE_SHOOT"]'
print(State) # prints '{ "STATE_IDLE": 0, "STATE_JUMP": 5, "STATE_SHOOT": 7 }'
print(State.values()) # prints '[0, 5, 7]'Reformating as this would make it clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to add spaces between so it's more clear, but it seems every little change I add ruins something else, so I will commit only the suggestions 👍
| func _ready(): | ||
| # Access values with Name.KEY, prints '5' | ||
| print(State.STATE_JUMP) | ||
| # Use constant dictionary functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # Use constant dictionary functions | |
| # Use dictionary methods: |
This refers to methods with the const qualifier, but they are not currently evaluated as constant expressions, even when called on constant dictionaries or named enums.
|
I apologize for my oversight. I did |
|
Thank you! Merged. |
Slight polish at enum example
|
Cherry-picked to 4.2. |
@timothyqiu detected a wrong comment output in #8599 (comment)
I also noticed
STATE_SHOOTof the enum is 6 at const but not at enum, also fixed. Made it 7 instead of 5, so it's obvious that the user can step and it's not in i++ order