Skip to content

Conversation

@TheYellowArchitect
Copy link
Contributor

@timothyqiu detected a wrong comment output in #8599 (comment)

I also noticed STATE_SHOOT of 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

@AThousandShips
Copy link
Member

AThousandShips commented Dec 12, 2023

I also noticed STATE_SHOOT of the enum is 6 at const but not at enum, also fixed.

Tested and it does print 6 in code, please confirm, as I ran it and it worked correctly, the current code is good as it does show the next value is incremented from the last one, so you can do groupings, like { FOO = 10, BAR (= 11), BOO = 20, BAZ (= 21) }

Copy link
Member

@AThousandShips AThousandShips left a 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]

@AThousandShips AThousandShips added enhancement topic:gdscript area:manual Issues and PRs related to the Manual/Tutorials section of the documentation cherrypick:4.0 cherrypick:4.1 cherrypick:4.2 labels Dec 12, 2023
Comment on lines 1232 to 1233
# 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}
Copy link
Member

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.

Copy link
Member

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.

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.

Comment on lines 1241 to 1244
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]'
Copy link
Member

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).

Copy link
Member

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

Copy link
Member

@AThousandShips AThousandShips Dec 12, 2023

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

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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.

@TheYellowArchitect
Copy link
Contributor Author

TheYellowArchitect commented Dec 13, 2023

I apologize for my oversight. I did git commit --amend, implementing the recommended suggestions.

@mhilbrunner mhilbrunner merged commit 8f784e4 into godotengine:master Dec 13, 2023
@mhilbrunner
Copy link
Member

Thank you! Merged.

mhilbrunner added a commit that referenced this pull request Jan 25, 2024
@mhilbrunner
Copy link
Member

Cherry-picked to 4.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:manual Issues and PRs related to the Manual/Tutorials section of the documentation cherrypick:4.0 cherrypick:4.1 enhancement topic:gdscript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants