0 votes

Greets!

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?

Cheers!

in Engine by (324 points)

1 Answer

0 votes

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.

by (809 points)

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?

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.

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

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")

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...

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 Player.gd):

var level
var character

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

and you can refer to them from another node as

 get_node("Player").level
 ...

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():
    randomize()

func roll_a_dice(minimum, maximum):
    self.roll = randi() % (maximum-minimum+1) + minimum
    print(roll)

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: 
                print("okokok")

As you could see, it's a bit of a mess, and all i got is that error: 'emitsignal: Error calling method from signal 'inputevent': 'CollisionShape2D(Dryad2.gd)::onArea2Dinputevent': 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!

The signal message is clear: your handler method onArea2Dinputevent 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"), rolladice 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.

Thxs for your goodwill. :) 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():
    randomize()

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: 
                print(self.roll)
                print("okokok")
                   else:
                print("hum")

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 rolladice 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.