Search code examples
godotgdscript

_init() after instancing a scene


I have two scenes. A disorder scene that acts as a class and a disorders scene that initiates all the disorders.

Disorder.gd

extends Node2D

var disorder_name
var disorder_cause
#-----------------Vitals------------------------
var pulse
var O2
var respirations
var systolic
var diastolic
var temperature
var blood_sugar

func _init(disorder_name, cause, pulse = 0, O2 = 0, respirations = 0, systolic = 0, diastolic = 0, temperature = 0, blood_sugar = 0):
    disorder_name = disorder_name
    disorder_cause = cause
    pulse = pulse
    O2 = O2
    respirations = respirations
    systolic = systolic
    diastolic = diastolic
    temperature = temperature
    blood_sugar = blood_sugar

Disorders.gd

extends Node2D

var disorder = preload("res://Scenes/Disorder.tscn")

func _ready():
    var influenza = disorder.instance()
    influenza._init(temperature = 5)

I get

error(7, 30): Unexpected assign.

I am not sure how to _init() the Disorder class in Disorders to be able to assign only affected params based on the Disorder instancing. temperature = 5 would add 5 degrees to the person's normal temperature


Solution

  • GDScript do not support named parameters.

    Which means you cannot skip them.

    If you really want to go for it, you could use a dictionary:

    func _init(disorder_name, cause, params := {}):
        self.disorder_name = disorder_name
        self.disorder_cause = cause
        pulse = params.get("pulse", 0)
        O2 = params.get("O2", 0)
        respirations = params.get("respirations", 0)
        systolic = params.get("systolic", 0)
        diastolic = params.get("diastolic", 0)
        temperature = params.get("temperature", 0)
        blood_sugar = params.get("blood_sugar", 0)
    

    Which you would use like this:

    var influenza = disorder.instance()
    influenza._init("influenza", cause, {temperature = 5})
    

    I don't know what you would put on cause, but notice it was not optional.


    However, given that your default values are zero, it is much simpler to just set them after _init. So _init can be:

    func _init(disorder_name, cause):
        self.disorder_name = disorder_name
        self.disorder_cause = cause
    

    And then:

    var influenza = disorder.instance()
    influenza._init("influenza", cause)
    influenza.temperature = 5
    

    You can, of course, set default values on the declarations, for example:

    var pulse = 0
    

    On a similar note, I want to point out that GDScript supports types. Use them.

    This is a Variant:

    var pulse
    

    This is a Variant with value 0 (int):

    var pulse = 0
    

    This is a Variant with value 0 (float):

    var pulse = 0.0
    

    This is a float with value 0:

    var pulse:float = 0
    

    This is an int with value 0:

    var pulse:int = 0
    

    This is a float with value 0 (the default value):

    var pulse:float
    

    This is an int with value 0 (the default value):

    var pulse:int
    

    This is an int with value 0 (using type inference):

    var pulse := 0
    

    This is a float with value 0 (using type inference):

    var pulse := 0.0