godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript 2.0: Unknown Function `convert()` in @GDScript scope

Open vonflyhighace2 opened this issue 3 years ago • 7 comments

Godot version

4.0-beta1

System information

Zorin OS-16, Intel hd 5500HD

Issue description

Some Global Scope functions are "unknown" in GDscript. "convert", function is one such function. Not sure how/why this is possible. Screenshot from 2022-09-15 11-32-42 Screenshot from 2022-09-15 11-34-23 Screenshot from 2022-09-15 11-34-34

Steps to reproduce

Use the convert function to any meaningful task should trigger the error.

Minimal reproduction project

No response

vonflyhighace2 avatar Sep 16 '22 18:09 vonflyhighace2

Seems related (same?) to #5523

KoBeWi avatar Sep 16 '22 20:09 KoBeWi

@KoBeWi. It indeed looks to be the same. I guess a decision whether to remove or improve it was not reached since this was from a year ago. I say just nuke the thing. One less "conversion" function to worry about among the many we already have. I guess not many used it either.

vonflyhighace2 avatar Sep 16 '22 20:09 vonflyhighace2

I have the same issue, I can't convert a variant from a Javascirpt.eval() rerturn to int in any way. This is VERY urgent for me so any kind of advice would be appreciated. I'm trying to get a browser parameter value.

QushyQushy avatar Sep 18 '22 09:09 QushyQushy

@QushyQushy just type cast it. Use the as keyword or int(variant) or if you want some convoluted way use var_to_str() and then convert the str to int

vonflyhighace2 avatar Sep 18 '22 18:09 vonflyhighace2

@QushyQushy just type cast it. Use the as keyword or int(variant) or if you want some convoluted way use var_to_str() and then convert the str to int

It returns null sometimes so that was the issue. I just used an if statement.

QushyQushy avatar Sep 18 '22 18:09 QushyQushy

Just wanted to point out that int("2") for example does not actually work to convert a string to number, I needed to also use str_to_var, so honestly either a working convert or a better casting system sound like a necessity to me.

stur86 avatar Sep 19 '22 14:09 stur86

It's not very clear in the screenshots what types you are actually trying to convert and how.

But if you have a string like "2" that you want to convert to an int, use "2".to_int().

akien-mga avatar Sep 19 '22 14:09 akien-mga

PLEASE don't remove the convert function, i need it too! It is very helpfull to have such utility.

MikeSchulze avatar Oct 09 '22 08:10 MikeSchulze

@MikeSchulze what are you using it for?

Zireael07 avatar Oct 09 '22 09:10 Zireael07

@MikeSchulze what are you using it for?

I build the plugin GdUnit3 and current on migrate to Godot4. I have a GdScript parser implemented to parse out a function descriptor to hold all informations about all argument type and defaults.

class_name GdFunctionArgument
extends RefCounted

var _name: String
var _type: int
var _default_value :Variant

const UNDEFINED :Variant = "<-NO_ARG->"
const ARG_PARAMETERIZED_TEST = "test_parameters"

func _init(name :String, type :int = TYPE_MAX, default_value :Variant = UNDEFINED):
	_name = name
	_type = type
	_default_value = default_value

func name() -> String:
	return _name

func default() -> Variant:
	prints("get converted type %s of %s" % [_type, _default_value])
	return convert(_default_value, _type)

By nature, the parser reads everything from a file as string and I have to convert the default arguments final from string to the real type.

MikeSchulze avatar Oct 09 '22 09:10 MikeSchulze

Did you try using str2var (3.x) / str_to_var (4.x)? This is the supported way to convert a string representation to a Variant.

akien-mga avatar Oct 09 '22 16:10 akien-mga

Yes i use currently str_to_var but this is not save enough, i wrote my own convert now to handle the edge cases as well. e.i. to convert a string "1" could be mean int could also mean a float. I have the type already extracted from the scripts and want to convert in the right value. So have a convert function with a type as argument would be great.

MikeSchulze avatar Oct 09 '22 16:10 MikeSchulze

In #70080 I am proposing to replace the existing convert method with a new type_convert method. This method will always succeed and it also has a clearer name to make it obvious that we are converting types.

aaronfranke avatar Jan 20 '23 21:01 aaronfranke