How do I GET or SET singleton ( autoload ) variables in any script, in Visual script . .

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

I have made an autoload script, called GlobalVariable.vs, and added some variables there, in visual script . . But, how can they be accessed in other scripts, like maybe drag - and -drop, thx . .

I’m not sure, if making a .vs, instead of a .gd, and writing clearly, to define variables is maybe a bug, total noob . . .

I would highly recommend to switch over to GDScript. In GD accessing a global singelton is much easier. You can reffeer to it with “SingletonName.variableName”

klaas | 2020-07-20 18:27

Have you found any way to do it yet? Thanks.

Kreaninw | 2020-09-30 23:47

:bust_in_silhouette: Reply From: klaas

Hi,

  1. add a “get” node
  2. set mode to “instance”
  3. set Base type to _VisualScriptEditor
  4. set Base script to your singleton … you have to switch to “all file types” in the load dialog because dialog is buggy or so
  5. select property

voila … you got your value in simple 5 steps :smiley:

or write a gd script and type “SingletonName.variableName” :wink:

When I do this, it says ’ can’t because, of invalid index ', the GET box has an obj input box, that needs an input, an ’ index ’ or, so . . Thx . . . .

jasperbrooks79 | 2020-07-20 23:54

it doesn’t work, when I try to ’ print ’ the output, it writes ’ invalid index name error ', the get box has an ’ obj ’ input port, what do I need to set that to ? <3

jasperbrooks79 | 2020-08-11 02:27

:bust_in_silhouette: Reply From: Kreaninw

To anyone who comes to this question and doesn’t find an answer yet, here is the answer:

Answered by the opener of this question himself on Github.

  1. Basically, just load the .vs script file in Project Settings > AutoLoad like you normally do with the .gd script file. Then, enable the singleton.

  2. Drag the singleton script file into the Visual Script edit window. This will create the preload node with an obj output that you can drag out to search for available actions.

  3. Search with variable keyword, you can choose to set or get the default value of variables in the singleton script.

  4. Choose the desired variable name with a str input name.