-
-
Notifications
You must be signed in to change notification settings - Fork 21.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
Allow some non-integer built-in constants in gdscript #14704
Conversation
That feature is already implemented as |
|
Considering that you might not want to use "pure" color values (which look unpleasant to the human eye), is this worth implementing? You could define constants using the |
Is there a way to add custom entries to ColorN in-game? |
@leoddd That's my stance, basically. If there's a static, curated list of colors Godot ships with, I'd like to have it as editor hint in some way. |
What about adding code completion to ColorN instead? |
How would you add code completion to I agree On the other hand, if we do not expose this feature (definition on non-numeric static constants), one could generalize the approach of this PR to add |
59e5bc0
to
27a7f42
Compare
PR is now cleaned up and much generalized (changed PR title to reflect this). To sum up this new PR: instead of making all static built-in constants integers (as in current master), built-in constants may now be other types (basically any As an example, this implements All non-color constants show in the docs just as integer constants do: Internally, in What this is not: a full-fledged static constants solution for having any expression or object type as a static constant or that lets users define their own static constants. |
core/variant.h
Outdated
static void get_numeric_constants_for_type(Variant::Type p_type, List<StringName> *p_constants); | ||
static bool has_numeric_constant(Variant::Type p_type, const StringName &p_value); | ||
static int get_numeric_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = NULL); | ||
static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants); |
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.
Wrong indentation here, BTW.
27a7f42
to
bb0462e
Compare
This is just nitpicking, but shouldn't |
Your |
Proved the point I made every time such constants were asked for that they're not improving code quality. If people don't understand how the coordinate system works, having such helpers just makes code less readable and more error prone; whatever is Vector3.RIGHT when you're looking towards (5.24, -12.13, -2.0)? Still, if people really want those, why not, they've been requested enough times to show that some people think differently from me ;) But I wouldn't extend GDScript features like non-integer built-in constants just for those. |
@Homer666 Not an expert on this one, but here's some thoughts on this confusing topic. If you'd pick a right-handed coordinate system and you'd define The reason is, I believe, that most people tend to think in left-handed systems; and even though OpenGL is right-handed in object space, you often find camera views in OpenGL that are configured to left-handed systems (https://stackoverflow.com/questions/4124041/is-opengl-coordinate-system-left-handed-or-right-handed). I agree with @akien-mga: all these terms lose their meaning if you're not in a standard camera frame. Only if you're in a standard camera in OpenGL object space, I think you want Unity also follows this convention: EDIT To substantiate that claim a bit, think of a person knowing no math in front of a paper with a point. Now tell that person to go - from that point - one step |
bb0462e
to
7fbabb0
Compare
Added a few more constants for illustration of what might make sense. |
@poke1024 I do agree that left-handed would come naturally to most people, but I don't know if deviating from the way Godot works is really a good idea. From what I can tell, Unity follows this convention because it actually uses a left-handed coordinate system for everything, so having Godot's two different coordinate spaces don't adhere to these two things. Identifying and understanding these little coordinate quirks have always been a requirement to using Godot beyond just plopping down Control nodes, so this won't really change anything beyond having to remember that some of the Vector constants are backwards... |
I understand, and indeed Unity seems to be consistenty left-handed. So, I guess I should change the constants to the Godot way and users could be informed that these constants only make sense if they consistently set up their local transformation frames in a way that adheres to these convention. The main question remains though if this PR will ever see the light of day :-) |
I hope it does! It's a good PR!!! Anyway, regarding |
148cac2
to
9a25997
Compare
9a25997
to
9c5f930
Compare
Thanks a lot for your contribution! Moving this PR to the 3.1 milestone, to be reviewed once the release freeze is lifted. It could eventually be cherry-picked for a future 3.0.1 maintenance release if it doesn't change the user-facing APIs and doesn't compromise the engine's stability. |
_VariantCall::add_variant_constant(Variant::VECTOR3, "UP", Vector3(0, 1, 0)); | ||
_VariantCall::add_variant_constant(Variant::VECTOR3, "DOWN", Vector3(0, -1, 0)); | ||
_VariantCall::add_variant_constant(Variant::VECTOR3, "FORWARD", Vector3(0, 0, -1)); | ||
_VariantCall::add_variant_constant(Variant::VECTOR3, "BACK", Vector3(0, 0, 1)); |
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.
Great work, although I'd also like to suggest adding "ONE"
and maybe "NEGONE"
for feature parity with the C# built-ins I added: https://github.com/godotengine/godot/blob/master/modules/mono/glue/Managed/Files/Vector3.cs#L280
core/color.h
Outdated
@@ -187,6 +188,7 @@ struct Color { | |||
static Color hex(uint32_t p_hex); | |||
static Color html(const String &p_color); | |||
static bool html_is_valid(const String &p_color); | |||
static const Map<String, Color> &names(); |
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 simply not add this here, its not going to be used anyway, just put them together with the other constants inside Variant
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.
Done.
This is very cool, if you can change what mentioned in the review, we can merge it |
modules/gdscript/gdscript_parser.cpp
Outdated
cn->datatype = _type_from_variant(cn->value); | ||
expr = cn; | ||
cn->value = Variant::get_constant_value(bi_type, identifier); | ||
cn->datatype = _type_from_variant(cn->value);expr = cn; |
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.
Slight nitpick: there shouldn't be 2 statements in the same line.
Bump, there is a very small formatting issue, but I would love to see this merged! 💙 |
I agree with the feature itself, but I think the list of constants that was added is way overkill, this should be reviewed before 3.1 is out. Things like |
They were introduced in godotengine#14704 but need more discussion IMO, they don't strike me as core features that would have to be registered in Variant directly. Moreover, they currently break the documentation XML as string constants end up encoded as e.g. `value=""..""`.
@akien-mga You're right, some constants are a pure demo thing "look what you can do with constants", I forgot about them. |
Why aren't the Color constants UPPERCASE? Also, in C# they are in a |
(Note that changing the case of the constants might warrant a change in ColorN's documentation, due to #35581) |
I can do some of these changes, I'm already invested in this. |
I'm always missing named compile-time color constants for Colors in the editor. This adds them:
Since adding full fledged static constants to gdscript (I started) might entail some longer discussions due to various side effects (time of initialization, change of byte code for singletons, etc.), this PR's implementaiton is just adding some syntactic sugar by expanding Color.xyz into Color(r, g, b, a) in the gdscript parser.