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