This is more of a “is there a better way” sort of question. I find myself struggling with arrays, loops, and object communication in Unreal Engine 4. Specifically adding elements to an array and pulling information out via a loop. To help my brain, I watched Epic’s live stream on Blueprint communication but it isn’t my mental go-to.
Here’s a scenario: Suppose you have some number of puzzle cubes in a scene which need to be toggled in a specific order; get the wrong order, they all reset. To check this puzzle in game, we’ll need to know the number of cubes and the state of each cube.
In C, I would do it like this:
// loop through all cubes
for(n = 0; n < numCubes; n++)
{
// add cubes to count if toggled on
if(cube[n].isOn && !cube[n].isCounted)
{
cube[n].isCounted = true;
cubesCounted++;
}
// remove cubes from count if toggled off
else if(!cubes[n].isOn && cube[n].isCounted)
{
cube[n].isCounted = false;
cubesCounted--;
}
}
// check puzzle status
if(numCubes == cubesCounted)
puzzleFinished = true;
eles
puzzleFinished = false;
In Unreal, however, I find myself tripping over Blueprint code – like sometimes, I just don’t get how these bubbles communicate. Below is a solution I came up with, but I feel I’m going about it the wrong way. Thoughts?
Huh…that was the first thing I tried but it wouldn’t get the element’s variable; using “get copy” did which is why I used it. I just tried it again though and it works now. Thanks.
// function check for puzzle
int PuzzleFinished()
{
// loop through the cubes;
for(n = 0; numCubes; n++)
{
if(!cube[n].isOn)
return false;
}
// if loop finished
return true;
}
And I was like – of course! So the new blueprint looks like this:
So, as GarnarP57 said, it’s best to run it off an event. The function runs only if we need to know the variable and the loop runs through each of the elements. If at any point the loop finds an element that isn’t on, it breaks the loop and returns false. If the loop finishes, it then returns true since they’ll all be on. Thus far, it works with all of my tests.
Tip:
In that example you actually don’t need a “loop with break” you can simple make a Return false node if IsOn=false. Then the function (and the loop) will end early. If it doesn’t end early it will reach “Completed” and you can return true.
You’re right, because the return would exit the loop upon the first instance of !isOn. Thanks. I’m still trying to wrap my brain around bubble scope. To me, those returns are outside the loop body but I guess not lol.