How to check if only one variable is true

Started by Miori, Sat 01/03/2014 15:10:42

Previous topic - Next topic

Miori

Hello,

is it possible to make an all around check for an array? For example I have this array:
Code: ags
bool konnichiwa[4]

As you see it is an bool array. I want to check if at least one of the variables in the array is true without checking every single one. It doesn't matter which one is true, it should just run a script if at least one is.

DoorKnobHandle

Nope, there is no shortcut in AGS script. So you're going to have to do it the old-fashioned way:

Code: ags

#define SIZE 4

bool bitvector[SIZE];

int i = 0;
while (i < SIZE)
{
    if (bitvector[i])
    {
        runCode(); // do whatever you want to do
        i = SIZE; // break out of the loop
    }
    i++;
}

Snarky

That doesn't seem to do exactly what Miori was asking for, dkh. I'd try this:

Code: AGS

// Method to count the number of true values in a bool array
int countTrue(bool tests[], int testsSize)
{
    int i=0;
    int count=0;
    while(i<testsSize)
    {
        if(tests[i])
            count++;
        i++;
    }
    return count;
}

// At some point in your code...
{
    // ...
    if(countTrue(konnichiwa, 4) > 0)
    {
        // Do stuff
    }
    // ...
}

DoorKnobHandle

My code checks the boolean array and calls a function as soon as it finds one true value in it. That's what Miori asked for I believe. Your code does the same except it doesn't break out after finding a positive result and instead always goes through the entire thing and reports the amount of true elements in the array. Two different things - but upon rereading the OP I'm pretty sure my code does what Miori wants! :)

As an aside, I had compleletely forgotten whether or not AGS allowed passing arrays of primitive (build in) data types like in your function, Snarky, C-style. I'm too lazy to test it right now but are those values from the array argument passed by value or by reference (the latter being the standard to save memory in other common languages)? Can anybody confirm?

Snarky

Yes you're right. I think I misread the runCode() part of your sample.

I do think it's cleaner to abstract out the test itself, as I have done, than to embed what is really a simple "if(condition)" into some kind of loop-switch construct. It would be easy to modify my version to just return true/false and to stop checking once it finds a true value, but I like the generalizability of the "truth count" (what if next time around you have to check whether they're ALL true?), and honestly, if your array size is on the order of 4, the time saved by skipping the last few tests is completely insignificant.

I didn't remember whether AGS allows passing arrays of built-in types either, but it does. And it certainly does not pass arrays by value (which would involve copying all the items into a new array); are there even any C-type languages that do?

mitlark

I think that if you can't pass an object as a value, you pass a pointer instead. Much better than duplicate the entire object in memory.
SPIN SPIN! SPIN SPIN!

monkey0506

mitlark that doesn't really apply here. Static arrays can't be passed to functions, and dynamic arrays are always passed by reference/pointer. As to the individual bools, AGS doesn't allow pointers to primitive types (or user defined types either).

Miori

Well... sorry, maybe I was a bit short with my explanation. It doesn't have to check the array all the time. Could be bad for the performance maybe, because there will be many of these arrays XD

I have a dictionary in my japanese-learning game and I want to implement a search. If you type a word in, you will jump to the page where the word is. BUT, you have to learn and find the words all over the game. The word itself, meaning, kana & kanji can be found seperatly from each other. Thats the 4 variables in the array. And the search for a word should ONLY work, if one of the 4 things is already known. So you can not search for things you never heard of.

Snarky

Quote from: Miori on Sun 02/03/2014 13:03:40
Well... sorry, maybe I was a bit short with my explanation. It doesn't have to check the array all the time. Could be bad for the performance maybe, because there will be many of these arrays XD

Neither my code nor dkh's "check the array all the time": they both check it once (mine checks the whole array, dkh's stops as soon as it finds a true entry). You don't have to worry about performance on this task: doing four table lookups and checking four booleans is trivially easy for the computer, and takes practically no time whatsoever (the result of the check, to "jump to the page where the word is", probably involves more than a thousand times as much work for the computer, depending on exactly how that happens). And it doesn't matter how many of these arrays you have, since you're only checking one of them at a time. It would only be worth optimizing this task if you were doing it hundreds or thousands of times in a single game loop, but since players can only look up one thing at a time, that's never going to happen.

Quote from: Miori on Sun 02/03/2014 13:03:40
I have a dictionary in my japanese-learning game and I want to implement a search. If you type a word in, you will jump to the page where the word is. BUT, you have to learn and find the words all over the game. The word itself, meaning, kana & kanji can be found seperatly from each other. Thats the 4 variables in the array.

I think this is a good example of a situation where it's better to have a more general "count how many you have" function. It seems very likely that at some point you'll want to check whether the player has collected ALL of the four components, and with the helper function in my example that will be an equally simple one-line expression: you don't have to write another function or another loop.

SMF spam blocked by CleanTalk