Multiple script arguments malfunction.

Edwin

Member
Hello, guys.

I made a script that has multiple arguments, it looks like this:
Code:
/// val_highest(value1, value2, value3, ...)

for (var i = 0; i < argument_count; i ++) {
    for (var j = 0; j < argument_count; j ++) {       
        if (argument[i] > argument[j]) {
            return argument[i];
        } else {
            break;
        }
    }
}
Basically this script just returns the highest value (i guess, if it works)

How to draw text using this script? I tried this in Create event:
Code:
var count, equivalent;

count = get_integer("Please input the values count", 2);

for (var i = 0; i < count; i ++) {
    equivalent[i] = get_integer("Please input the equivalent for value "+string(i+1), 1);
}
This above code asks user the count of values and then asks what do they equal to.

How can I draw the text then? I can't input the arguments of script as the equivalent[...] array.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Since there is no way to expand array contents into a comma-delimited list (as code - outside of strings), instead of passing one or more arguments to the script, you could pass the array itself to the script.
 

Edwin

Member
Since there is no way to expand array contents into a comma-delimited list (as code - outside of strings), instead of passing one or more arguments to the script, you could pass the array itself to the script.
How can I perform it?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Pass the array itself, using the variable that stores the array, to the script as a single argument. Said array can have any given amount of indices. You can then handle it like the argument array and retrieve its size using the corresponding function array_length_1d.
 

Edwin

Member
Pass the array itself, using the variable that stores the array, to the script as a single argument. Said array can have any given amount of indices. You can then handle it like the argument array and retrieve its size using the corresponding function array_length_1d.
Thank you. It worked. I guess this would be useful for ds lists too.

For reference:
Code:
/// return_highest_array(array)

// Create local array variable
var array = array_create(array_length_1d(argument0));

// Set array values
for (var i = 0; i < array_length_1d(array); i ++) {
    array[i] = argument0[i];
}

// Return the highest value of array
for (var j = 0; j < array_length_1d(array); j ++) {
    for (var k = 0; k < array_length_1d(array); k ++) {
        if (array[j] >= argument0[k]) {
            return array[j];
        } else {
            break;
        }
    }
}
By the way, can I get the array-argument's value if argument is already an array? Like: argument[0][j]

You know I can't use more than 15 arguments using argument0, argument1, etc.
 

CloseRange

Member
By the way, can I get the array-argument's value if argument is already an array? Like: argument[0][j]
i do belive it works but you might have to do argument[0, j]

I personally like returning objects with values in them (just remembering to delete them after and disable them before) because I can pass and return as much information as I need. But that's a personal preference.

maybe off topic but I don't think your script works like you think it does.
Code:
for (var j = 0; j < array_length_1d(array); j ++) {
    for (var k = 0; k < array_length_1d(array); k ++) {
        if (array[j] >= argument0[k]) {
            return array[j];
        } else {
            break;
        }
    }
}
just looking at that part I don't feel like it actually returns the highest value.
maybe I'm wrong but I'd say just store 1 variable like var highest = 0, loop through the array once and change the value of highest if it's the highest.
Then you can just return highest.
your whole code could just look like this honestly:
Code:
/// return_highest_array(array);
var h = 0;
for(var i=0; i<array_length_1d(argument[0]); i++) {
     h = max(h, argument0[i]);
return h;
 

Edwin

Member
i do belive it works but you might have to do argument[0, j]

I personally like returning objects with values in them (just remembering to delete them after and disable them before) because I can pass and return as much information as I need. But that's a personal preference.

maybe off topic but I don't think your script works like you think it does.
Code:
for (var j = 0; j < array_length_1d(array); j ++) {
    for (var k = 0; k < array_length_1d(array); k ++) {
        if (array[j] >= argument0[k]) {
            return array[j];
        } else {
            break;
        }
    }
}
just looking at that part I don't feel like it actually returns the highest value.
maybe I'm wrong but I'd say just store 1 variable like var highest = 0, loop through the array once and change the value of highest if it's the highest.
Then you can just return highest.
your whole code could just look like this honestly:
Code:
/// return_highest_array(array);
var h = 0;
for(var i=0; i<array_length_1d(argument[0]); i++) {
     h = max(h, argument0[i]);
return h;
Yeah, you're right. I don't really know why my old script wasn't returning the highest value =\
 
T

Timothy

Guest
Yeah, you're right. I don't really know why my old script wasn't returning the highest value =\
Because you were returning as soon as you found a value higher than the first value in the argument list.

your whole code could just look like this honestly:
Code:
/// return_highest_array(array);
var h = 0;
for(var i=0; i<array_length_1d(argument[0]); i++) {
h = max(h, argument0[i]);
return h;
There is a problem here if all values in the array are < 0. Its easily fixed by initializing "h" as the first value in the array and then looping through the remaining values.

P.S. Also, unless you actually need to pass in an array, you guys are just re-implementing what the function "max" already does.
 

CloseRange

Member
P.S. Also, unless you actually need to pass in an array, you guys are just re-implementing what the function "max" already does.
The whole problem in the first place was him having problems because he had to use arrays. So yes I assume he needs arrays.

Also yes it will break.
But yours will break if the isn't anything in the array ;) so best we also add
Code:
if(array_length_1d(array) == 0) return 0;
to the begining.
At that point we should probs also store the length of the array in a temp var.
 

TsukaYuriko

☄️
Forum Staff
Moderator
By the way, can I get the array-argument's value if argument is already an array? Like: argument[0][j]
Yes, but since chained array accessors are not yet implemented, you would have to first store the array that is stored in argument[0] in a temporary variable. You can then access that array's indices using variable[n].
 
Top