How to pass data from one script to another using C#

Godot Version

4.2.1.stable.mono

How to pass data from one script to another using C#?

I have two scripts in C# and I want to pass a bool value to another script.

I don’t know C#, so I might not be very helpful, but in GDScript, I would use a signal, maybe transfer_bool, with an argument. In GDScript it could look like this:

signal transfer

func transfer(arg):
     transfer.emit(arg)

Not sure if this is very helpful, but the concept might be.
in C# this might be:

[Signal]
delegate void TransferEventHandler(bool arg);

//to transfer the bool:

//some function with an argument arg
EmitSignal(SignalName.Transfer, arg);

I have never really used C#, so this could be completely off.

1 Like

var myScript = GetNode<MyScript>("NodePathString");
and you have access to all public variables in that node script
myScript.variableName.

here you go

using Godot;
using System;

public partial class MySender:Node {

	//////////////////////////////////////////////////////////////
	// [Signal] is how we create a signal for Godot in c#   //////
	//////////////////////////////////////////////////////////////

	// please note: delegate must end in --> EventHandler <--

	[Signal]
	public delegate void MyHelloWorldEventHandler(); // SignalName.MyHelloWorld created!

	[Signal]
	public delegate void MyTestEventHandler(); // SignalName.MyTest created!

	///////////////////////////
	// callable functions /////
	///////////////////////////

	// no arguments
	// Callable.From(DoTest)
	void DoTest() {
		GD.Print("############### This is a test ##################");
	}


	// string argument
	// Callable.From<string>(DoHelloWorld)
	void DoHelloWorld(string a_message) { 
		GD.Print(a_message);
	}




	public override void _Ready() {

		//////////////////////////
		// connect signals  //////
		//////////////////////////

		Connect(SignalName.MyTest, Callable.From(DoTest)); // no arguments
		Connect(SignalName.MyHelloWorld, Callable.From<string>(DoHelloWorld)); // string argument

		////////////////////////
		// emit signals ////////
		////////////////////////

		EmitSignal(SignalName.MyHelloWorld, "This is a message from the future");
		EmitSignal(SignalName.MyTest);
	}
}

with c#, if Godot doesn’t require you to emit/connect a signal, i wouldn’t. your node is an inherited class, use functions to send/receive information.

in the example below, i find the node “Dest” and this node’s Message() function. message are logged. then i iterate over the message log and print it.

source for MySource node

using Godot;
public partial class MySource:Node {
	public override void _Ready() {

		// get node named Dest of type MyDestination
		MyDestination v_dest_node = GetParent().GetNode<MyDestination>("Dest");


		// send message to node
		v_dest_node.Message("Hello, world!");
		v_dest_node.Message("100");
		v_dest_node.Message("This is a test", 1000000000);


		// print message log from MyDestination node

		if(true == v_dest_node.HasMessages()) {

			GD.Print("########### message log ###############");

			foreach(string j_string in v_dest_node.GetMessageLog()) {
				GD.Print(j_string);
			}
		}
	}
}

.
.
source for MyDestination node

using Godot;
using System.Collections.Generic;
public partial class MyDestination:Node {
	List<string> m_message_log = new List<string>();


	public bool HasMessages() {
		return m_message_log.Count != 0;
	}

	public List<string> GetMessageLog() {
		return m_message_log;
	}


	void AddMessage(string a_message) {
		GD.Print(a_message);
		m_message_log.Add(a_message); // add to log
	}

	public void Message(string a_string) {
		AddMessage("string:" + a_string);
	}
	public void Message(int a_int) {
		AddMessage("int:" + a_int);
	}
	public void Message(string a_string, int a_int) {
		AddMessage("string:" + a_string + " int:" + a_int);
	}
}


1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.