+1 vote

I'm trying to spawn a NinePatch to serve as a text box. However, since it's a child of the Player node, it uses the player's origin as its own. So, as soon as it's added as a child, it

var talkbox = load("res://objects/talkbox.tscn") #This scene is the 9patch with its own script. 
var ibox = talkbox.instance()
#=== I have tried all of the options below, each by itself at a different time
ibox.rect_position = $playercamera.get_camera_screen_center()
ibox.rect_position = $playercamera.get_camera_position
ibox.rect_position = Vector2(position.x - 180, position.y + 100)

The problem is that they always spawn the box in a different place compared to the center of the screen.

Images of the problem

Is there a way to make a spawned node position itself relative to the visible screen? A gdscript function you can call?
I messed around with the 9patch anchor, but I couldn't for the life of me get it to spawn where I need it to.

Godot version 3.3.3
in Engine by (292 points)

2 Answers

0 votes

you should make the controls node that do not have any link with the "world", child of a CanvasLayer object.
This way those control nodes will always be in front of everything and their rect position will only depends on screen coordinates
Something like


by (1,436 points)
0 votes

Try changing the position after the add_child() function

#This scene is the 9patch with its own script. 
var talkbox = load("res://objects/talkbox.tscn")
var ibox = talkbox.instance()

#Add ibox instance to scene Tree

#get offset of ibox's size
var offset = ibox.rect_size() * vector2(0.5, 0.5)

#get screen center using viewport since camera size and position are not constant
var center = $playercamera.get_viewport_rect().size * vector2(0.5, 0.5)

#set ibox position to center of screen then offset by half of ibox's size
ibox.rect_position = center - offset
by (4,636 points)
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.