I'm trying to make a function that looks for a value in an array, but I'm uncertain of how returning values work.
Here's what the function looks like, roughly:
function searchArray (int whichvalue) {
int search = 0;
while (search > -1) {
search ++;
if (array[search]==whichvalue) {
? return true; ?
}
if (search == 100) { // 100=array size
? return false; ?
}
}
}
Here's how I'd like to use it:
if (searchArray(1)==true) Display ("bla bla bla");
But as I said, I'm uncertain of how a certain value is returned. I'd just like to return a true/false value depending on if the wanted value is found in the array or not.
function searchArray (int whichvalue) {
int search = 0;
bool doit;
while (search > -1) {
search ++;
if (array[search]==whichvalue) {
doit=true;
return doit;
}
if (search == 100) { // 100=array size
doit=false;
return doit;
}
}
}
It would be better to return the int so you can check with values 1 or 0. But there it is with boolean.
Yeah I might as well use an int. Thanks for the example!
In this case returning the wrong type should work, but in general, if the function is supposed to return a boolean value, it has to look like this:
boolean ArrayContains (int whichvalue) {
int search = 1;
while (search <= 100) {
if (array[search] == whichvalue) return true;
search++;
}
return false;
}
function is merely a substitute for int; in theory, every function should use the return type instead of "function" or "void" if it isn't supposed to return anything.
To clarify that the type for boolean in AGS is bool so it would be:
bool ArrayContains(int whichvalue) {
Also, in AGS the following types all have implicit conversions to int:
bool
char
short
(any enumerated type)
So returning any of those types of values from a function would work. However if you want to return any pointer (such as a Character*, InventoryItem*, etc.), a String, or a float then you must specify the exact type you are working with as there is no implicit conversion possible.