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

Get Function Argument Type(s) #3614

Closed
CrezyDud opened this issue Nov 30, 2021 · 12 comments
Closed

Get Function Argument Type(s) #3614

CrezyDud opened this issue Nov 30, 2021 · 12 comments

Comments

@CrezyDud
Copy link

CrezyDud commented Nov 30, 2021

Describe the project you are working on

Console where you can call functions from ingame

Describe the problem or limitation you are having in your project

i need to get the argument types
preferably without creating an array and hardcoding that stuff in it

Describe the feature / enhancement and how it helps to overcome the problem or limitation

can't do that know (as far as i know)
would be happy for this, so i wouldn't have to hardcode that in some array

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

func clear(bg: Color)
 blabla

func entered_command(commands)
 blabla
 if command in self:
  for i in get_func_arg_types(command).size():
   if args[i] !is get_func_arg_type(command, i)
    return
 callv(command, args)

If this enhancement will not be used often, can it be worked around with a few lines of script?

not as far as i know
other than some shiddy spereate array

Is there a reason why this should be core and not an add-on in the asset library?

no other way of doing it as far as i know
without extra manual work every time i add/modify a function

@CrezyDud
Copy link
Author

CrezyDud commented Nov 30, 2021

so, in total all i need is:
array: get_func_arg_types(function: string)
idk: get_func_arg_type(function: string, argument: int) <- argument: int starts at 0

get_func_arg_type(function: string, argument: int) isn't really needed,
because we could just do get_func_arg_types(function: string)[argument: int]

@Xrayez
Copy link
Contributor

Xrayez commented Nov 30, 2021

Such ability already exists via ClassDB and/or Script API:

extends Node2D

func _ready():
	for method in get_script().get_script_method_list():
		if method.name == "clear":
			print(method.args)
			assert(method.args[0].type == TYPE_COLOR)

func clear(bg: Color):
	pass

However, this doesn't seem to provide proper information about method signatures ("bg" variable is empty), may be a bug. But this properly returns the type.

Note that when you print the entire array, it may not print the info properly for some reason, which may be a bug in data representation (which requires evaluating/stringifying nested data structures recursively).

Works in Godot 3.x for me.

@CrezyDud
Copy link
Author

i used to use get_method_list()
which seems to be different from get_script_method_list()
lol

@CrezyDud
Copy link
Author

this worked so far

@Xrayez
Copy link
Contributor

Xrayez commented Nov 30, 2021

Yeah, get_method_list() would return method info for the built-in class only. I kind of agree that there's a potential discrepancy for built-in vs custom/scripted classes, see proposals like #2804.

@CrezyDud

This comment has been minimized.

@Xrayez
Copy link
Contributor

Xrayez commented Nov 30, 2021

In cases like this you can infer the required number of args required_argcount = argcount - default_argcount

But looks like get_script_method_list() does not populate default_args (always empty), again likely a bug.

@CrezyDud

This comment has been minimized.

@Xrayez
Copy link
Contributor

Xrayez commented Nov 30, 2021

typeof(args[i]) always returns 4

Expected, because typeof() is meant to be used on object instances, but args only provides type information. You should use args[i].type in this case and see if it matches with any TYPE_* constants at global scope.

For the record, it wasn't me who marked your previous message as off-topic.

@CrezyDud
Copy link
Author

For the record, it wasn't me who marked your previous message as off-topic.

that was me, trying to keep this clean, yet my problems solved

@CrezyDud
Copy link
Author

Invalid index 'type' (on base: 'String')

@KoBeWi
Copy link
Member

KoBeWi commented Nov 30, 2021

As discussed above, this already exists, but is bugged.
I found some old issue: godotengine/godot#33624
Closing.

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

No branches or pull requests

3 participants