-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
docs: Add a paragraph about self. #8928
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: A Thousand Ships <[email protected]>
Co-authored-by: A Thousand Ships <[email protected]>
Thanks for the PR! This is greatly appreciated as it is one of the oldest undocumented features of GDScript. However, in my opinion, in the current edition the emphasis is shifted a little in a wrong direction. This feature is not intended solely for inheritance, as the reader might think. I think In GDScript there is no if has_method("test"):
test() # Error.
self.test() # OK.
call("test") # OK. |
@dalexeev I (hopefully) addressed most of your concerns in e7e4ac8, but i am still not sure how to approach the examples. I have something like this in mind: class_name Item extends Node
# Returns the expected size an item will take up in
# player's inventory (or -1 if it cannot be collected).
func get_size() -> int:
var size = get(&"size")
return size if size else -1
# When player touches an item, collect it if it is
# collectible (i.e. has a `collect` method).
func on_player_touch() -> void:
if has_method(&"collect"):
# collect() # Would be an error!
# self.collect() # @dalexeev This is also appears to be an error
# on latest 4.2, perhaps you misremembered this part?
call(&"collect")
# .. another file ..
class_name Potion extends Item
var size := 2
func collect() -> void:
print("Collected a potion!") But it looks pretty verbose and (in my view) not immediately understandable without additional comments. Do you have any better ideas? |
dead3eb
to
e7e4ac8
Compare
@dalexeev As i haven't heard anything negative towards the proposed example, i went ahead and replaced the weapon/pistol example with this new one. I also added a |
@elenakrittik Could you look into replying to the above suggestions (or applying them directly) so this PR can be merged? Thanks in advance 🙂 |
Much sorry, i'll apply the automatic change right away but i unfortunately will not be able to address dalexeev's feedback until after a week or so. Once i'm back i'll make sure to drive this to the finish line! |
Co-authored-by: RedMser <[email protected]>
Co-authored-by: Danil Alexeev <[email protected]>
Sorry for the long wait! I believe everything should be good now. |
set("not_exist", 123) # No error or warning.
self["not_exist"] = 123 # Runtime error.
self.not_exist = 123 # Runtime error.
not_exist = 123 # Compile time error.
print(get("not_exist")) # No error or warning.
print(self["not_exist"]) # Runtime error.
print(self.not_exist) # Runtime error.
print(not_exist) # Compile time error. |
Co-authored-by: Danil Alexeev <[email protected]>
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.
Looks good to me.
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.
If dalexeev approves it's good.
I suggested a couple changes to better follow the style guide.
@@ -1530,6 +1530,36 @@ Here are some examples of expressions:: | |||
Identifiers, attributes, and subscripts are valid assignment targets. Other expressions cannot be on the left side of | |||
an assignment. | |||
|
|||
self | |||
^^^^ |
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.
^^^^ | |
~~~~ |
Maybe promote the header by one level? I don't see how self
is semantically a subsection of Expressions
.
I'm also not sure if this is exactly the right section of the page to put this - is Statements and control flow
really more fitting than either Classes
or Properties (setters and getters)
?
However, since I'm not sure exactly where the best place to put it is, it is okay to merge this PR with the new section where it is, and consider moving it in a followup PR.
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.
If we ignore the specifics of how the engine treats self
(most notably, for the engine it's a keyword and not a "name"/identifier), self
is just another variable that refers to the current class instance, so, personally, it makes sense to me to display self
as a special-case of an identifier expression.
If we assume that self
does not indeed belong to Expressions
and we should move it somewhere else, i also can't find a fitting place for it. Moving it one level up is definitely not a solution (self
is not a statement (unless you write a line consisting of a standalone self
, but that's a technicality)). The best i can find is the Classes
section, however, given it's current content, i struggle to see self
there without rewriting parts or all of the section.
Co-authored-by: tetrapod <[email protected]>
Adds a paragraph about the
self
keyword and documents that it can be used to refer to variablesdefined in subclasses of current class.
Closes godotengine/godot#87944