GameMaker [Solved] Optional arguments not working correctly

D

dozy

Guest
[GameMaker Studio 2 version 2.2.3.436] I have made a script that uses optional arguments and it keeps giving me an error,
"function 'hitbox_create' expects 4 arguments, 6 provided."
Four arguments are supposed to be required, and two more optional. This is really baffling me because I have made other scripts with optional arguments that work perfectly. In fact, a lot of the code in this script was copy & pasted from another of said scripts to make sure it was the same, I can see no real difference in the way it works.
The section which uses optional arguments is this:
Code:
if (argument_count > 4) { hb.dmg_interupt        = argument[4]; }
if (argument_count > 5) { hb.dmg_knockback    = argument[5]; }
I have checked and checked again but I cannot find any reason for this not working, or even anyone else who seems to be having a problem doing this right. I hope I am just being stupid and there is something obvious here I'm missing.
Just in case it's relevant, I'll show the code that calls the script:
Code:
var hx = x;
var hy = y-54;
hitbox_create(hx,hy,obj_hitbox_swing,dmg,dmg_interupt,dmg_knockback);
The whole script itself:
Code:
///@func    hitbox_create(x,y,type,damage,[interupt],[knockback]);
///@desc    Create a hitbox and set its values
///
///@param    x
///@param    y
///@param    type
///@param    damage
///@param    [interupt]
///@param    [knockback]

var hb = instance_create_layer(argument0,argument1,layer,argument2);

hb.owner    = id;
hb.dmg        = argument3;

if (argument_count > 4) { hb.dmg_interupt        = argument[4]; }
if (argument_count > 5) { hb.dmg_knockback    = argument[5]; }
And finally, just for comparison, another script of mine which I can confirm has functioning optional variables:
Code:
///@func    apply_damage(damage,[interupt],[knockback]);
///@desc    Apply an amount of damage and interupt frames
///
///@param    damage
///@param    [interupt]
///@param    [knockback]

with (other.owner) {
    hp -= argument[0];
    if(argument_count > 1) { alarm[11]     = argument[1]; interupted = true; }
    else exit;
}

if(argument_count > 2) apply_knockback(argument[2]);
 

Relic

Member
From manual:

NOTE: You cannot mix the two types of argument variables when calling a script. You must use either argument0 ... argument15 or argument[0 ... max_num].

Try changing argument0, argument1 and argument2 into argument[0], argument[1] and argument[2].
 
Yeah, I remember dealing with this. It's confusing when you don't know about it, but yeah, Relic's onto it. Just get into the habit of always using argument[ ] and you'll never run into it again.
 
This fixed an error I was seeing in Feather Messages "gamemaker cannot mix argument# and named parameters"

Even though my game still worked, I changed my code to:

GML:
argument[0] = bulletType;
... in order to clear out that error in Feather Messages.
 
Top