So I'm making this simple game in godot, and I have successfully made a start menu. However, I want the buttons in the start menu to actually execute other scripts. I already know how to add a function to a button, all I need is how to execute another script using a different script in godot.
If you still don't understand what I mean, look at this python code:
exec(open("file.py").read())
I basically want to do something like that in gdscript.
First of all, GDScript does not have top level statements. Well, at least not per se (it has top level variable declarations - a.k.a fields/properties - with initializers). Which means that whatever GDScript you want to execute would have to be in a method. Let me put it another way: you don't execute GDScript scripts. You execute GDScript methods that are in GDScript scripts.
Second, when a button is pressed it sends a "pressed"
signal (other system would call that an event or a notification). Whatever you want to execute when the button is pressed, you want to put it in a method connected to the signal (other systems would call that an event listener or handler).
Usually, you would connect the signal to a method in a script attached to a Node in the scene tree.
You can connect signals from the editor, in the Node panel -> Signals tab, you will find the signals of the selected node. From there you can connect them to any node on the same scene that has a script attached. And thus, you should have an script attached beforehand on the node you want to connect the signal to.
Once you tell Godot to connect a signal, it will ask you to select the node you will connect it to, and allow you to specify the name of the method that will handle it (a name is generated by default). It is also possible to connect a signal from code.
See Signals.
And from the connected method you can call whatever else you need. For example, you could call a static method in some other script; call to an script in an autoload (singleton); load a script, instance it, and call a method on the instance; evaluate code; call some other executable… If it is one of those things what you are interested in one of those, please clarify.
Addendum: Since the example would be loading an script. Here is an example of that:
var script := load("res://script.gd")
script.static_method_name()
var instance = script.new()
instance.method_name()
Addendum 2: Sine exec
takes source code, let us see an example of that too:
var file := File.new()
file.open("res://script.gd", File.READ)
var source_code := file.get_as_text()
var script := GDScript.new()
script.source_code = source_code
script.reload()
script.static_method_name()
var instance := script.new()
instance.method_name()
To the people who just skip to the examples to copy and paste: the examples above are not the usual way of doing things in GDScript.
Addendum 3
If you are going to be referencing a script multiple times, don't load it each time. Instead preload
it to a const at the start of the script where you need it:
const ClassName := preload("res://script.gd")
Or better yet, instead of loading or preloading the script. Go to the other script and give it a class name with class_name
at the start of the file, like this:
class_name ClassName
And then, where you need it, you can do this:
ClassName.static_method_name()
var instance = ClassName.new()
instance.method_name()
That is idiomatic Godot. Although, admittedly, the approach of using File
to load the script shown above, could be the basis for a modding system.
To reiterate, you need to connect the "pressed"
signal of the button, so Godot knows what to call when the button is pressed. And usually you would connect the signal to a script attached to a node on the scene.