Godot: Add typed variables to GDScript

Created on 27 Jun 2015  Â·  11Comments  Â·  Source: godotengine/godot

So right now, every variable can be one of three types...

  • Constant - it has one value and one value only
  • Dynamic - it can have its value changed to one of any type from a string to a vector3
  • Exported - this allows you to have an adjustable value for each instance of an object

However, the downside with having to use dynamic types with all possible values that are changeable is that they are not the fastest solution around. Now I propose that GDscript gets a third main variable type, static.

The main benefit I can see is that it can potentially make scripts a bit faster, this will not replace the general dynamic type, but merely give the developer another way to optimize logic and make code more readable (because the interpreter would not have to check what type the value is and will throw an error if the wrong type of value is assigned).


The syntax would work similar to how the export system works, so you could have like...

static var health = int(5)
static var percent = float(12.5)
static var message = "hello"
static var rotation = Vector3(0,1,0)
static var list = [0,1,2,3,4]
static var blue = Color(0,0.5,1)

Would this be worth adding to Godot (which would depend on how much speed gain scripts can get in practice)?

archived feature proposal gdscript

Most helpful comment

Patrons voted this, so its priority for 3.1

On Dec 2, 2017 8:19 AM, "Hugo Locurcio" notifications@github.com wrote:

Not having optional static typing that is especially used for ALL engine
API means the learning curve for using Godot skyrockets. The constant
requirement to consult documentation is frustrating. Sorry guys ;(

Juan has some plans about implementing optional static typing in GDScript,
which may be implemented in a future Godot 3.x release (it won't be done
for 3.0, as it is currently in feature freeze). This could also bring
performance improvements when making use of static types.

Also, note that you can access the API documentation very easily from the
editor, you can even hold Ctrland click on a class name to access its
built-in documentation.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/godotengine/godot/issues/2200#issuecomment-348685415,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF-Z25e2QmcdkCliOrxcS4cC8bl2Nr4vks5s8TIrgaJpZM4FNYOb
.

All 11 comments

This is something i really, really want to do (typed variables), as it
would allow more ability to optimize code and improve code completion..
still, would rather wait a bit for this as it's quite a big an
unstabilizing change

On Sat, Jun 27, 2015 at 5:39 PM, Ace-Dragon [email protected]
wrote:

So right now, every variable can be one of three types...

  • Constant - it has one value and one value only
  • Dynamic - it can have its value changed to one of any type from a
    string to a vector3
  • Exported - this allows you to have an adjustable value for each
    instance of an object

However, the downside with having to use dynamic types with all possible
values that are changeable is that they are not the fastest solution
around. Now I propose that GDscript gets a third main variable type,
_static_.

The main benefit I can see is that it can potentially make scripts a bit
faster, this will not replace the general dynamic type, but merely give the
developer another way to optimize logic and make code more readable
(because the interpreter would not have to check what type the value is and

will throw an error if the wrong type of value is assigned).

The syntax would work similar to how the export system works, so you could
have like...

static var health = int(5)
static var percent = float(12.5)
static var message = "hello"
static var rotation = Vector3(0,1,0)
static var list = [0,1,2,3,4]
static var blue = Color(0,0.5,1)

Would this be worth adding to Godot (which would depend on how much speed
gain scripts can get in practice)?

—
Reply to this email directly or view it on GitHub
https://github.com/okamstudio/godot/issues/2200.

I see, I didn't know that adding this would entail a very involved process.

I don't need it today (my game works very smooth right now), but hopefully it can be done within the next several version releases.

after a long discussion, GDScript will be left untyped. We may make or bind an alternative language that is typed and leave gdscript as is.

@reduz Recently we discussed that this is probably no longer the case, as it would make JIT-ing much simpler... so this should be reopened, right? :smiley:

Want to try adding it? It could be a syntax like

var a:int

for example

On Dec 29, 2016 17:36, "Bojidar Marinov" notifications@github.com wrote:

@reduz https://github.com/reduz Recently we discussed that this is
probably no longer the case, as it would make JIT-ing much simpler... so
this should be reopened, right? 😃

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/godotengine/godot/issues/2200#issuecomment-269689589,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF-Z2xnD-VM7ya9W-NWotaK--88thHrsks5rNBnMgaJpZM4FNYOb
.

I prefer Dart's syntax:

var imVariant = "hi"
String imString = "ok thx"

Not having optional static typing that is especially used for ALL engine API means the learning curve for using Godot skyrockets. The constant requirement to consult documentation is frustrating. Sorry guys ;(

If that "var" keyword means "variant", neikeq's syntax makes sense.
But if "var" means "variable" (note that the keyword "const" is used in replacement of "var".) reduz's syntax sounds like the most consistent. Just my two cents post .

Not having optional static typing that is especially used for ALL engine API means the learning curve for using Godot skyrockets. The constant requirement to consult documentation is frustrating. Sorry guys ;(

Juan has some plans about implementing optional static typing in GDScript, which may be implemented in a future Godot 3.x release (it won't be done for 3.0, as it is currently in feature freeze). This could also bring performance improvements when making use of static types.

Also, note that you can access the API documentation very easily from the editor, you can even hold Ctrland click on a class name to access its built-in documentation.

Patrons voted this, so its priority for 3.1

On Dec 2, 2017 8:19 AM, "Hugo Locurcio" notifications@github.com wrote:

Not having optional static typing that is especially used for ALL engine
API means the learning curve for using Godot skyrockets. The constant
requirement to consult documentation is frustrating. Sorry guys ;(

Juan has some plans about implementing optional static typing in GDScript,
which may be implemented in a future Godot 3.x release (it won't be done
for 3.0, as it is currently in feature freeze). This could also bring
performance improvements when making use of static types.

Also, note that you can access the API documentation very easily from the
editor, you can even hold Ctrland click on a class name to access its
built-in documentation.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/godotengine/godot/issues/2200#issuecomment-348685415,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF-Z25e2QmcdkCliOrxcS4cC8bl2Nr4vks5s8TIrgaJpZM4FNYOb
.

This is a duplicate of #10630, which is more recent but also has some more activity, so closing this one in favour of the other. If any information described here is missing from the other issue, feel free to link it/copy it there.

Was this page helpful?
0 / 5 - 0 ratings