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

Add enums to GDScript #2966

Closed
bojidar-bg opened this issue Dec 1, 2015 · 11 comments · Fixed by #6292
Closed

Add enums to GDScript #2966

bojidar-bg opened this issue Dec 1, 2015 · 11 comments · Fixed by #6292

Comments

@bojidar-bg
Copy link
Contributor

TL;DR

I would like to avoid writing

const CORNER_W  = 0
const CORNER_NW = 1
const CORNER_NE = 2
const CORNER_E  = 3
const CORNER_SE = 4
const CORNER_SW = 5

And be able to write it like:

enum {CORNER_W, CORNER_NW, CORNER_NE, CORNER_E, CORNER_SE, CORNER_SW}

Some more ideas

Those ideas might simplify the usual workflow, or add additional features for strange workflows or setups.

Nice addition to avoid repetition:

enum CORNER_{W, NW, NE, E, SE, SW}

Custom values idea 1:

enum {CORNER_W = 1, CORNER_NW = 2, CORNER_NE = 3, CORNER_E = 4, CORNER_SE = 5, CORNER_SW = 6}
enum {CORNER_W = 4, CORNER_NW, CORNER_NE, CORNER_E, CORNER_SE, CORNER_SW}

Custom values idea 2:

enum {CORNER_W, CORNER_NW, CORNER_NE, CORNER_E, CORNER_SE, CORNER_SW} = [1,2,3,4,5,6]
enum {CORNER_W, CORNER_NW, CORNER_NE, CORNER_E, CORNER_SE, CORNER_SW} = range(1,7)

Maybe enum should be prefixed by const or var depending on use? (IMO, it would be better to assume const, as to not create ambiguities, and/or hard to understand code)

const enum {}
var enum {}
@akien-mga
Copy link
Member

The wanted behaviour can be somewhat achieved with:

 const CORNER = { "W":0, "NW":1, "NE":2, "E":3, "SE":4, "SW":5 }

It's less elegant of course (and the variables would be CORNER.W and not CORNER_W) :)

Regarding the custom values ideas, it just boils down to supporting multiple assignments:

const CORNER_W, CORNER_NW, CORNER_NE, CORNER_E, CORNER_SE, CORNER_SW = 1, 2, 3, 4, 5, 6

@WeAthFoLD
Copy link

+1 Would really love to use enums.

But I don't think "var" enums are really helpful. (I think you mean "var" by being able to reassign enum values like CORNER_W = 5, correct? >.<). Do you have any use case in mind?

@bojidar-bg
Copy link
Contributor Author

@WeAthFoLD ~ No, I don't have any use case in mind about them. Just added them as an option… 😄

@WeAthFoLD
Copy link

IMO enumerations are constants by nature, which is true in many other languages. If you allow enums to be variable and regard them as sth like syntax sugar of multiple assignments, they will be somewhat misleading >_

@bojidar-bg bojidar-bg mentioned this issue Jan 29, 2016
@Zylann
Copy link
Contributor

Zylann commented Mar 13, 2016

What about naming the enum, so we can write Corner.NW ?

@neikeq
Copy link
Contributor

neikeq commented Mar 13, 2016

@Zylann I don't see the point since GDScript is dinamically typed. But if the above is implemented, you could do something like this:

class Corner:
    enum { W, NW, NE, E, SE, SW }

@Zylann
Copy link
Contributor

Zylann commented Mar 20, 2016

You could use named enums to put them into exported variables, so we can reuse their values, type them or document them without rewriting.

bojidar-bg added a commit to bojidar-bg/godot that referenced this issue Aug 27, 2016
@akien-mga akien-mga added this to the 2.2 milestone Sep 11, 2016
@Sslaxx
Copy link

Sslaxx commented Sep 11, 2016

So, is Corner.W the syntax to be used for accessing enums?

@bojidar-bg
Copy link
Contributor Author

@Sslaxx Well, yeah, depending on what you want from it. All three of those are valid:

#1
enum Tile {EMPTY, WALL, BREAKABLE, ONEWAY}
func foo(bar):
    if bar == Tile.EMPTY:
        return true
    return false # TODO, make it possible to go through oneway tiles

#2
func _bar(foo):
    return foo == EMPTY # Not that namespaced, but, well...

#3
enum {STATUS_ERROR, STATUS_GOOD, STATUS_EVER_BETTER}
func prune_connections():
    for conn in connections:
        if connections[conn].status == STATUS_ERROR:
            connections.erase(conn)

@Zylann
Copy link
Contributor

Zylann commented Sep 12, 2016

The point I see in "namespacing" is actually being able to name the enum, so it could be used in export for example (instead of writing all possible values twice), or documented.

@bojidar-bg
Copy link
Contributor Author

@Zylann Yeah, that's planned, though it would be nice if you open an issue about it 😄

chanon pushed a commit to chanon/godot that referenced this issue Jan 22, 2017
Faless pushed a commit to Faless/godot that referenced this issue Apr 6, 2017
Fixes godotengine#2966

(cherry picked from commit 4ee82a2)
@akien-mga akien-mga modified the milestones: 2.2, 3.0 Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants