Posted July 24, 2024 by alexisduprey
Introduction:
If you haven't read any of my other posts and are curious about what I'm doing you can check them out here:
This post will specifically be about finishing up the gameplay loop. This involved setting up pickups, spawner (to spawn enemies and pickups), and finally setting up the Game Mode (where score is kept and GameOver function is).
Disclaimer: I'm fairly new Unreal and have only been learning game dev for about a year now so there may actually be easier solutions to problems I document. Feel free to let me know so that I can continue to learn!
Pickups:
Picks are a pretty simple C++ class. The steps I did to create them:
As I said pickups are pretty straight forward. They have a Capsule component for when the player overlaps them (as well as an overlap function), they have a Flipbook component for their default sprite/animation, and they have an enum called PickupType that is set up in the blueprint for each pickup type. The overlap functions simply checks if the overlapping actor is a player and if so, calls the player's PickupItem functions and sends the type as a parameter.
There is a switch statement in the player's PickupItem function that either increments the player's lives by 1 (if the player picks up a health potion) and plays the cure sounds or plays an animation with a notify states that calls a special attack function (finds all the enemies and calls their Die function) if the player picks up a special gem.
The Game Mode:
Also pretty simple so far. The game mode only keeps track of score and calls the game over function once the player's lives reach 0. The steps to create:
The Game mode has an integer Score variable as well as functions that set and increase the score (SetScore and IncreaseScore). It also has a GameOver function that only sends a console log message telling the player game over. I plan on this controlling when the game over screen is shown, but I need to set up the UI first (Probably my next blog).
It knows when it's game over because grabs a reference to the player in begin player and subscribes to a delegate I created on the player called PlayerGameOverDelegate. The player broadcasts this when its lives reach 0.
Spawner:
Spawner is one of the more complicated classes in the game. Steps to create:
I can't figure out a way to explain exactly what the spawner is doing to get it to work exactly how it did in my Unity game without it sounding like a convoluted mess, so I'll just talk about the basics and a couple of the problems I faced.
Basically, the spawner starts 2 timers in the beginning of the game: EnemySpawnTimer and PickUpSpawnTimer.
Enemy spawn behavior is as follows:
I had to do some really weird stuff with the timer to get it working like this because timers (from what I can tell can't have variable times when they timeout). In the end, I have it not looping through the Set timer function, but in the function that is called when the timer timesout (it is starting to sound convoluted I know!). Basically, each time the timer times out I call the SpawnWave function which restarts the timer manually with a new time out time that is between 0 - 1 seconds until it reaches the right wave number.
Pick up spawn behavior is as follows:
Nothing special about implementing this since I used the default looping in the set timer function to handle the spawn every 8 seconds.
Both pickups and enemies are spawned at a random vector in the map area (a function I created) and both timers are stopped when the player's game over delegate is triggered.
Conclusion:
That's it! The gameplay loop is all wrapped up. The game really feels like a game now! Everything is working even though the only indication to the player is through console logs. I'm going to fix that next go around with setting up the UI and final polish, but I'm unsure if I'll do a devlog about it.