How I handle a big diversity gameplay elements
Having a lot of different gameplay elements is one of the characteristics of RPG-oriented games, and handling them as a developer can be a real challenge. Here is how I implement different enemies to facilitate future debugging and gain time.
The key is to ask yourself how much can you re-use your assets and try to build an environment where different pieces of code can be interchangeable. It turns out Defold is simply amazing for that! All of this thanks to two features :
- Script Properties
For example, here are two enemies. Those are .go files that will be used as prototypes for factories (don’t forget to use dynamic loading to save ressources!). In my project, all enemies will all have to :
- Take damages
- Be impaired by player actions (stuns, slows, push etc…)
However there are a ton of things that my enemies will do differently from one another :
- Shooting different projectiles
- Dealing different damages
- Resisting different elements
- Moving in different patterns
- Attacking more or less often
Therefore two options come to you in this situation :
- The first option would be to use go.property() to make your unique script very customizable. The drawbacks are mostly organizational. The more your enemies have to be customized the more complicated and long your script becomes.
- Your second option is to create a script for each different elements. The problem with creating a lot of different scripts is that adding new features to your game can become extremely slow and painful if you have to update every single script of every single custom entity.
You guessed it, it is all about balance : you need to find out what can be re-used with a few customizable parameters, and what has to be separated across multiple scripts.
So here is how I made my enemies. Their core aspects will be handled by a script that will be the same for every enemy. This is what I named “EBU - enemy base unit”. It has script properties such as their maximum hitpoints, their speed, their resistances.
Now, the coolest part is that thanks to the way Defold handles messaging and addressing, it is very easy “plug scripts into one another” (if that makes sense). First you need to use a custom ID for your components. As you can see of those two screenshots, the AI and ATK scripts of the sheep and crab are two different scripts but thanks to custom ID they will be addressed the same way. I could swap those two scripts and they would still function in harmony with the other scripts!
So, in order to have my enemies be stunned by the player, I coded the basic move cycle in the enemy_base_unit, and had the #AI sends directions via messaging. Now if I have any bug to fix or new interactions to add to the enemies I simply don’t have to modify every AI script I made, only edit the enemy_base_unit script.
Like this, I can combine custom movement patterns and custom attack patterns and have it all function quiet easily.
Thanks for reading, hope it inspired you to make more complex projects with Defold!