r/howdidtheycodeit • u/Flohhhhhh • Jul 03 '22
Question How do they code rogue like upgrades??
I’m looking at making a game with a roguelike progression style. The main thing that is confusing me is how having such a wide variety of effects would work.
For example, stat bonuses would be easy. But say I’m making effects that add new mechanics to projectiles, new mechanics to movement, or more complex things. How would I handle coding that?
I assume I would have a database of all the upgrades and their effects, but on the actual classes do I just need 1000 boolean variables for if it has that effect or not and check all of them one by one in the events? How could I approach that? By
7
u/CarniverousSock Jul 03 '22
IMO the best method is to do this object-oriented. You instantiate “behavior” objects throughout the run, which have member methods that are executed when the scene is updated.
Example: a power up that makes projectiles move in an erratic way. You could have a list of abstract “bullet movement” objects that are called on the bullets every frame. Each executes its logic on the bullet motion, so the behaviors stack. The update loop doesn’t need to know what the behaviors do: they just call the virtual functions on the objects in the list.
4
u/Flohhhhhh Jul 03 '22
Example, I want character to do more damage to enemies that are frozen.
That’s great, just check if the player has the upgrade to do more damage with a Boolean and take care of business.
But what if instead of frozen, there are 100 more variations of this for different states?
I can’t just check all 100 booleans whenever the player attacks. That’s where I’m stuck.
27
u/FauxGuyFawkesy Jul 03 '22
It's interesting right. Checking a bunch of flags seems unintuitive but really, a single bool check is pretty fast. Why not change how we think about it? If the player has an up to date list of upgrades, all we need to do is check the list, not every possible upgrade.
22
u/cael14 Jul 03 '22
I can only speak from personal experience but my current project uses delegates to register and unregister to game events so instead of checking 1000 booleans only the registered events are fired. I can explain further if interested.
21
u/ISvengali Jul 03 '22 edited Jul 03 '22
This is one of the harder questions to answer in games. Every place Ive worked has solved it in a different way.
Ive seen event processing systems, OOPish ones, componentized ones. straight code (your bool example), etc
They all sort of suck; they all have issues. There is no magic bullet which makes it at all easy.
Dont go crazy with designs. Just pick something and use it. A nice componentized version with layered updates (as some folks have suggested in here), works great.
Dont be scared about just doing the calculation every frame. Its relatively cheap compared to a lot of things.
Keep an eye out for any minor refactoring. If something seems annoying in your current design, refactor it a bit to make it easier. Every project is going to have a different sweet spot for their designs.
Being able to describe why a value is what it is can be very nice for debugging.
For example, lets say you have strength upgrades. With one design, you would have a Strength value, that you update with your upgrade, so you change a Strength value from 10 to 13, then to 15. However, the reason it has been changed is lost. Sure, you can log it, and thats not bad, but theres another way.
But in another design, you have a BaseStrength, 1 or more UpgradeStrength-s,and a calculated final Strength. So, if you have a BaseStrength of 10, an UpgradeStrength of +2 for a helmet, and +3 for a temporary spell. for a final strength of +15, you can then later do debug Strength which would spit out:
Furthermore, the BaseStrength could actually be +7 for RaceStrength and +3 BerserkerStrength
2
u/Eonzenex Jul 03 '22
There's a system Unreal engine you could look into: Gameplay ability system
Basically, an effect applies a tag. Tags have conditions such as other tags that are required (Enemies get the "fire" tag if they have the "covered in oil" tag), or tags that block it (Enemies can't get the "fire" tag if they have the "wet" tag), etc.
When you activate something that can be modified, you check for tags that could change its behaviour.
This is a big system to setup so it'd only be useful if you had a lot of upgrades, or upgrades that change on other tags.
1
u/oddbawlstudios Jul 03 '22
Why not have a base class, that holds basic stats. And then have different classes that inherit the base class and overrides things like stat modifiers, or have a generic type that the, for example frozen ability, has and override that so it does the frozen effects?
1
u/noogai03 Jul 03 '22
You probably want something like the strategy pattern. An upgrade class that hooks into a ton of game logic - e.g. every time you call "takedamage()" it loops through all the upgrades and applies their "damageModifier()" function. So armour or a potion might subtract from the incoming damage. But you could also have it hook into the move() method, etc.
I would probably do this one kd two ways: either have the modifier methods return some kind of Optional and just do nothing if they return empty/null, or have all modifier methods extend a base implementation that just returns the value unmodified.
This method avoids all the nasty coupling and boolean flags.
1
u/frenchtoastfella Jul 03 '22
As mentioned in other examples you can use event system and subscribe to event like on attack, on special, on hit, etc. Then only the subscribed stuff will fire.
Me on the other hand, while working on my game blade bouncer 2: revolution used a shit ton of if checks based on a dictionary called "special powers". It's not the same system but I do have a wide variety of mythic powers and talents which change hiw stuff behaves. This wasn't the cleanest solution but it got the job done with little to no performance impact so I'm satisfied with it.
1
u/TotPott Jul 03 '22
I tend to lean into event systems nowadays as a step above just an abstracted list. So the character controller will contain multiple event dispatchers for stuff like movement, firing, recieving damage etc. and when these events fire they pass about some mutable params. So in this case, the upgrades just register themselves as event listeners and perform and bespoke modifications the need to.
Here's a few examples:
An upgrade which halfs incoming damage:
-Upgrade registers as a listener for the Damage event -Player takes damage so makes some event params with the damage amount attached then fires the event -The upgrade the modifies the params and halfs the amount -Player damage flow can continue and use the params value in any following logic
An upgrade which gives you double speed for a second after taking damage:
-Upgrade registers as a listener for the Damage and Movement event -Player takes damage so fires the event -The upgrade hears the event so takes note of the current clock time -Game continues on... -When Player moves they fire an event with params containing the movement stats -Upgrade hears this event -Compares the last damage time to see if it's been under 1sec -If it has, double the speed value in the params -Player movement flow can continue and use the params value in any following logic
The beauty of this style of design imo is that you keep the code very clean and segmented. The player controller doesn't know anything about the upgrades system so can be kept simple and each upgrade can have very bespoke behaviour that's all contained within its own files. Later on I you need a new thing upgrades can effecy, you can just add a new event and don't need to retrofit a bunch of hacky code.
Of course, this is only one of the many approaches 😉
1
u/Ezeon0 Jul 03 '22 edited Jul 03 '22
In my game I use a struct to define each upgrade and each upgrade contains a list of effects.
The effects are implemented in a couple of different ways. For stats, I have a component that just add them together. For ability type effects, I use a separate class for each category of abilities.
For example, you could have a projectilemodifier class that will modify the number of projectiles, their speed, the spread, etc.