Тёмный

Clean Code using the Strategy Pattern 

git-amend
Подписаться 13 тыс.
Просмотров 12 тыс.
50% 1

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

Игры

Опубликовано:

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 60   
@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 4 месяца назад
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.
@gabrijel9129
@gabrijel9129 8 месяцев назад
I hope to see your channel blow up one day! These are really useful concepts you are teaching. Many thanks!
@git-amend
@git-amend 8 месяцев назад
I appreciate that!
@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!
@txh105
@txh105 3 дня назад
Finally i found a great channel, tks Adam for sharing your knowledge
@git-amend
@git-amend 3 дня назад
Awesome! Welcome aboard!
@yummybunny7351
@yummybunny7351 9 месяцев назад
Thanks for Extension Methods!
@git-amend
@git-amend 9 месяцев назад
You're welcome!
@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!
@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!
@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.
@techdave99
@techdave99 9 месяцев назад
I made a comment earlier but RU-vid 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 RU-vid studio, not sure why it doesn’t show.
@gabrijel9129
@gabrijel9129 8 месяцев назад
Also, may sound random, but your code is just so clean and quite an inspiration
@git-amend
@git-amend 8 месяцев назад
Thank you!
@yashaswiification
@yashaswiification 7 месяцев назад
thanks for this, it makes life easy!! :)
@git-amend
@git-amend 7 месяцев назад
Cheers!
@Aliberation
@Aliberation 9 месяцев назад
Awesome content!
@git-amend
@git-amend 9 месяцев назад
Thank you!
@micha2433
@micha2433 4 месяца назад
If college teachers explained things this well, humanity would be several centuries ahead now.
@git-amend
@git-amend 4 месяца назад
Too kind!
@MrJQ3d
@MrJQ3d Месяц назад
Bom demais, excelente didática!
@git-amend
@git-amend Месяц назад
Thank you!
@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!
@mohammada8328
@mohammada8328 9 месяцев назад
YOU ARE AWESOME
@git-amend
@git-amend 9 месяцев назад
Haha thanks!
@mohokhachai
@mohokhachai 9 месяцев назад
All right sensie
@Vastlee
@Vastlee 9 месяцев назад
Why the extra boilerplate of making your own delegate for the event ButtonPressedEvent vs Action? Old Habits?
@git-amend
@git-amend 9 месяцев назад
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.
@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 :)
@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
@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?
@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.
@ncrx7
@ncrx7 6 месяцев назад
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
@sethbrundle6506
@sethbrundle6506 9 месяцев назад
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 9 месяцев назад
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
@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.
@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.
@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.
@septillion.
@septillion. 9 месяцев назад
Surprised to ever see a Unity video again
Далее
How to use the Decorator Pattern (Card Game Example)
14:55
skibidi toilet 76 (full episode)
08:11
Просмотров 11 млн
8 Design Patterns | Prime Reacts
22:10
Просмотров 394 тыс.
Streamline Your Game - Without Being a Memory EXPERT!
17:05
Better Coding in Unity With Just a Few Lines of Code
15:27
В поисках летнего заноса
1:19
Просмотров 29 тыс.