How to make a variable general?

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


I mean, how could a var defined in a function could become a var from the script itself, and so be used in others of its functions or other nodes?


:bust_in_silhouette: Reply From: Sween123

There are two ways:

  1. Create a variable first, inside the function you just assign a value to it.
  2. Create a dictionary, save every variables of the script inside this dictionary. By that I mean not using new variables, just put a new key inside the dictionary. It contains all the infos about the scene.
    If you use var within a function, that will be a local variable, which means it’s not usable once outside of the function. To make a variable “general”, I think you mean a global variable, or relatively global.

Yes, that is what i mean, but creatin a var first, then the value assigned in the function won’t be accessible outside of it, right?

Wiith a dictionary, you mean creating it in any script, to store var datas? And does it keeps changes of my var?

Syl | 2020-02-13 08:44

Variables created inside a function will not be usable once outside of it.
Create them outside of the function. (In script)
Same for the dictionary, for example:

var info = {}
func my_function():
    info.level = 1
    info.character = {}

Here .level and .character are just like variables, but within a dictionary.

Sween123 | 2020-02-13 09:04

And they could be retrieved and used from other functions and nodes? Just bein put inside those fancy brackets?

Syl | 2020-02-13 09:23

A dictionary itself is also a variable.
You can go to the Godot docs or search on Google to learn about what a dictionary is. It’s a commonly used variable type. Where it can be used is determined by where it is declared, just like all the other variables.
For example, you have a scene Node2D - Player, the script is attached to Node2D.
You have a dictionary INFO in Player, to get the dictionary, in the script of Node2D, use get_node("Player").get("INFO")

Sween123 | 2020-02-13 09:32

Chipping in to reinforce a point; Sween123 is addressing two distinct topics: (a) using class variables (i.e. declaring the variable outside the function) and (b) using variables of type dictionary. Your need as stated in the original question is answered by (a).

Encapsulating multiple class variables in a dictionary (b) might look cleaner in case there are many of them, but nothing forbids just using distinct variables instead, that is (for example in

var level
var character

func my_function():
    self.level = 1
    self.character = {}

and you can refer to them from another node as


Andrea Giancola | 2020-02-15 02:13

Thxs, but i can’t make a ‘{’ sign in the first place… For some reason in the godot editor, ctrl+alt+4{ doesn’t write it…

Syl | 2020-02-14 12:21

Thxs but my issue is a bit more tricky:

Trying to roll a dice on a collisionshape, with two conditions from outside variables

extends CollisionShape2D

var maximum = 100
var minimum = 1
onready var cursor_state = get_node("/root/Node2D/Use").cursor_state
var roll = randi() % (maximum-minimum+1) + minimum

func _ready():

func roll_a_dice(minimum, maximum):
	self.roll = randi() % (maximum-minimum+1) + minimum
func _on_Area2D_input_event(event):
		roll_a_dice(1, 100)
		self.roll = randi() % (maximum-minimum+1) + minimum
		if event is InputEventMouseButton and event.pressed:
			if cursor_state == "selecting" and roll <= GlobalP.charm: 

As you could see, it’s a bit of a mess, and all i got is that error: ‘emit_signal: Error calling method from signal ‘input_event’: ‘CollisionShape2D(’: Method expected 1 arguments, but called with 3…’

Put some ‘roll’ var a bit everywhere, but what are those arguments in excess causin the error? How to make them legit? And how to type that ‘{’ in godot?

That’s a lot askin, sry. If you got time, cheers!

Syl | 2020-02-15 13:40

The signal message is clear: your handler method _on_Area2D_input_event expects one parameter, while it is called with three (through the signal connection). Looking at the docs for the area2d input event signal, I can see the expected signature is:

 _on_Area2D_input_event( viewport, event, shape_idx )

Regarding the curly braces ‘{’, if you are referring to typing them, that depends on your keyboard layout; on my (Italian) keyboard, the key combination is Shift + whatever you do to type ‘[’ .

You are already aware that you need to clean up roll calls. Notice that class variables mention above are fit for representing some persistent “state” of a class, like life points of a character. Since rolling a dice is a transient result which changes every time (“stateless”), roll_a_dice is more aptly refactored as a simple function, returning a value without setting anything external:

func roll_a_dice(minimum, maximum):
    return randi() % (maximum-minimum+1) + minimum

and then call inside the event handler.

Andrea Giancola | 2020-02-15 16:17

Thxs for your goodwill. :slight_smile: Cleared the area2D issue but i can’t get my roll with the click on it and the two conditions, like this:

extends CollisionShape2D

var maximum = 100
var minimum = 1
onready var cursor_state = get_node("/root/Node2D/Use").cursor_state

func _ready():

func roll_a_dice(minimum, maximum):
	var roll = randi() % (maximum-minimum+1) + minimum
func _on_Area2D_input_event(_viewport, event, _shape_idx):
		roll_a_dice(1, 100)
		if event is InputEventMouseButton and event.pressed:
			if cursor_state == "selecting" and self.roll <= GlobalP.charm: 

Nothin is printed, but the ‘hum’, printed twice, dunno why, although the conditions should be to print self.roll and okokok…

How do you call the area2d event inside the roll_a_dice func?

And for the curly braces, the strange is that i can type it normally with ctrl-alt+4, but in the godot editor, nothin is written.

Syl | 2020-02-15 20:45