godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.36k stars 21.25k forks source link

Error assigning boolean properties of typed class instance #98830

Open kester-habermann opened 2 weeks ago

kester-habermann commented 2 weeks ago

Tested versions

Godot Engine v4.3.stable.official.77dcf97d8 Godot Engine v4.4.dev3.official.f4af8201b

System information

Godot v4.4.dev3 - Debian GNU/Linux 12 (bookworm) 12 on X11 - X11 display driver, Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4080 SUPER (nvidia) - AMD Ryzen 9 5900X 12-Core Processor (24 threads)

Issue description

This code does not compile:

extends Node3D

func _ready() -> void:
        var material := ORMMaterial3D.new()
        material.transparency = true

The editor shows this error:

Line 5:Cannot assign a value of type "bool" as "BaseMaterial3D.Transparency".
Line 5:Value of type "bool" cannot be assigned to a variable of type "BaseMaterial3D.Transparency".

changing it to this, it works:

extends Node3D

func _ready() -> void:
    var material = ORMMaterial3D.new()
    material.transparency = true

This code has the same problem:

func _ready() -> void:
    var mesh_instance := MeshInstance3D.new()
    mesh_instance.cast_shadow = false

Assignment of other non-boolean attributes works without problems when using the typed instance, e. g. this works:

func _ready() -> void:
    var mesh_instance := MeshInstance3D.new()
    mesh_instance.position = Vector3.ZERO

Steps to reproduce

  1. Create a blank new project
  2. Add a Node3D
  3. Attach a script
  4. Add the above method
  5. Error appears right away in editor (even before attempting to run project)

Minimal reproduction project (MRP)

new-game-project.zip

RedMser commented 2 weeks ago

This is not a bug, the fields you mentioned are not booleans but enums^1. It just so happens to work due to implicit conversions making OFF = 0 = false and ON = 1 = true.

But with strict typing (e.g. gdscript knows that there is a type mismatch), it definitely should error for these assignments out of the box. Not every enum represents "on/off with additional values", so IMO it's a feature request to allow these conversions with strict typing enabled.

kester-habermann commented 2 weeks ago

My bad, I didn't look closely enough at the documentation. I was really under the impression that these were supposed to be booleans. Thanks for the quick reply! I try it with strict typing and using the correct enum values.

MarcusPaulsson commented 2 weeks ago

I guess this is not a bug but it seems like the enum types for these are not explicit, During explicit casting, it still seems like you can declare any int's as bool enum's.

dalexeev commented 2 weeks ago

We have some inconsistency with implicit conversions (between bool, int, float, and enums; String, StringName, and NodePath; Array, and Packed*Array; etc.). Sometimes the analyzer reports an error where it works at runtime for untyped code. I am not a fan of implicit conversions, but I think we should approach this in a consistent way.

There probably shouldn't be a compile-time error if it works at runtime, but warnings would be useful in some cases. There are already NARROWING_CONVERSION and INT_AS_ENUM_WITHOUT_CAST, but they don't cover all cases. But a general IMPLICIT_CONVERSION warning would be annoying for many users, especially in cases int to float, String to StringName, String to NodePath. The warning would only be suitable for strict mode, like INFERRED_DECLARATION.