0 votes

Hello,

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 unprojectposition 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, caninteract, isnot_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():
    pass

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))
    get_node("TextureRect/AnimatedSprite").set_frame(0)
    get_node("TextureRect").show()
    get_node("TextureRect/AnimatedSprite").stop()

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

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

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

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.

in Engine by (21 points)

1 Answer

0 votes
Best answer

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 playercaninteract () function is not executed first.
Check it out, I can't check it on godot now and sorry mi english.

by (2,048 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.