#Function argument

16 messages · Page 1 of 1 (latest)

solar heath
#

So I have a function that needs some arguments like arg1,arg2 and some args have default value

Func function(arg1,arg2=0,arg3):
pass

How do I pass those args that have default value and just give value to other ones like

function(‘string’, arg3=6) skipping arg2

haughty ocean
#

well, first thing is you can't have default values before non-default values

solar heath
#

I know that sorry about it had to be fast

fair flame
#

There are no keywored args like in python

haughty ocean
#

and the... what Dex said

fair flame
#

what you can do is use a dictionary instead

solar heath
#

Do I have to give it every value then?

fair flame
#
func function(args:Dictionary) -> void:
  var arg1 = args.get("arg1", default_value)
  var arg2 = args.get("arg2", default_value)
  if not "arg3" in args:
    push_error("arg3 is needed")
    return
...
#

so you can just do function({"arg3": your_value})

solar heath
#

Thanks its pretty good solution

fair flame
#

see the official docs for more dictionary magic

haughty ocean
fair flame
#

right? gdwheeze

#

sad that Object types doesn't has the same implementation

solar heath