0 votes
using System;
using Godot;
using Dictionary = Godot.Collections.Dictionary;
using Array = Godot.Collections.Array;

public class global_vars : Node

    onready var viewport = GetViewport();
    onready Vector2 gameSize = new Vector2(

    public Vector2 gameRes = new Vector2(256, 240);

    //Scale type for the game window.
    //0: Integer Height - 10:9
    //1: Integer Height - 4:3
    public int scaleType = 1;

    public void _Ready()
        viewport.Connect("size_changed", this, "resize_viewport");


    public void ResizeViewport()
        var newSize = OS.GetScreenSize();
        var scaleFactor;

        if(newSize.x < gameSize.x)
            scaleFactor = newSize.x / gameSize.x;
            newSize = new Vector2(gameSize.x * scaleFactor, gameSize.y * scaleFactor);

        if(newSize.y < gameSize.y)
            scaleFactor = newSize.y / gameSize.y;
            newSize = new Vector2(gameSize.x * scaleFactor, gameSize.y * scaleFactor);

        viewport.SetSizeOverride(true, newSize);



Very new to using Mono and C# so bear with me. I'm trying to port my current screen rescaling script to C# but I'm not having any luck. I can get the rest of the code to work, but I can't get the right syntax for the onready function. Here's the original code:

extends Node

onready var viewport = get_viewport()
onready var game_size = Vector2(

var game_res = Vector2(256, 240)

#Scale type for the game window.
#0: Integer Height - 10:9
#1: Integer Height - 4:3
var scale_type = 1

func _ready():
    viewport.connect("size_changed", self, "resize_viewport")

func resize_viewport():
    var new_size = OS.get_screen_size()
    var scale_factor

    if new_size.x < game_size.x:
        scale_factor = new_size.x / game_size.x
        new_size = Vector2(game_size.x * scale_factor, game_size.y * scale_factor)

    if new_size.y < game_size.y:
        scale_factor = new_size.y / game_size.y
        new_size = Vector2(game_size.x * scale_factor, game_size.y * scale_factor)

    viewport.set_size_override(true, new_size)
Godot version 3.3.2 Mono
in Engine by (193 points)

The error I get is as follows in Visual Studio Code. Yes, I have the 3 needed extensions installed.:

The type or namespace name 'onready' could not be found (are you missing a using directive or an assembly reference?)

1 Answer

+1 vote

As of now there's no such things as a onready equivalent for C#, but there's a pull request on the project's repo discussing about it: https://github.com/godotengine/godot-proposals/issues/2425
However, you can do the same thing by initializing the onready variables and set them in the _Ready() function:

Viewport viewport;
Vector 2 gameSize;
public void override _Ready()
 viewport = GetViewport();
 gameSize = new Vector2 (ProjectSettings.GetSetting("display/window/size/width"),ProjectSettings.GetSetting("display/window/size/height"));
//Rest of _Ready()
by (224 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.