Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Rednib | |
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.