Godot: Add an import keyword to access classes: `Import res://my_file.gd as ClassName`

Created on 18 Oct 2017  路  6Comments  路  Source: godotengine/godot

Following on a discussion about writing idiomatic GDscript with @willnationsdev and @bojidar-bg, we noted that you end up with mixed snake_case and PascalCase when you create new instances of objects if you load scripts:

KinematicBody2D.new()
var my_cool_node = load("res://my_cool_node.gd")
my_cool_node.new()

It looks like the variable isn't a node type. GDscript already borrows some syntax from python. In python, you can import the content of another package and assign it to a new identifier:

import xml.etree.ElementTree as ElementTree

The idea isn't to borrow the complex features. Just the base syntax. Importing a GDscript class into another:

import 'res://my_cool_node.gd' as MyCoolNode

So that we can write

MyCoolNode.new()
KinematicBody2D.new()

Or keep a global class like that

import "res://utils/Utils.gd" as Utils

Utils.useful_method(my_data)

Open for discussion! Past issue on a similar note: https://github.com/godotengine/godot/issues/5570

discussion feature proposal gdscript

Most helpful comment

var my_cool_node = load("res://my_cool_node.gd")

Well, using snake_case for variables doesn't mean that you can be more creative when importing something that represents a class and not a node:

const MyCoolClass = preload("res://my_cool_class.gd")

func do_stuff():
    var my_cool_node = MyCoolClass.new()

That said, I'm not saying that I'm against the proposal. Just that the snake_case/PascalCase arguments are not so relevant to define if we want such a system :)

All 6 comments

There were some discussions about this in the past, nice to see it surface as an issue 馃憤

var my_cool_node = load("res://my_cool_node.gd")

Well, using snake_case for variables doesn't mean that you can be more creative when importing something that represents a class and not a node:

const MyCoolClass = preload("res://my_cool_class.gd")

func do_stuff():
    var my_cool_node = MyCoolClass.new()

That said, I'm not saying that I'm against the proposal. Just that the snake_case/PascalCase arguments are not so relevant to define if we want such a system :)

I'm not sure where I see the PascalCase and snake_case mix being a Godot problem in the first example you gave, Nathan. This looks like the two being mixed was the end user's choice, not Godot's choice.

In regards to the keyword suggestion, personally I don't see the difference between what's suggested and what Akien suggested (using the const keyword) besides how it's typed out. But besides that, I'm not against such a keyword, I'm just confused as to the need for it.

Didn't think about this. Then we'd have to update the GDscript writing guidelines to state that PascalCase is okay for constants in this case. Currently it's only var snake_case and const CONST_CASE. Not a big deal for the end user but as we're trying to make all the code in the docs consistent for writers it's worth taking note of.

So yeah aside from that considering that in gdscript:

  • you rarely get to use inner classes and build "libraries"
  • you can access singletons and global values from any script

There's not really a need for imports as in Py or JS. Do we close it here or someone wants to add something?

I'm pretty sure that this can be closed at this point. Making any additional changes would just be unnecessary additional complexity for the parser for relatively little benefit if the styling of the const PascalCase = preload("script.gd") construction can be used in place of the import concept's goal.

This is now fixed by a3f1ee5c5.

Was this page helpful?
0 / 5 - 0 ratings