实例化场景后的 _init()

发布于 2025-01-20 17:37:28 字数 1074 浏览 1 评论 0原文

我有两个场景。充当阶级和疾病 s 场景的疾病场景,引发了所有疾病。

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

我得到

错误(7,30):意外分配。

我不确定如何_Init() 障碍 disororders中的类能够仅基于dimander分配受影响的参数实例。 温度= 5会在人的正常温度中增加5度

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-01-27 17:37:28

gdscript不支持名为参数

这意味着您不能跳过它们。

如果您真的想使用它,则可以使用词典:

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)

您会这样使用它:

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

我不知道您会在原因上使用什么,但是请注意,它不是可选的。


但是,鉴于您的默认值为零,只需在_Init之后设置它们要简单得多。因此,_init可以是:

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

然后:

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

您当然可以在声明上设置默认值,例如:

var pulse = 0

在类似的说明上,我想指出 gdscript支持类型。使用它们。

这是一个变体:

var pulse

这是一个具有值0(int)的变体:

var pulse = 0

这是一个具有值0(float)的变体:

var pulse = 0.0

这是一个带有值的浮点0

var pulse:float = 0

这是一个具有值0的INT:

var pulse:int = 0

这是一个带有值0(默认值)的浮子:

var pulse:float

这是一个具有值的INT 0(默认值):

var pulse:int

这是一个具有值0(使用类型

var pulse := 0

推荐)的INT:这是一个带有值0的浮点:

var pulse := 0.0

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文