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').

  • DaveTheCoder replied to this.
  • DaveTheCoder
    I finally figured it out!
    Here's the correct code:

    var new_player = preload("res://playerMain.tscn").instance()
    var resource = load("res://Players/basicPlayer.tres")
    new_player.resource = resource
    container.add_child(new_player)

    and then in the child:

    var resource
    func get_resources():
    	baseJohnsPerHour = resource.baseJohnsPerHour
    #etc.

    Since res://Players/basicPlayer.tres is a Resource and not a Node, get_node() obviously doesn't work. So using load() solved the problem!

    jooscher var resource

    That occurs in two places: in func _on_slot_1PopUp_id_pressed() and in the second script, which this line is using:
    baseJohnsPerHour = resource.baseJohnsPerHour

    The second instance of resource does not appear to be initialized, so it's null, as the error message is indicating.

      DaveTheCoder
      Thanks for the reply 🙂
      Shouldn't export(Resource) var resource be enough to initialize it?
      Also: If I just assign a resource through Godots UI, it works 🙁

      The export keyword allows the variable's value to be set in the Inspector (Godot UI). That line only declares the variable; it doesn't initialize it.

        DaveTheCoder
        I finally figured it out!
        Here's the correct code:

        var new_player = preload("res://playerMain.tscn").instance()
        var resource = load("res://Players/basicPlayer.tres")
        new_player.resource = resource
        container.add_child(new_player)

        and then in the child:

        var resource
        func get_resources():
        	baseJohnsPerHour = resource.baseJohnsPerHour
        #etc.

        Since res://Players/basicPlayer.tres is a Resource and not a Node, get_node() obviously doesn't work. So using load() solved the problem!