Announcement

Collapse
No announcement yet.

Blueprint Loops, Objects, and Logic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:

    Code:
    // 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?

    Click image for larger version  Name:	Loops2.PNG Views:	0 Size:	151.5 KB ID:	1686196
    Last edited by SeldinG; 11-12-2019, 04:43 PM.

    #2
    Move complicated logic to c++.

    or

    UE4 Blueprints From Hell
    https://blueprintsfromhell.tumblr.com/

    Comment


      #3
      • 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

      Comment


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

        Comment


          #5
          Originally posted by GarnerP57 View Post
          • You don't need the Get node in a ForEachLoop, just use the Array Element
          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.

          Comment


            #6
            Originally posted by Everynone View Post
            Also, the ++ & -- nodes set by reference - no need to Set it again. 2 nodes down, 48 to go...
            Oh, it works that way? Good to know. Thank you.

            Comment


              #7
              Originally posted by empty2fill View Post
              Move complicated logic to c++.
              Agreed but I'm trying to...learn this :/ because I have to teach it and my students go pale at the sight of code.

              Originally posted by empty2fill View Post
              Is there no god?

              Comment


                #8
                Originally posted by SeldinG View Post
                Is there no god?
                On the contrary

                Click image for larger version

Name:	download.png
Views:	438
Size:	365.0 KB
ID:	1686885

                Comment


                  #9
                  A friend of mine suggested this:

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

                  Click image for larger version

Name:	examp2.PNG
Views:	427
Size:	70.5 KB
ID:	1688432

                  Comment


                    #10
                    Originally posted by SeldinG View Post
                    ...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.

                    Click image for larger version

Name:	examp2.PNG
Views:	427
Size:	70.5 KB
ID:	1688432
                    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.

                    Comment


                      #11
                      Originally posted by GarnerP57 View Post
                      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.


                      Comment

                      Working...
                      X