godotengine / godot

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

Cryptic GDScript Error Message for A Beginner Programming Mistake - "Unexpected token" #40411

Open tdnzr opened 4 years ago

tdnzr commented 4 years ago

Godot version: v3.2.2 stable OS/device including version: Windows 7

Issue description:

This 1-line beginner program throws a cryptic error message:

print("Hello World")

Corresponding error message:

"Unexpected token: Built-In Func:"

And it's elaborated on in the Errors tab as:

"get_token_identifier: Condition "tk_rb[ofs].type != TK_IDENTIFIER" is true. Returned: StringName()"

Expected Behavior: GDScript is advertised to new users for its similarity to Python. So it's bad when trivial code doesn't work for incomprehensible reasons - nobody should have to check documentation or have to google to debug a "Hello World" program.

How to Fix it: GDScript doesn't like the code above because you aren't allowed to do much of anything but define variables or functions at the base level of a script. So either make the reason for this specific error message crystal clear, or make the "Unexpected token" error message in general more clear.

Elaboration: Comprehensible error messages are very important for programming. Personally, I tried Godot Engine and GDScript for the reasons mentioned above, then got this bizarre error even when following basic, community-approved programming tutorials. For instance, I followed this tutorial by GDQuest (https://www.youtube.com/watch?v=UcdwP1Q2UlU ), and copying the code on most of the slides in that video causes the error mentioned in this issue, unless you code inside a function like _ready. Naturally, an incomprehensible error message in that situation is really bad.

Calinou commented 4 years ago

Related to https://github.com/godotengine/godot/issues/27702.

ee0pdt commented 4 years ago

The editor highlights issues like this immediately and is very helpful in showing incorrect syntax usage. "Unexpected token" is a common error shared by at least Python and Javascript, but I agree that the message is cryptic for a beginner.

To give a clearer message the parser would need to understand the context here and I'm not sure how easy that is to achieve.

Calinou commented 3 years ago

@vnen Do you know how difficult would it be to detect method calls outside of functions and print a more helpful error message accordingly?

vnen commented 3 years ago

@Calinou should be simple to add this. The Unexpected token error is the catch-all but we can add clearer errors when the context can be inferred.