Why " Invalid set index '_stat'(on base: 'Reference ()') with value of type 'int' " ?

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

While runnin this code :

extends Node2D
class Etat:

    func _init(state,mode):
	    self._stat = state
	    self._mode = mode

var entity=Etat.new(1,"P")

It print :“Invalid set index ‘_stat’(on base: ‘Reference ()’) with value of type ‘int’”.
Can you expain me why and how i can solve this problem please?

:bust_in_silhouette: Reply From: Bean_of_all_Beans

By the looks of it, you never declared _stat nor _mode. It also looks like you may be trying to do this similar to how it is done in Python.

To fix this (assuming you want _stat and _mode to be accessible to the rest of the functions defined under Etat), try doing this:

class Etat:
    var _stat
    var _mode
    
    func _init(state,mode):
        _stat = state
        _mode = mode

You can put self. before _stat or _mode under _init, but it is not entirely necessary. Personally, I do it to help myself remember which functions belong to which class.

Thank you. It works.

JeanKouss | 2020-09-23 12:20