Can I call a function giving descriptive labels to parameters?

Godot Version

4.3

Question

If I have a function

func move_sprite(sprite_moved: bool, new_pos: Vector2, SP_name: String):

The function call ends up looking like

func move_home()
    move_sprite(true, Vector2(0,0), "Home")

It’s hard to know what the parameters refer to without looking at the function.

Is it possible to somehow add descriptive labels to the function call?

The lines below are incorrect, I know, just illustrations of what I’m thinking of. Is something similar to one of these options possible?

move_sprite(sprite_moved: true, new_pos: Vector2(0,0), SP_name: "Home")
move_sprite("sprite_moved" true, "new_pos" Vector2(0,0), "SP_name" "Home")
move_sprite(#sprite_moved# true, #new_pos# Vector2(0,0), #SP_name# "Home")

Thanks
Toby

1 Like

Yea, it would be nice that if you declare the type in the function like you have. Then it would create a autocomplete in the editor when the function is used. :thinking:

No, is not possible, what u can do is put a commentary before the function call or use a dictonary instead:

func move_home() -> void:
	var dict_parameters: Dictonary
	dict_parameters["sprite_moved"] = true
	dict_parameters["new_pos"] = Vector2.ZERO
	dict_parameters["SP_name"] =  "Home"
	move_sprite(dict_parameters)

func move_sprite(dict_parameters: Dictionary) -> void:
	# Do your stuff
1 Like

This called (essentially) “named parameters” or “named arguments”.
It looks as though implementing this is not going to happen.
This proposal talks about it.

The dictionary idea is one workaround and another I saw posted was:

move_sprite(  
   true,   #sprite_moved   
   Vector2(0,0),  #new_pos 
   "Home" #SP_name 
)
2 Likes

Thanks for responding.

Though the link you gave, I have discovered that what I’m imagining are called “inline parameter annotations”, “inline decorations”, “inlay hints” or “inline hints”. They’re added by the editor and are non-editable.

I’ve created a discussion with an example from VSCode and a mockup of what it would look like in the Godot editor.

I like it. Its a good idea so I upvoted it.

1 Like

Godot should implement it to attract more people. If you are a truly engineer you would need that. Using dict for arguments is sick for complex project.