Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript 2.0: Unknown Function convert() in @GDScript scope #65919

Closed
vonflyhighace2 opened this issue Sep 16, 2022 · 13 comments · Fixed by #78882
Closed

GDScript 2.0: Unknown Function convert() in @GDScript scope #65919

vonflyhighace2 opened this issue Sep 16, 2022 · 13 comments · Fixed by #78882

Comments

@vonflyhighace2
Copy link

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

@Calinou Calinou changed the title Unknown Function in GDScope GDScript 2.0: Unknown Function convert() in @GDScript scope Sep 16, 2022
@KoBeWi
Copy link
Member

KoBeWi commented Sep 16, 2022

Seems related (same?) to #5523

@vonflyhighace2
Copy link
Author

vonflyhighace2 commented Sep 16, 2022

@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.

@QushyQushy
Copy link

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.

@vonflyhighace2
Copy link
Author

@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

@QushyQushy
Copy link

@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.

@stur86
Copy link

stur86 commented Sep 19, 2022

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.

@akien-mga
Copy link
Member

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().

@MikeSchulze
Copy link

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

@Zireael07
Copy link
Contributor

@MikeSchulze what are you using it for?

@MikeSchulze
Copy link

@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.

@akien-mga
Copy link
Member

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.

@MikeSchulze
Copy link

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.

@aaronfranke
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants