Skip to content
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

Make Color.hex constant #10294

Closed
mageowl opened this issue Jul 28, 2024 · 4 comments
Closed

Make Color.hex constant #10294

mageowl opened this issue Jul 28, 2024 · 4 comments

Comments

@mageowl
Copy link

mageowl commented Jul 28, 2024

Describe the project you are working on

RPG roguelike with arrays of colors used to modify textures based on skin tone

Describe the problem or limitation you are having in your project

A constant array provides a performance boost, but is harder to read than using the Color.hex method.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Make Color.hex(...) a constant function so it can be used in constant arrays.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

const MY_COLOR = Color.hex(0xffffff)

If this enhancement will not be used often, can it be worked around with a few lines of script?

No, because constant functions cannot be defined with GDScript.

Is there a reason why this should be core and not an add-on in the asset library?

Color is a basic type in GDScript.

@AThousandShips
Copy link
Member

Do you mean compile time evaluated? Because it can't be const because it's static and const doesn't make any sense for static methods, this is more a limitation in the GDScript compiling generally and not specific to Color.hex

@KoBeWi
Copy link
Member

KoBeWi commented Jul 28, 2024

You can use constructor instead of static method

const MY_COLOR = Color(0xffffffff)

@mageowl
Copy link
Author

mageowl commented Jul 28, 2024

Thanks, I didn't realize this worked. I don't think it's documented.

@KoBeWi
Copy link
Member

KoBeWi commented Jul 28, 2024

Turns out it's not a constructor, but an implicit conversion operator.
This works too:

const MY_COLOR: Color = 0xffffffff

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

No branches or pull requests

3 participants