How to do script communication with inputs and outputs?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Voxelstice

Is it really possible to have scripts communicate with eachother? I’ve searched everything and most answers either ended up being impossible to replicate in C#, or not working.

What I want to do is call a function from another script and have it return something, like a string for example. However in my case, this doesn’t seem to return what exactly I want it to.

Script 1

var result = filesystem.Call(game.src.filesystem.getFiles());
GD.Print("Result: " + result);

Variable filesystem is a Node.

Script 2

public static string getFiles()
{
	GD.Print("Function from another script is fired");
	return "Hello";
}

The function does successfully run, but what it returns is blank.
Is there anything I’m doing incorrectly? I can’t seem to figure this out.

:bust_in_silhouette: Reply From: zhyrin

I don’t use c# and I’m unfamiliar with the godot c# api, but the first line seems funky.
You call the Call() method on filesystem, and give it a string parameter with the value of "Hello" (return value of your getFiles() method). The result is most probably null since filesystem doesn’t have a method called Hello.
a) Why do you use Call() explicitly?
b) What is the expected argument type for Call()?

The Hello is not a method, it’s a string.

What do you mean by using Call() explicitly?
Call() returns Variant and expects StringName method, params Variant[] args

Voxelstice | 2023-03-22 12:41

Why do you use Call() is the question. Why not just call the method you want itself?
This is what your code tells godot:

here is my custom node (filesytem), please execute its function named:

here is my custom classes static method, please execute it (game.src.filesystem.getFiles()) → returns Hello string

Okay, so you want me to call the function called Hello on this custom class filesystem.

I’m guessing your filesystem doesn’t have a method called Hello, so it can’t execute it, your call to filesystem.Call() will instead return null and store it in your result variable.
What you want is:
var result = game.src.filesystem.getFiles();

zhyrin | 2023-03-22 17:46

:bust_in_silhouette: Reply From: Voxelstice

Turns out I was doing it wrong.

I’ve just found a solution to this, I’m pretty sure someone is gonna have the same issue so I’ll show the code here:

Script 1

var filesystem = GetNode<filesystem>("/root/Control/Filesystem");

GD.Print("File: " + filesystem.getFiles());
filesystem.setFile("content");

filesystem is a existing .cs script
Script 2

public string contents = "This is file content";
public string getFiles()
{
	GD.Print("Returning file");
	return contents;
}

public void setFile(string newContents)
{
	contents = newContents;
}

I simply looked at singletons and knew what the solution should be.