godotengine / godot

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

GDSCript: Object.new() throws Parse Error #41462

Closed sanchopanca closed 4 years ago

sanchopanca commented 4 years ago

Godot version: 3.2.2.stable.official

OS/device including version: Windows 10, PC

Issue description: An attempt to create an instance of Object by calling Object.new() ends with the following error: Parse Error: Static constant 'new' not present in built-in type Object This contradicts the documentation of Object that mentions the following:

You can construct Objects from scripting languages, using Object.new() in GDScript, new Object in C#, or the "Construct Object" node in VisualScript.

Steps to reproduce: Put the following in to object-new.gd:

extends MainLoop

func _init():
    var o = Object.new()

Then run:

godot --no-window --script object-new.gd

Minimal reproduction project: object-new-bug-report.zip

amoriqbal commented 4 years ago

Confirmed on Linux Mint. Godot 3.2.2

bug#41462confirm

But the error is different in Godot 4.0

bug#41462confirm4 0

Calinou commented 4 years ago

I'm not 100% sure if this is a bug or the documentation being incorrect. Does it work if you try to create a Reference or a Resource instead? (You're more likely to use a Reference than an Object in a real-world project due to automatic memory management.)

bojidar-bg commented 4 years ago

I would argue it is a bug, since it is possible to create a plain Object through other means (thus it is not impossible to have one by-design) and since it is not ambiguous what the user wants when doing Object.new().

For an example of creating a plain object, consider:

extends Node

class X extends Object:
    pass

func _ready():
    var obj1 = ClassDB.instance("Object")
    print(obj1) # [Object:XXXX]

    var obj2 = X.new()
    obj2.set_script(null)
    print(obj2) # [Object:XXXX+1]
sanchopanca commented 4 years ago

Does it work if you try to create a Reference or a Resource instead?

Yes it works, as well as any other subclass of Object it seems.

Xrayez commented 4 years ago

I say it's a bug even if the use cases for instantiating a pure Object reaches 0, but I'm sure there are. In C++ it's certainly possible to instantiate an Object:

https://github.com/godotengine/godot/blob/58f5c2bab6f03e0c887447532832b18ac76ad250/tests/test_validate_testing.h#L110

In theory you can use this as some kind of a placeholder. Creating pure objects may also be useful for mock testing. Or use objects as struct data with set_meta and get_meta. Dictionaries are better suited for this, but you may lack more advanced functionality by attaching a script with set_script to extend an object at run-time.

Forbidding the instantiation of other "abstract" classes like Reference or Resource from script altogether may also end up more work than allowing instantiation of Object alone, so lets not strip advanced functionality for the sake of making Godot "beginner friendly", I'd still like to use some advanced features while I still can, even if they're not completely obvious or not documented.

In any case, we're talking about completeness. The fact that this issue was reported tells there's something which got a user feel empty inside. 😄