#There are all kinds of design patterns,
1 messages · Page 1 of 1 (latest)
Ill make a thread so i dont spam general
Okay so my main issue is im doing a MOBA and I have to make the structure of the characters and the abilities. Right now I have player and enemy that inherit from entity (Entity has name and stats). I also have weapon, armour, trinket, race and class classes which are introduced in the player by composition
Now the problem is right here every ability has variables and some of them might share with other abilities some of the variables
and other abilities share other variables
or even methods
so if i start doing inheritance it will get really big and hard to manage, not even counting that if later on i want to add more variables or methods everything collapses
How many of these are actually shared, that need to be accessed from a class outside of the ability?
Itd make sense if abilities all defined stuff like cooldown. but if you just so happen to have a "radius" in a projectile ability and a "radius" in an explosion ability, then these arent shared. And they definitely shouldnt need to be accessed externally from only having a base class (which is where inheritance would come into play)
To me it just sounds like you've created your own problem of seeing the same variable name in multiple places, when they are not necessarily related variables.
In this case I name them in different ways
like projectileRadius and explosiveRadius
btw, if for example I have stats on weapon, armour, race and class, should I name them all _stats on each class or names should be different? Like _raceStats, _weaponStats... etc
The naming doesnt actually matter. If what I assumed above is correct, that you're referring to variables that seem similar, then this truly isnt an issue.
Call it what you want, though I'd probably just go with stats. As youd be referencing an instance so itd already be something like weapon.stats
You 
If it's a pain to use your own code, then it's bad design.
For what you were asking about shared variables, first think about what problem youd be solving by even grouping them. Let's just imagine you go the inheritance route and complete it all. What issue is now solved?
At what point would you even be referencing an ability by anything other than the base class? The base class which should have no knowledge of things like "radius" or other ability specific things. If you are referencing it by a more specific class (something that defines a radius) then this goes against inheritance already.
wdym by base class? I would never use the father class, just the last childs
which are the skills
with all the variables and methods
every other skill which has a child is just abstract
cause each skill will have its own methods and stuff
A base class would be the one everything inherits from. If you're using the child classes only, you're doing inheritance very wrong and you should definitely look up examples of how it should be used
Unless you're making a very complex AI that can decide what to do based on its abilities, I see no reason why you should need to reference specific fields of an ability.
but wdym by "using"? Im talking about using inside the own class, ofc when I have to declare it in other script i declare the base class
You wrote "use" here.. what do you mean by that then? Using here to me would mean referencing an instance of the class. But now I honestly have no clue what you're even saying in response to my message above about not needing to group variables that arent truly related
they are related, you thought they arent but im telling you all the variables that i want not to get duplicated are the same
Then you should share what you consider to be the same from them. Going through this theory talk will lead nowhere. Even if they are the same, I already addressed this: #1303674716279803915 message
If you arent actually referring to them externally, this is a non issue
okay, and one more thing
If I have a object and I use DontDestroyOnLoad(object), after i change the scene the object shouldnt run Start again right?
No it shouldnt
So why after I change scene all my stats are lost
if the _stats from entity are initialized on start?
It doesnt happen if i do it on awake or right where it is right now
Btw Player is just a child of entity
Your start method is empty, whatever is happening here is completely unrelated to the start method
yeah i know
the problem exists if i do it this way ```CS
public abstract class Entity : MonoBehaviour
{
[SerializeField] protected string _name;
[SerializeField] protected Stats _stats;
protected virtual void Start()
{
_stats= new Stats(0, 0, 0, 0f, 0f, 0, 0, 0f);
}
protected virtual void Update()
{
}
}```
but it shouldnt if start is not called when using dontdestroyonload
Add a debug and see for yourself if start is being called again. Print the instance ID too so you can see if you have multiple instances
I also have to go, so I suggest posting this outside the thread as it's own question
okay thx
I'm a bit late to reply, but here's another approach you can take:
Instead of having a class tree for all your abilities, define them in data. Have one class(a ScriptableObject would be great for this), containing all the possible properties, like range, radius, damage, effects, vfx, etc, and define them in instances of this class. This way you can have a wide range of abilities defined as data, instead of being hardcoded. This also aligns with composition well.
how do i define them on instances so i can just take the attributes i want?
And for methods can I do the same?
You could use enums or something, but maybe not separate the use from don't use. For example, if it's an area of effect ability, it would have are > 0, so when you run the check whether it hits something or not, you know to make an overlap sphere with that radius or something. On the other hand, if it doesn't have an area of effect, then set the property to 0. Then when you get to the area of effect check, you see if the area is 0, and if it is, skip the check entirely.
I'd have methods in separate classes. SOs should be just plain data.
Aside from maybe some utility methods.
Yeah but there ur having a useless variable on that ability
It's not useless. It's there to denote that the ability does not have area of effect.
Yeah but that variable will be on that stat
And never use
Why would you have variables y don't use never
It would be used. To tell that there's no need for a an area check.
And it would be used in more general sense, assuming you'll have abilities that do have an area of effect.
It's a property of the object. It being 0 doesn't mean that it's useless. It describes something about the object.
For example?
imagine its a magic arrow, it has range, cd, dmg as variables that use
but in stats if we get all the stats of all the abilities there might be also areaEffect, effectDuration, percentageAttackSpeedBoost...
an infinite amount
btw is a bad practice making 1 constructor for each case of stats so u can just build whatever u need?
Some of these could be pulled out into their own "components" with composition. For example, an ability could have an "effects" list that would contain the possible effects data, like effect area, duration, etc... same could be done with boosts if needed.
Depends. Not entirely sure how/what you have in mind.
So, as I said, might want to think of your abilities in a more composite way. For example, consider the ability as just a means of delivering certain effects, whether it's damage, healing, buffs, environment modification, or whatever else. The delivering methods are not that numerous: it's either a projectile or a melee/touch ability and it can either have an area it affects or not. Cool down could also be handled here. Then you have the list of effects that it applies. You could even have several lists for different timings or conditions and you could even pull this data into separate data container as well. Then the effects themselves could be more spread out with inheritance if needed.