Concurrency (or reference) issues

Hey there,

I have another issue that I need some help with.

Idea and setting (roughly)

  • The player pawn needs to dodge obstacles
  • There are 10 different kinds of obstacles, each offering a specific way to get past it
  • Each x seconds, a random obstacle is chosen to put in the player’s way

Implementation

Once an Obstacle is placed in the player’s way, it should not be moved before the player has passed it (Had that, was terrible :/). So, I need the randomizer to ignore those that are already “in use”. This has lead to the following design:

  • Besides other properties, Obstacles have a Availableproperty that is set to true by default and set to false, once the obstacle is placed in front of the player pawn. Only by passing the obstacle, this is reset to true (collider at the back side triggers)
  • For performance reasons, the obstacles are pooled, so that I don’t need to instanciate them at runtime
    • In the Game Controller BP, I hold a map that contains each of the 10 possible obstacles, indexed by an ENUM key that describes how to pass them
    • A Timer started in the BeginPlayevent of the controller accesses the pool to…
      • find the next obstacle
      • mark it as unavailable
      • place it before the player

Controller:

(Yes, the timer is not set to looping on purpose, as the Spawn Interval changes over time). The PlaceObstaclefunction - or more precisely : GetAvailableObstaclefunction - does the random stuff:

Obstacle: Resetting Availability. I call a function in the controller to work on the array directly, since I wanted to make sure I’m not editing a copy (Which is what Array.get gives me) of the Obstacle

Controller: Map item modification

Problem

I don’t know whether it’s concurrency of Timer and event-driven access to the availabitlity flag, but pretty soon, the obstacles become unavailable, but not available anymore. In short: I run out of Obstacles.

I can’t help the feeling that my way of looping over the map and creating an array from it is causing the issue…

Any thoughts on this?