Topic was automatically imported from the old Question2Answer platform.
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.
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();