Search Unity

Heavy code freezing up game while caluclating

Discussion in 'Scripting' started by MonkeyKasai, Jul 24, 2018.

  1. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Hi there,

    I'm just wonder if there was anyway to have code do calculations without causing the game to freeze up. I'm currently working of a turn base strategy game for VR. It's similar to chess in which each unit can move differently, but can only move if the "king" unit is safe or can save the king unit.
    In my game you are allowed to move two units per turn. When selecting a unit to see its legal moves, if it can't find any legal moves, it checks the other units to see if any of the other units would be able to move/attack if the currently selected unit moves. (eg, If you were able to move two pieces in chess per turn. If the opponents queen was putting your king in check and the only way to save the king would be to move your pawn out of the way which would then allow one of your own pieces to now be able to attack the opponents queen, which would then save your king).

    At the moment i'm just using a for loop to call a function in each piece which returns a bool if that piece can move or not, the loop will then break if it finds a piece that can move. Unfortunately this is causing the game to freeze for awhile as it searches for a piece that can move.

    Any have any suggestions about how I can go about this?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could move this set of heavy calculations over to a coroutine to spread it out over a number of frames, or you could try out the new jobs system where it would run in a separate parallel thread.
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Unless I am missing something, that should be a pretty simple process. Do you know why this particular check is taking so long to complete?

    Or do you mean that the overall process (of handling the double move with all the associated 'AI' logic) is taking a long time?
     
  4. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    if you need to make only calculations, you can use classic Threads. Unity stuff can be modified only from main thread, but calculations can run in background threads without affecting the main thread. I'm using this in my VR solution and works well.
     
    Kiwasi likes this.
  5. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Thanks for your replys guys,
    Im guessing it's taking so long because it is checking all the possible moves that can be made. So if a unit can move all the way across the board horizontal and vertical (like a Rook in chess), its checks all those tiles (unless something is blocking the way, then it will stop), it will then check if it is safe to move to that tile, if it's not safe then it will ask all the other alive units if by moving there they would be able to move to allow it to be a safe move. A player has 32 units each, so if one unit can move 8 up, 8 down, 8 right, 8 left. It will check each of those 32 tiles, and then check with the other alive units from each of those tiles, so if nothing is blocking the units way it will call 992 functions on the other units asking if its safe (This is only if those tiles aren't safe). Then the other units check all their tiles.
     
  6. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    Why not to create some StateHandler and assign it for each tile? Something which will hold information about what unit is on that tile and also some reserved state about which unit will move on that tile? (I think it's turn based)
    Then when you will be iterating unit movement on tiles, you will be asking the StateHandler if it's available to move there. And if you have some interactions between units, it's way easy to get unit reference from that StateHandler on target tile and check that interaction.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Seems like an obvious case for threading.

    - Abstract the game state away from Unity.
    - Run all of your complex calculations on the abstract state.
    - Send the results back to Unity when done.

    With this setup you can actually be sneaky and run some calculations while the player is doing other things. Like taking their turn, moving through menus or watching animations.
     
  8. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Thanks for your help guys,
    I decided to use another thread as the players turns switch to calculate the first move of every piece, second move was able to calculate fast enough without any performance issues so just kept that the same. I'm running into a few issues with the units being able to move in places that are blocked or on top of same team units but i'm sure i'll be able to find figure it out :)