Download

Blueprint Loops, Objects, and Logic

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?

Move complicated logic to c++.

or

UE4 Blueprints From Hell

  • You don’t need the Get node in a ForEachLoop, just use the Array Element
  • You should never use GetAllActorsOfClass in a Blueprint Tick function
  • Make the logic Event based so it only has to check when a variable changes
  • As already suggested move this to C++ as it would only need a few lines of code compared to 50 nodes with spaghetti

Also, the ++ & – nodes set by reference - no need to Set it again. 2 nodes down, 48 to go…

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.

Oh, it works that way? Good to know. Thank you.

Agreed but I’m trying to…learn this :confused: because I have to teach it and my students go pale at the sight of code.

Is there no god?

On the contrary

download.jpg

A friend of mine suggested this:


// 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.