Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Center control programatically

Is there a convenient way to center a node, or more specifically, a TextureRect, into the screen? Note in my case my display window is set to be stretched in 2D "keep" aspect.

Obviously the position could be calculated with the formula:

x = WindowWidth / 2 + TextureWidth / 2
y = WindowHeight / 2 + TextureHeight / 2

But I've not found how to get the original window size of the Godot project. I'm obtaining the screen width through this.get_tree().root.size.x, and it varies according to how the window is resized. For example, my project width is 1024 and I'd like to not express this width in my code. Is that possible?

Godot Engine also seems to have the notion of anchor and centering when you use its visual editor, so I suspect there is a way of centering the node without manual calculus.

Update

The answer by @Theraot shows method of positioning node to center based on a preset, but it positions the node elsewhere, not on the center:

(Note "state" means "scene")

import State from '../State.jsx';
import RecoyxLogo from 'res://img/company-logo/recoyx.png';

export default class PresentsState extends State {
    constructor() {
        super();
    }

    async _ready() {
        await godot.yield(this.get_tree(), 'idle_frame');
        let recoyxLogo = new godot.TextureRect;
        recoyxLogo.texture = RecoyxLogo;
        recoyxLogo.expand = true;
        recoyxLogo.rect_size = new godot.Vector2(1200 / 2, 600 / 2);
        // recoyxLogo.rect_position = new godot.Vector2(this.get_tree().root.size.x / 2 + (1200 / 2 / 2), this.get_tree().root.size.y / 2 + (600 / 2) / 2);
        recoyxLogo.set_anchors_and_margins_preset(godot.Control.PRESET_CENTER);
        this.add_child(recoyxLogo);
    }

    _process(delta) {
    }
}

Here's State.jsx:

export default class State extends godot.Node2D {
    constructor() {
        super();
        // this.scale = new godot.Vector2(0.47, 0.47);
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can change the size of your control from code by setting its rect_size and rect_position.

And you can do that in a method connected to the "resized" signal of the root of the scene tree (the root of the scene is a Viewport, and - as you would expect - it emits the "resized" every time its size changes).

As per the original window size, you can read it from project settings, by calling ProjectSettings.get_setting passing the property path as parameter. The properties for the window size are "display/window/size/width" and "display/window/size/height" (you can figure out these by looking in the Project Settings window or on the documentation for ProjectSettings).

And yes, you can set margins (with the method set_margin) and anchors (with the method set_anchor) from code, and also use the presets (with the methods set_margins_preset, set_anchors_preset, and set_anchors_and_margins_preset - this last method is a convenient way to set both at the same time). Setting the preset Control.PRESET_CENTER should do.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda