c# not onready. this is bug

Godot Version

Replace this line with your Godot version

4.5.1 window-mono-version

Question

using Godot;
using System;

public partial class Button1 : Button
{
public AudioEffectRecord recordEffect;
public AudioStreamWav recording;
public RichTextLabel text;
Recordnode upnode;
public override void _Ready()
{
upnode = GetNode(“/root/Node2D”);
recordEffect = upnode.recordEffect;
recording = upnode.recording;
text = GetNode(“/root/Node2D/RichTextLabel”);
Pressed += OnPressed;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}

private void OnPressed()
{
	text.Text = "[center]recording....[/center] ";
	recordEffect.SetRecordingActive(true);
}

}

and other

using Godot;
using System;

public partial class Recordnode : Node2D
{
public int busIndex = AudioServer.GetBusIndex(“Record”);
public AudioEffectRecord recordEffect;
public AudioStreamWav recording;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
	recordEffect = (AudioEffectRecord)AudioServer.GetBusEffect(busIndex, 0);
	
	Godot.GD.Print(busIndex);
	Godot.GD.Print(recordEffect);
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}

}

and send this error

E 0:00:04:036 void Button1.OnPressed(): System.NullReferenceException: Object reference not set to an instance of an object.
<C# 오류> System.NullReferenceException
<C# 소스> Button1.cs:27 @ void Button1.OnPressed()
<스택 추적> Button1.cs:27 @ void Button1.OnPressed()
Callable.generics.cs:39 @ void Godot.Callable.g__Trampoline|1_0(object, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
DelegateUtils.cs:86 @ void Godot.DelegateUtils.InvokeWithVariantArgs(nint, System.Void*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant*)

this is bug? 이거 버그죠?

Please format your code properly using ``` tags. Indicate the exact script line that’s causing the error and post a screenshot of your scene structure.

1 Like
public partial class Button1 : Button
{
	
	public Recordnode mainnode;
	public  AudioEffectRecord recordEffect;
	public RichTextLabel text;
	public override void _Ready()
	{
		this.mainnode = GetNode<Recordnode>("/root/Node2D");
		this.recordEffect = mainnode.recordEffect;
		text = GetNode<RichTextLabel>("/root/Node2D/RichTextLabel");
		Pressed += button1pressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
	
	private void button1pressed(){
		text.Text = "[center]recording....[/center] ";
		this.recordEffect.SetRecordingActive(true);
	}
}

and other code

using Godot;
using System;

public partial class Recordnode : Node2D
{
	public int recordBusIndex;
	public AudioEffectRecord recordEffect;
	public Button3 button3;
	public Button2 button2;
	public Button1 button1;
	public RichTextLabel text;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		//button1 = GetNode<Button1>("/root/Node2D/Button1");
		//button2 = GetNode<Button2>("/root/Node2D/Button2");
		//button3 = GetNode<Button3>("/root/Node2D/Button3");
		recordBusIndex = AudioServer.GetBusIndex("Record");
		recordEffect = AudioServer.GetBusEffect(
			recordBusIndex, 0
		) as AudioEffectRecord;
		text = GetNode<RichTextLabel>("/root/Node2D/RichTextLabel");
		//button1.Pressed += button1pressed;
		//button2.Pressed += button2pressed;
		//button3.Pressed += button3pressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

}

i think

this.mainnode = GetNode(“/root/Node2D”);
this.recordEffect = mainnode.recordEffect;

this code not working like call by reference. GetNode this not call by reference?

or

beacase twice code _Ready() is not yet run, so i listen System.NullReferenceException error code?

i know other code style can do this think. but .. .. some so so

This is not the same code you initially posted.

In any case the error message says exactly which line caused the error. It’s line 27. However you haven’t posted the entire code so we don’t know which of two lines in OnPressed() that is. You also renamed that function in the snipped you posted afterwards.

So look at the error message, identify the line and tell us which exact line that is in the posted snippet.

Also, post the scene tree structure.

1 Like

i check problem

using Godot;
using System;
public partial class Button1 : Button
{
	
	public Recordnode mainnode;
	public  AudioEffectRecord recordEffect;
	public RichTextLabel text;
	public override void _Ready()
	{
		this.mainnode = GetNode<Recordnode>("/root/Node2D");
		this.recordEffect = mainnode.recordEffect;     // <---------here code cut 
		text = GetNode<RichTextLabel>("/root/Node2D/RichTextLabel");
		Pressed += button1pressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
	
	private void button1pressed(){
		text.Text = "[center]recording....[/center] ";
		this.recordEffect.SetRecordingActive(true);
	}
}
using Godot;
using System;
public partial class Button1 : Button
{
	
	public Recordnode mainnode;
	public  AudioEffectRecord recordEffect;
	public RichTextLabel text;
	public override void _Ready()
	{
		this.mainnode = GetNode<Recordnode>("/root/Node2D");
		
		text = GetNode<RichTextLabel>("/root/Node2D/RichTextLabel");
		Pressed += button1pressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
	
	private void button1pressed(){
		text.Text = "[center]recording....[/center] ";
        this.recordEffect = mainnode.recordEffect;     // <---------here code past 
		this.recordEffect.SetRecordingActive(true);
	}
}

change code like this. do well that. i think _Read() not yet woking in class Recordnode. so this.recordEffect = mainnode.recordEffect call the None. this is call by value. so than not read like call by reference. …. hmm….. i think this is little problem.

It seems like it doesn't quite match the [onready] specification in Godot's C#.

scene tree

 

Node2D —–Recordnode.cs
   |-Button1 --- Button1.cs
   |-Button2 --- Button2.cs
   |-Button3 --- button3.cs
   |-RichTextLabel
   |-AudioStreamPlayer2D 

Parent’s _Ready() runs only after all of its children’s _Ready()s have been executed.

So if you initialize something in Button1::_Ready() with the value that it initialized in Recordnode::_Ready() that value won’t be there because Recordnode::_Ready() hasn’t run yet.

1 Like

ok. i understand.

So, that means the code runs first in the child node, and then in the parent node. Thank you.
I've been experimenting with GoDot again, thinking I'd like to try something new as a hobby after a long time, and I'm starting to grasp the concept again. Thank you.

Yes, so you need to have this order of execution in mind when initializing stuff that depends on other stuff in _Ready()

public override void _Ready()
{
	
	text = GetNode<RichTextLabel>("/root/Node2D/RichTextLabel");
	Pressed += button1pressed;
} 

public void _afterReady()
{
	if (i == false)
	{
		i = true;
		this.mainnode = GetNode<Recordnode>("/root/Node2D");	
		this.recordEffect = mainnode.recordEffect;		
	}	
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
	_afterReady();
}

how about this style?

It’ll work but it’s a bit clunky. I’d instead just initialize everything (including children) in Recordnode::_Ready()