Clean Code using the Strategy Pattern

  Рет қаралды 11,785

git-amend

git-amend

Күн бұрын

Unity C# Architecture: Strategy Programming Pattern in Unity C# using expert Software Engineering Principles. Modularize behaviour and enforce the Single Responsiblity Principle!
🔔 Subscribe for more Unity Tutorials / @git-amend
#unity3d #gamedev #indiedev
▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬
0:00 The Strategy Pattern
1:20 Shield Spell Strategy
3:38 Projectile Strategy
6:30 Orbital Spell Strategy
Extension Methods and Utils
github.com/adammyhre/3D-Platf...
Assets Shown In This Video (Affiliate Links)
Odin: assetstore.unity.com/publishe...
Dungeon Mason Tiny Hero Duo: (FREE): assetstore.unity.com/packages...
Chromisu: Handpainted Forest MEGA Pack assetstore.unity.com/packages...
SineVFX: Better Crystals assetstore.unity.com/packages...
Starry MoMo Shield Bubbles assetstore.unity.com/packages...
Kyeoms Shoot and Hit assetstore.unity.com/packages...
DOTween (FREE) assetstore.unity.com/packages...
Follow me!
linktr.ee/gitamend

Пікірлер: 58
@git-amend
@git-amend 9 ай бұрын
24-hour Challenge! Identify a method in your existing project where you're using hardcoded values to dictate behaviour. Replace those hardcoded elements with strategies, allowing dynamic behaviour changes without modifying the existing code. Don't forget to click the LIKE button!
@BornToTroll-it5ju
@BornToTroll-it5ju 3 ай бұрын
I've had software engineers working on my projects before but tbh never of your calibre. I bet you get headhunted a lot!! 😅
@techdave99
@techdave99 9 ай бұрын
Another awesome video. I love these pattern videos.
@yummybunny7351
@yummybunny7351 9 ай бұрын
Thanks for Extension Methods!
@git-amend
@git-amend 9 ай бұрын
You're welcome!
@arjanb7158
@arjanb7158 9 ай бұрын
Thank you so much, man! Having a look right now
@arjanb7158
@arjanb7158 9 ай бұрын
This makes it really clear! Again thanks so much! How do you decide between a builder constructing your projectile vs putting the info on the projectile prefab? Is it for variables that you want dictated by the spell instead of the projectile? Or do you usually treat prefabs as a visuals-only thing?
@git-amend
@git-amend 9 ай бұрын
@@arjanb7158 This is another excellent question. Primarily, I would use a prefab to define a visual style, set the colliders appropriately, and generally speaking, I would have put the 2 components I configured with the Builder onto the prefab beforehand. That's why I like using the GetOrAdd Extension method - if that component is already on the prefab, I just Get it so I can configure it as dictated by the Builder methods - however, if it isn't there (because I forgot or whatever) then it will get Added to the instance of the prefab, and no matter what I am 100% certain that the projectile has the components I expect before I start configuring them with speed/duration etc. I think I will go into more detail on that in a future video about building a robust ability system.
@gabrijel9129
@gabrijel9129 7 ай бұрын
I hope to see your channel blow up one day! These are really useful concepts you are teaching. Many thanks!
@git-amend
@git-amend 7 ай бұрын
I appreciate that!
@hetsmiecht1029
@hetsmiecht1029 9 ай бұрын
I literally have an exam tomorrow about software design patterns, this video could not have been timed better. You are the best!
@git-amend
@git-amend 9 ай бұрын
Best of luck! Perfect timing!
@derekdipietro9805
@derekdipietro9805 9 ай бұрын
Your videos are incredible, keep it up I think you'll gain some popularity on here. It certainly isn't easy but I've been working in unity for a few years now and have learned a good bit the last few weeks.
@git-amend
@git-amend 9 ай бұрын
Thank you so much!
@yashaswiification
@yashaswiification 7 ай бұрын
thanks for this, it makes life easy!! :)
@git-amend
@git-amend 7 ай бұрын
Cheers!
@ItsDan123
@ItsDan123 6 ай бұрын
Genuinely great content with concrete examples and practical advice for integrating it with Unity. So often tutorials either show coding things poorly but it seems fine in Unity (e.g. assigning a weapon directly), coding things great but no thought in how it will work with Unity (okay I have my sword but how do I get that into my characters hand!?). You do a good job of showing where things can be pure code and when you can use something like ScriptableObjects to create that bridge.
@git-amend
@git-amend 6 ай бұрын
Thank you so much! Very motivating to read your comment! Cheers!
@micha2433
@micha2433 3 ай бұрын
If college teachers explained things this well, humanity would be several centuries ahead now.
@git-amend
@git-amend 3 ай бұрын
Too kind!
@techdave99
@techdave99 9 ай бұрын
I made a comment earlier but KZfaq seems to have removed it. It was a short comment saying I like the video. Thanks for showing how to apply patterns in game development. It's hard to find good videos about this. There are a lot about patterns, just not with specific game dev examples.
@git-amend
@git-amend 9 ай бұрын
No worries! Glad you like these, will make more as time permits. I see your other comment in KZfaq studio, not sure why it doesn’t show.
@Aliberation
@Aliberation 9 ай бұрын
Awesome content!
@git-amend
@git-amend 9 ай бұрын
Thank you!
@gabrijel9129
@gabrijel9129 7 ай бұрын
Also, may sound random, but your code is just so clean and quite an inspiration
@git-amend
@git-amend 7 ай бұрын
Thank you!
@MrJQ3d
@MrJQ3d 23 күн бұрын
Bom demais, excelente didática!
@git-amend
@git-amend 23 күн бұрын
Thank you!
@mohammada8328
@mohammada8328 9 ай бұрын
YOU ARE AWESOME
@git-amend
@git-amend 9 ай бұрын
Haha thanks!
@mohokhachai
@mohokhachai 9 ай бұрын
All right sensie
@DisfavoredKin
@DisfavoredKin 9 ай бұрын
good video got to love the more advenced stuff for gamedev. what do u plan on doing after u cover all design patterns u want?
@git-amend
@git-amend 9 ай бұрын
Thank you! I have a long list of topics to cover, which range from Advanced C# language features to implementing different game systems. If you have any specific topics, feel free to make suggestions!
@cursedfunction
@cursedfunction 5 ай бұрын
@git-amend at 1:30, is there a specific reason that you are locally caching the indexer and then using it in the callback while using the for-loop indexer whan accessing the array?
@Vastlee
@Vastlee 8 ай бұрын
Why the extra boilerplate of making your own delegate for the event ButtonPressedEvent vs Action? Old Habits?
@git-amend
@git-amend 8 ай бұрын
I was toying with the idea of doing something more robust for the buttons, but then decided not to so as not to detract from the topic of the video. You are right of course, that could have been done in one line in the end.
@Piaasek
@Piaasek 6 ай бұрын
Great content my boy, do you have git projects for those available? ;)
@git-amend
@git-amend 6 ай бұрын
Not for most of the Patterns videos. But, many other videos have accompanying source code which can be found here: github.com/adammyhre
@MaxVinopalDev
@MaxVinopalDev 8 ай бұрын
Hey, good videos, going through all of them and you are describing it really well, so thanks! In case that you'd want for example an animation to play when a spell is cast, how would you handle it? Would you prefer having animation controller passed as parameter in CastSpell along with origin transform? Asking this because I am kinda using this pattern but this is something I tend to run into - I start needing access to stats, animation controller, etc and then it starts feeling like it would be easier if the hero knew about the spell so all this stuff is easily accessible, but it makes it tighter coupled and can't be reused for enemies as well for example.
@git-amend
@git-amend 8 ай бұрын
That's a great question, and actually a good topic for a future video. Here's some initial thoughts: 1) You might consider a Context Object - you could encapsulate all necessary data into a SpellContext object and pass this to the CastSpell method: public class SpellContext { public AnimationController AnimationController { get; } public Transform Origin { get; } public Stats Stats { get; } //... other necessary data } public interface SpellStrategy { void CastSpell(SpellContext context); } 2) If animation behavior varies greatly across spells, employing the Decorator Pattern could allow for stacking behavior such as animations on top of the basic spell casting functionality. This way your Player and Enemies can use their own decorators, but the same spell (here I've added the ISpell interface to the SpellStrategy): public interface ISpell { void CastSpell(); } public abstract class SpellDecorator : ISpell { protected ISpell spell; public SpellDecorator(ISpell spell) { this.spell = spell; } public abstract void Cast(); } public class AnimatedSpell : SpellDecorator { AnimationController animationController; public AnimatedSpell(ISpell spell, AnimationController animationController) : base(spell){ this.animationController = animationController; } public override void Cast() { this.animationController.PlayAnimation(); spell.Cast(); } } // Example usage: ISpell animatedSpell = new AnimatedSpell(spell, animationController); animatedSpell.Cast(); 3) You could consider using the Command pattern as well, which you could use to wrap up the above Decorator so that you could easily pass it around like an object, for example into your 'Attack' state of your state machine. I hope that helps!
@MaxVinopalDev
@MaxVinopalDev 8 ай бұрын
Thanks for such elaborate reply! I watched the decorator video and thats definitely something I want to experiment with. Feels powerful but not super easy to imagine at the start. That being said, a video showing a combination of patterns in a more elaborate use case such as this would definitely be someting I would watch :)
@ncrx7
@ncrx7 5 ай бұрын
It's a nice method, but it works incorrectly if the button order and the spell order in the scriptable object list are not synchronized and we can not use monobehaviour features like coroutines etc. . in scriptableobject
@shunpeng5700
@shunpeng5700 9 ай бұрын
I wonder if there is any design pattern that work really well with the unity netcode for gameobject that you may share, specially for client/server RCP?
@git-amend
@git-amend 9 ай бұрын
That's a good question. The first thing that comes to mind is the Command pattern. This would be useful if you had a lot of network requests (especially a variety of Server/Client RPCs) going back and forth and needed to manage them in a more organized manner or add additional functionality such as logging. Thanks for the suggestion; maybe I can use that as an example in a future video.
@GeniusPancake
@GeniusPancake 7 ай бұрын
You can use strategy pattern with NetCode. You can create abilities with almost the same setup as this one (with some adjustments). - Have a static array of "Ability" scriptable objects, in some Database, that hold the data for the ability (name, sprite, prefab, description, abilityInfo (it holds all values like damage, health, speed, etc. written in string to look like JSON)) - Have AbilityState that is abstract method that represents the ability state and is networked object - Extend AbilityState for each ability and it would access the Ability data by using the static array and deserialzing the AbilityInfo string as json - Design the, for example "ShieldAbilityState" to work what you want - Put the ShieldAbility_Prefab into the ShieldAbility scriptable object as prefab parameter - When player is using the Ability, it would go into the "ShieldAbility" scriptable object and "Instantiate" the ShieldAbility_Prefab (which is networked object now) - Voila, you have networked ability and ShieldAbilityState is responsible for it's creation and destruction without the player knowing anything about the ability. This is just a short explanation but it can be expanded upon quite a lot and you can do same as he did here.
@JacobNax
@JacobNax 7 ай бұрын
I should advise everyone watching here that you should recycle all your objects instead of destroying or instantiating them as they have real overhead on larger scales. To have a production ready spell system you also need to think about the scale of such systems and overhead as they can negatively impact performance on mobile phones. It's also important to understand that every solution has it's own applications and no 1 solution fits all.
@ekekw930
@ekekw930 8 ай бұрын
could you make an RPG series using all the best practices?
@git-amend
@git-amend 8 ай бұрын
That's a good idea, I'll keep that in mind.
@sethbrundle6506
@sethbrundle6506 8 ай бұрын
Ur mentioning copilot a lot throughout the video. I must have missed something or should be aware of another video, but what kind of copilot are you using? Is it an addon from the unity store or...? Seems like something I would be into
@git-amend
@git-amend 8 ай бұрын
I use GitHub Copilot, primarily for context aware code completion, but it can also provide AI generated suggestions. It's a huge timesaver. github.com/features/copilot
@septillion.
@septillion. 9 ай бұрын
Surprised to ever see a Unity video again
@bbrainstormer2036
@bbrainstormer2036 9 ай бұрын
Really helpful series of videos! Just following various tutorials isn't really going to teach people these patterns. Why the anime girl thumbnail though?
@git-amend
@git-amend 9 ай бұрын
Thank you! Regarding the thumbnail, I wanted to come up with an image that represented an action (Building something, Producing something in a Factory, Strategizing, Creating something Abstract, etc), and it seems to resonate with viewers. Or, maybe they just like Anime girls. Either way, it seems to be working.
How to use the Decorator Pattern (Card Game Example)
14:55
git-amend
Рет қаралды 7 М.
Visitor: How I Mastered the Toughest Programming Pattern
12:59
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 86 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 482 М.
Каха ограбил банк
01:00
К-Media
Рет қаралды 11 МЛН
The Strategy Pattern: Write BETTER PYTHON CODE Part 3
15:31
ArjanCodes
Рет қаралды 110 М.
EASY Stats and Modifiers in Unity | Broker Chain Pattern
16:05
Improve Your Unity Code with MVC/MVP Architectural Patterns
15:32
The Unity HACK that the PROS know
21:27
git-amend
Рет қаралды 10 М.
D in SOLID - I wish I learned the LAST letter FIRST
18:00
git-amend
Рет қаралды 7 М.
Learn to Build an Advanced Event Bus | Unity Architecture
13:27
Strategy Pattern, The Best Software Design Pattern
2:23
Jono Williams
Рет қаралды 55 М.
How to Program in Unity: Command Pattern Explained
22:37
iHeartGameDev
Рет қаралды 65 М.