✅ 3 objects inheritance thing + force call base.DoThing();
So I want to make Class3 inheriting Class2 inheriting abstract Class1, to have Class1's function.
Very confusing, but here's what I'm trying to do in code:
Bonus, I would also want to forcely call Class2's DoThing without the programmer removing the base.DoThing();
34 Replies
It's same thing.
public override void DoThing()
for Class3
But you still need to call base.DoThing()
ofc, with that waybut won't override modifier override the base.DoThing() or I'm just taking the name too literally?
(and like do a loop)
That is why
base
keyword is there for
If you just call DoThing()
then it's recursiveanyway, either way, I want to make the base.DoThing() be forced so that the base code runs
Can't do that unless you make another method and seal original one
seal
hehh
interesting
so it's a forceful version of base.DoThing() ?
(
DoThing2
should be protected)Yes because overriding
DoThing2
will not effect how DoThing
workBut yeah, this is the right approach. Give the subclass a method to override which is empty, and then call it from your actual override
would it be a good thing to do that?
Depends on what your goal is?
If you are making some kind of library and want to give some foolproof approach maybe
But it will take away user's control about when to call
base.DoThing
my goal is to make a first person game where the player can choose any 'role'. roles can be for example a Cat, a Citizen, a Policeman, etc.
however, these "roles" may also have code for AI, so that the player can become any character that the AI can also be.
.
my plans to do that is to make a BaseControllableEntity class that would have an isPlayer boolean.
then, there will be several more bases such as BaseHuman, since there will be many roles that would be human.
the roles would then inherit BaseHuman and have code for both an AI, and player controls.
this means, for example, that the player could become an evil "Cat" and use his claws to attack, and the AI can also be a Cat and share the same claws attack code.
@canton7 @cathei ^^
do you understand my plans?
You might want to split the 'controller' and 'role' so you don't have to make every role with two version
But without going too deep about architecture I don't see anything blocking you from calling
base.DoThing()
wdym
I'm trying to understand why you are worried about calling
base.DoThing()
Though I do wish there was a built-in warning for itno, let's say this is Cat code:
@cathei this can be the Cat code
also I improved the code a bit
Mixing the ai and user control into the same method smells
wdym
I'd split that into into separate classes. UserCatController looks at user inputs then calls Cat methods to DoPawAttack etc. AiCatController does the same but controlled by the AI
why would you prefer 2 different scripts for controlling?
Sticking both user and AI control, which see separate things, into the same method separated by an if statement is ugly, and it's going to get hard to maintain fast
You just develop a feel for these things with time
?
!
why hard to maintain fast
You'll end up with massive methods, where half of the method isn't related to the other half
I think the best thing is to keep doing it the way you suggested, and you'll find the problems once you've spent enough time developing it. Then you'll try something else next time
wdym
What I said 🙂
idk, I would like opinions from more people
and besides, it's not hard to make 2 methods and separate the controls there
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Not yet, I want opinions on above
aaa
this is still active
\
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.what
idk
I will re-open a new one with a better issue
or smh