Godot: include("res://script.gd") function

Created on 6 Jul 2016  路  10Comments  路  Source: godotengine/godot

It could be really useful to part a code in several files, just like PHP include() function http://php.net/manual/en/function.include.php

archived discussion feature proposal gdscript

Most helpful comment

:-1: from me, that's the worse feature some programming languages have in general, though it might be useful in some cases.

It is way better to use an autoload or "imports":

const ClassName = preload("res://script.gd")
var local_instance = ClassName.new()
# Or:
onready var local_instance = ClassName.new(self)

func _use():
    local_instance.do_something(1,2)

On a side note, wouldn't custom global functions be way more useful?

All 10 comments

:-1: from me, that's the worse feature some programming languages have in general, though it might be useful in some cases.

It is way better to use an autoload or "imports":

const ClassName = preload("res://script.gd")
var local_instance = ClassName.new()
# Or:
onready var local_instance = ClassName.new(self)

func _use():
    local_instance.do_something(1,2)

On a side note, wouldn't custom global functions be way more useful?

I may be pulling this from my behind, but wasn't (isn't?) there an "extends [filename]" syntax, as well?

@Sslaxx doesn't count since you can't extend more than one file.

I don't feel include() would be useful, I prefer to use imports with preload. One use case I see is when a script becomes a behemoth of 1000+ lines. But I also fight this and it can always be solved by a better code design (splitting in component nodes for example).
Another use case is code "re-use", which is actually a fancy way to copy/paste, I don't like this either.

Custom globals sound useful, but to be used with care, as the global namespace is already full of reserved names. Preloading enforces a local container name (name of the const).

I don't see the need for custom global functions. Currently you can make a singleton with functions and use those. Much better to read singleton.my_func() (which I can track down) than just my_func() (which I can't tell where it comes from).

IMO the less access to global namespace the better.

@vnen or you can preload() the script if the function is static, no need for a singleton if you don't hold any state :)

Custom global functions that don't require calling a singleton would be nice. Right now I have some global math functions (functions for modifying vectors and normalizing angles) which I have to call mymath to use, which is a bit strange to have a difference just because I wrote them.

Given the lack of consensus and the addition of named classes in the master branch (a3f1ee5c5), I think this is more or less settled now.

I'd still find this usefull to split a script in parts

Was this page helpful?
0 / 5 - 0 ratings