How to send the result of the script to 2D Mainscreen?

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

For example,I am writing code on Godot engine(Script).

var a : int = 7
var b : int = 3
func run():
     print(7 / 3)

The final code process(result) will be show on output screen.(main viewport)

2

I want to know how to send the result of the script to 2D Mainscreen(main viewport).I mean 2D mainscreen that is near 3D,Script,AssetLib on Godot’s Editor Interface.

edit: What my script doing is calculate the result from 7/3 and print the result to the viewport.

:bust_in_silhouette: Reply From: pcanella

You may want to use a Singleton/Autoload pattern for this. Here’s a couple of links that explain this:

https://kidscancode.org/godot_recipes/3.x/recipes/singleton/

Basically you can share back and forth with the Singleton as kind of a “shared” global class.

So for example if you want to save the result, in your Singleton you’d have something like

Singleton.gd

var shared_data = null
   .....

SomeScript.gd

Singleton.set("shared_data", somedatayouwanttosave)
....

MainScreen.gd

Singleton.get("shared_data")
...

Note, those examples can be simplified to just:

Singleton.shared_data = "my new value"

and

var local_var = Singleton.shared_data

jgodfrey | 2023-03-15 23:57

oh yes! Good call out :slight_smile:

pcanella | 2023-03-16 00:04