# Variable in for loop changing

**URL:** <https://forums.unrealengine.com/t/variable-in-for-loop-changing/873555>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [April 7, 2023, 4:12pm UTC](https://forums.unrealengine.com/t/variable-in-for-loop-changing/873555 "2023-04-07T16:12:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinestCreative](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finestcreative/32/1034971_2.png) [@FinestCreative](https://forums.unrealengine.com/u/FinestCreative)\
**Post date:** [April 7, 2023, 4:12pm UTC](https://forums.unrealengine.com/t/variable-in-for-loop-changing/873555/1 "2023-04-07T16:12:26Z")

</div>

In my chess game I created an AI that now can look 3 steps into the future. Each step I update the the variable `Moves` in `chess_piece` that contains an array of moves a `chess_piece` can make.

The problem is, if I update the `Moves` for `Board.Pieces` deeper in my algorithm, it also seems to update the `NewPieces` in the for loop at the beginning of the algorithm. Is there a way so that `NewPieces` in this for loop does not update if I update the pieces later on in the algorithm?  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/5/e95c03cb8da3d4bec74fa54c4486912b94cdadc6.png)

The entire recursive function for my algorithm  
`Board.UpdateBoardState(Depth)` updates the array `Moves` for all `chess_piece` in `Board`

```auto
GenerateAlphaBetaTree(Board:board,Depth:int, Alpha:int, Beta:int)<suspends>:tuple(chess_piece,move,int)={
        
        if(Mod[PositionsChecked,25] = 0):
            #Print("positions checked = {PositionsChecked}")
            Sleep(0.003)
        if(Depth = 0){
            set PositionsChecked += 1
            Score := EvaluateState(Board,Board.Turn)
            if(Board.Turn= color.White):
               # Print("awe at root with score {Score} for color white")
            else:
               # Print("awe at root with score {Score} for color black")
            return (chess_piece{},move{},Score)
        }
        else:
            set Nodes += 1
            var Alphaa:int = -10000
            var Betaa:int = 10000
            var X:int = 0
            var Best:tuple(chess_piece,move,int) = (chess_piece{},move{},-10000)
            var NumPositions:int = 0
            if(Board.Turn = AIColor):
                set Best = (chess_piece{},move{},-10000)
                set X = -10000 
            else:
                set Best = (chess_piece{},move{},10000)
                set X = 10000

            Board.UpdateBoardState(Depth)
            NewPieces:=Board.Pieces
            for(Piece:NewPieces):
                if(Piece.Color = Board.Turn):
                    for(Move:Piece.Moves):
                        set NumPositions += 1
                        #Print("move {Move.Start} to {Move.Target} total moves for this are {Piece.Moves.Length} wi d {Depth}")
                        Tuple := SimulateMove(Board,Piece,Move.Target)
                        MaybeSlainPiece := Tuple(0)
                        MaybePromotedPiece := Tuple(1)

                        set Board.Turn = FlipColor(Board.Turn)

                        Triple := GenerateAlphaBetaTree(Board,Depth-1, Alpha, Beta)
                        Score := Triple(2)
                        set Board.Turn = FlipColor(Board.Turn)

                        if(Board.Turn <> AIColor):
                           # Print("eval player move {Move.Start} to {Move.Target} with score {Score} , best {Best(2)}")
                            if(Score <= Best(2)): # if score < +infinite
                                set Best = (Piece,Move,Score)
                        else if(Board.Turn = AIColor):
                            #AI evaulated board, return highest number
                            #Print("eval ai move {Move.Start} to {Move.Target} with score {Score} , best {Best(2)}")
                            if(Score >= Best(2)): # if score > -infinite
                                set Best = (Piece,Move,Score)

                        #Print("undo move")
                        UndoMove(Board,Piece,Move,MaybeSlainPiece,MaybePromotedPiece)
            if(NumPositions = 0):
                #set PositionsChecked += 1
                Score := EvaluateState(Board,Board.Turn)
                if(Board.Turn= color.White):
                    #Print("++++we at early root with score {Score} for color white")
                else:
                    #Print("+++++we at early root with score {Score} for color black")
                return (chess_piece{},move{},Score)

                        
           # Print("----------- Checked {NumPositions} board positions and total positionschecked = {PositionsChecked}")  
            if(Board.Turn = AIColor):
              # Print("AI: best was move{Best(1).Start} to {Best(1).Target} with ID {Best(0).ID} and score {Best(2)} ------")
            else:
                #Print("Player: best was move to {Best(1).Target} with ID {Best(0).ID} and score {Best(2)} -------")
            return Best
    }

```
