Project with multiple files (gd script) Godot 3.0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rednib
:warning: Old Version Published before Godot 3 was released.

Hello community,

i’m totally new to Godot (but not to programming) and don’t get it how file management works within Godot.

I just want to split the project in multiple files and then load needed files within other files.

I’ve used preload for that, but this instanciates the file everytime i load it.

So when i load a file within several different files, i have several different objects. I’m a bit confused by that.

I have posted the scripts so that you can see what i am doing wrong.

global.gd:

 extends Node
 var player = preload("res://scripts/player.gd").new()

player.gd:

extends Node

# include skill class (see skills.gd)
var skill = preload("res://scripts/skill.gd").new()

var _name
var _level = 1
var _maxMana = _level * 110
var _mana = _maxMana

# The player can take up to four skills into battle
var _equippedSkill = []

func _ready():
	pass

func getPlayer():
	return self

func create(name, level = 1, maxMana = 110):
	_name = name
	_level = level
	_maxMana = maxMana
	_mana = maxMana

func getName():
	return _name

func addEquippedSkill(skill):
	_equippedSkill.append(skill)


func getEquippedSkill(skillNumber):
	if skillNumber == 1 :
		return _equippedSkill[0]
	elif skillNumber == 2 :
		return _equippedSkill[1]
	elif skillNumber == 3 :
		return _equippedSkill[2]
	elif skillNumber == 4 :
		return _equippedSkill[3]

skill.gd:

extends Node

# Skill class

var _name
var _healValue
var _hotValue
var _hotTime
var _castTime
var _manaCost

var _imagePath


func create(name, imagePath, healValue, hotValue, hotTime, castTime, manaCost):
	_name = name
	_imagePath = imagePath
	_healValue = healValue
	_hotValue = hotValue
	_hotTime = hotTime
	_castTime = castTime
	_manaCost = manaCost
	return self

func getImagePath():
	return _imagePath

func _ready():
	pass

And in the main script or scene script i just want to reference the player object and it’s variables and methods.

Further a don’t get it how to instanciate an object.

When i don’t use “new()” on preload, i can’t reference anything within the loadet script at all, and if i use “new()”, it creates a new object.

Everything i just want, is load the file and then instanciate objects from let’s say player, or skills.

But within the file, i can’t do something like:

in player.gd:

func create()
    var player = new Player
    ( ... init member variables ...)
    return player

in main or scene script:

"load the file somehow"

var player = Player.create()

Hope this is not getting to confusing.
I just want to include script files into other script files and want to use the content from the includet file, as you would do in any other language.

:bust_in_silhouette: Reply From: Zylann

I’ve used preload for that, but this instanciates the file everytime i load it.

Of course it does, because in your example you are using new. If you don’t use new, what you get is the class itself (because scripts are implicitely classes), from which you can call static functions, or instantiate the class by using new.
If the script inherits a node type, then using new will give you an instance of that node with the script already attached to it (it’s all done transparently), which you will need to add to the tree (because that means you create the node by code).
Once you get this, this way of working with preload is similar to import in Python.

A more common way of splitting scripts in Godot is to place them on different nodes, and then access them with get_node("node/path").some_var = 42. GDScript is tightly integrated in Godot, so it’s also useful to understand the scene system because it’s often used with the scene tree in mind.

Hello, thanks for the answer. What do you mean by:

A more common way of splitting scripts in Godot is to place them on different nodes

How do i place scripts on nodes? And what are the benefits of doing it that way instead of using preload?

Rednib | 2017-12-02 09:49

Because you cannot script much if you don’t place a script on a node in the first place. Game engine scripts, by definition, extend the features of the engine with your own game logic. The engine works by providing you many kinds of nodes that you can place in scenes to form a level, a character etc. But giving behaviour to them must be done by adding scripts to them (right-click a node, and choose “Attach script”).

You can theoretically do everything with scripts with preloads and procedural new / add_child, but there would be no point in using the editor in the first place.

You should really read the docs about this http://docs.godotengine.org/en/stable/learning/step_by_step/scenes_and_nodes.html

Zylann | 2017-12-02 16:49