How to go about preloading an asset. (Error: Invalid get index 'x' (on base: 'Nil'))

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dorijan5484


I’m relatively new to godot engine and I’ve recently stumbled into a sort of an annoying problem.
I’m trying to set a nodes position based on a unproject_position of camera.
I’m using global script activated as a singleton to store camera’s id through camera’s script.
Later the variable, camera’s id, is used in GUI’s script to show a button above the players head when he can interact with an object.
Player has different stages of interaction with an object: is_interacting, can_interact, is_not_interacting.

Now the error happens only if I try to set the button, at the time hidden, above the player at the start of the game. It is as the camera’s script was still not initialized.
I tried preloading the camera asset and trying to run it as camera’s id through the global script, but my attempts either failed or reproduced the same problem as before.

How would one go about loading the asset before main scene is initialized. Therefor, have the cameras id, before the script called for the unproject_position.

Here is the code that produces an error from the title:

extends MarginContainer

onready var LABEL = $HBoxContainer/Elements/State/Box/Label

var pos
var cam
var screenPos

func _ready():

func player_can_interact():
	pos = global.player_pos
	cam = global.cam_id
	screenPos = cam.unproject_position(pos)
	get_node("TextureRect").set_position(Vector2(screenPos.x, screenPos.y-80))

func player_is_interacting():
	get_node("TextureRect").set_position(Vector2(screenPos.x, screenPos.y-80))

func player_is_not_interacting():
    #this is the part that produces the error
	get_node("TextureRect").set_position(Vector2(screenPos.x, screenPos.y-80))

func _physics_process(delta):
	LABEL.text = "State: "+global.player_state
	if global.player_state == "can_interact":
	if global.player_state == "is_interacting":
	if global.player_state == "idle" || global.player_state == "is_moving":

And here is my attempt at preloading the camera scene through the global script (as well as some others):

extends Node

onready var PLAYER = load("res://Objects/Player/Player.tscn")
onready var COPY_MACHINE = load("res://Objects/Assets/Office/Copy machine/copy_machine.tscn")
onready var CAMERA = load("res://Objects/Assets/Miscellaneous/Camera.tscn").instance()
onready var UI_F = load("res://UI/F_KEY.tscn")

var cam_id
var player_id
var player_pos
var player_state

func _ready():
  cam_id = CAMERA
  player_id = null
  player_pos = null
  player_state = "idle"

I might be going at this completely the wrong way, so any ideas or suggestions are greatly appreciated.

:bust_in_silhouette: Reply From: estebanmolca

It may be that your error originates from the global player_pos variable that is null when calling the player_is_not_interacting () function. Since cam.unproject_position (pos) depends on the position of the player in this case. That would explain why it only happens at the beginning, since then the player_can_interact () function initializes the player’s position.
I also see that the screenPos variable is null if the player_can_interact () function is not executed first.
Check it out, I can’t check it on godot now and sorry mi english.

Thank you very much. As soon as you said it I realized my mistake.
Problem is fixed :slight_smile:

dorijan5484 | 2020-01-18 06:42