Hi everyone,
I am stuck once again. How do I make a child instance access a ressource file, that I define while adding the child?
In my Scene scMain.tscn
I instance a child on button press
func _on_slot_1PopUp_id_pressed(id):
# Hide the popup
$MarginContainer/VBoxContainer/Tabs/Overview/slot_1PopUp.visible = false
# Create a new instance of the playerMain scene
var resource = get_node("res://Players/basicPlayer.tres")
var targetContainer = get_node("MarginContainer/VBoxContainer/Tabs")
var new_player = preload("res://playerMain.tscn").instance()
var container = MarginContainer.new()
playerCounter += 1
container.name = "player_" + str(playerCounter)
new_player.name = "player_" + str(playerCounter)
new_player.resource = resource
# Add the new instance to the scMain scene
targetContainer.add_child(container)
container.add_child(new_player)
As you can see I try to pass the basicPlayer.tres to the child so it accesses this as resource, but that doesnt work.
Here's the code of the child up to the point where it crashes:
extends Node
export(Resource) onready var resource
var moneyPerSecond: float
var level: int
var baseJohnsPerHour: float
var johnsPerHour: float
var johnsPerSecond: float
var moneyPerJohn: float
#everything about experience
var expPerJohn: float
var experience: float
var baseLevel: int
var expExponent: float
var expMultiplicator: float
func _ready():
update_johnsPerSecond()
update_experience()
update_level()
update_moneyPerSecond()
update_expPerJohn()
func _on_Timer_timeout():
get_resources()
update_johnsPerSecond()
update_experience()
update_level()
update_moneyPerSecond()
update_expPerJohn()
#picks the ressource from the according ressource file
func get_resources():
baseJohnsPerHour = resource.baseJohnsPerHour
baseLevel = resource.baseLevel
moneyPerJohn = resource.moneyPerJohn
expPerJohn = resource.expPerJohn
expExponent = resource.expExponent
expMultiplicator = resource.expMultiplicator
Here's the error:
Invalid get index 'baseJohnsPerHour' (on base: 'null instance').