+3 votes

How to redefine an expandable class variable?
Expamle:

class Asd:
    var variable = 1

class Dsa extends Asd:
    variable = 2
in Engine by (111 points)

1 Answer

+2 votes
Best answer
class Asd:
    var variable = 1

class Dsa extends Asd:
    func _init(p_variable = 2):
        variable = p_variable

# somewhere in your code

var dsa = Dsa.new(2)
print(dsa.variable) # should print 2
by (1,356 points)
selected by
  1. class Asd:
    var variable = 1

  2. class Dsa extends Asd:
    func init(pvariable = 2):
    self.variable = p_variable #it does not carry self?

    # somewhere in your code

  3. var dsa = Dsa.new(2) print(dsa.variable) # should print 2

It's not necessary in this case. self is needed if you need to distinguish between method's parameter name and member name:

func _init(variable = 2):
    self.variable = variable  # notice no `p_` prefix
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.