+1 vote

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?

in Engine by (16 points)

1 Answer

+1 vote
Best answer

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.

by (329 points)
selected by

Thank you. It works.

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.