![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | 9BitStrider |
using Godot;
using System;
[Tool]
public class Section : Node2D {
// Set the size of the bounding box for the room.
private CollisionShape2D box;
public int width = 256;
public int height = 240;
[Export] private int _width {
get {return width;}
set {width = value;
setWidth(width);}
}
[Export] private int _height {
get {return height;}
set {height = value;
setHeight(height);}
}
private bool allowResize = false;
private float limitUp;
private float limitDown;
private float limitLeft;
private float limitRight;
public override void _Ready() {
box = (CollisionShape2D)GetChild(0).GetChild(0);
allowResize = true;
}
private void setWidth(int value) {
width = value;
_updateSize();
}
private void setHeight(int value) {
height = value;
_updateSize();
}
private void _updateSize() {
Vector2 extents = new Vector2(width / 2, height / 2);
box.Shape.Set("extents", extents);
box.Position = extents;
}
}
Essentially, the problem I’m having is that updateSize() is running before the node is completely ready, giving me an error:
Object reference not set to an instance of an object.
I changed _updateSize() to the following:
private async void _updateSize() {
await ToSignal(this, "ready");
Vector2 extents = new Vector2(width / 2, height / 2);
box.Shape.Set("extents", extents);
box.Position = extents;
}
This stops the above error, but the box will now only update once the ready signal has been tripped. Is there a way to check to see if the script has already passed the ready function before doing this? I want to be able to update the box size on the fly.
Check if box is null, and if so, create the reference to it there and then.
SteveSmith | 2023-01-02 13:02
Thanks. It worked like a charm.
9BitStrider | 2023-01-02 13:30
Thanks, I’ll add it as an answer.
SteveSmith | 2023-01-02 13:41