Godot: str2var raises a blocking script error when passed an invalid string, should maybe return null

Created on 21 Sep 2017  路  11Comments  路  Source: godotengine/godot

Because GDScript does not have Try... Except... Causes the following code to handle errors

func _ready():
var db = str2var("")
print("is error?",db)

enhancement junior job gdscript

Most helpful comment

Alright, now I get it :)

It's actually not due to passing an empty string but any kind of invalid string (i.e. that does not match what var2str would produce for a given Variant type): https://github.com/godotengine/godot/blob/8632408dbd50223256a140940a5f02fb040a79da/modules/gdscript/gd_functions.cpp#L656-L681

Instead of raising a blocking error, it could maybe return Variant() (null) and just raise a non-blocking error in the debugger.

All 11 comments

I don't understand. What's the problem?

Oh. str2var raises an error with empty string.

@akien-mga
There is no output here. The program will host,db should be null

@Dillybob92 You're code is fine. The string has a length of zero therefore it will never enter the if statement.

If you put this instead, it'll enter the else portion of the code.

    var db = ""
    if db.length() != 0:
        db = str2var("")
        print("is error?",db)
    else:
        print("db is 0 length as expected!")

Edit: Did you mean to put "db.length() == 0"

Yep, I think you meant to put "db.length() == 0" :)

The more you post, the farther we go from understanding what this is all about :D

What's the problem with the original code? What does db contain? To quote the issue template that was completely ignored:

  • Issue description: What happened, and what was expected.
  • Steps to reproduce:

@Dillybob92 I should've read more of the above post and I probably would've seen what you were communicating more clearly ;)

@akien-mga Here's the problem quite simply:

func _ready():
    var db = str2var(some_var) # If some_var is an empty string then an exception is thrown here that there's no way to detect.
    print("is error?",db) # This line is never reached if some_var = "" (or any non value string, IE. "fals" would also choke, but "false" would not)

So, exception handling would be the ultimate solution overall, but I think in the meantime I agree that it could simply return null.

Returning -1 wouldn't be a good idea since you don't know whether it failed or if your value in the string is actually -1

Alright, now I get it :)

It's actually not due to passing an empty string but any kind of invalid string (i.e. that does not match what var2str would produce for a given Variant type): https://github.com/godotengine/godot/blob/8632408dbd50223256a140940a5f02fb040a79da/modules/gdscript/gd_functions.cpp#L656-L681

Instead of raising a blocking error, it could maybe return Variant() (null) and just raise a non-blocking error in the debugger.

@akien-mga The decoding error should return null

Sorry, my English is poor in water expression. I submitted a translation tool to issues

Taking up this one

Fixed by #22934.

Was this page helpful?
0 / 5 - 0 ratings