C
C#2y ago
Costin

✅ 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:
public class Class3 : Class2 {
public ... void DoThing() { // any way????
//I would also want to forcely call Class2's DoThing without the programmer removing the base.DoThing();
//code
}
}
public class Class3 : Class2 {
public ... void DoThing() { // any way????
//I would also want to forcely call Class2's DoThing without the programmer removing the base.DoThing();
//code
}
}
public class Class2 : Class1 {
public override void DoThing() {
//code
}
}
public class Class2 : Class1 {
public override void DoThing() {
//code
}
}
public abstract class Class1 {
public abstract void DoThing();
}
public abstract class Class1 {
public abstract void DoThing();
}
Bonus, I would also want to forcely call Class2's DoThing without the programmer removing the base.DoThing();
34 Replies
cathei
cathei2y ago
It's same thing. public override void DoThing() for Class3 But you still need to call base.DoThing() ofc, with that way
Costin
Costin2y ago
but won't override modifier override the base.DoThing() or I'm just taking the name too literally? (and like do a loop)
cathei
cathei2y ago
That is why base keyword is there for If you just call DoThing() then it's recursive
Costin
Costin2y ago
anyway, either way, I want to make the base.DoThing() be forced so that the base code runs
cathei
cathei2y ago
Can't do that unless you make another method and seal original one
Costin
Costin2y ago
seal hehh
cathei
cathei2y ago
public class Class2 : Class1
{
// You override this
protected virtual void DoThing2() {}

public sealed override void DoThing()
{
DoThing2();
//code
}
}
public class Class2 : Class1
{
// You override this
protected virtual void DoThing2() {}

public sealed override void DoThing()
{
DoThing2();
//code
}
}
Costin
Costin2y ago
interesting so it's a forceful version of base.DoThing() ?
canton7
canton72y ago
(DoThing2 should be protected)
cathei
cathei2y ago
Yes because overriding DoThing2 will not effect how DoThing work
canton7
canton72y ago
But yeah, this is the right approach. Give the subclass a method to override which is empty, and then call it from your actual override
Costin
Costin2y ago
would it be a good thing to do that?
cathei
cathei2y ago
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
Costin
Costin2y ago
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?
cathei
cathei2y ago
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()
Costin
Costin2y ago
wdym
cathei
cathei2y ago
I'm trying to understand why you are worried about calling base.DoThing() Though I do wish there was a built-in warning for it
Costin
Costin2y ago
no, let's say this is Cat code:
public class Cat : BaseControllableEntity
{
float pawAttackTimer;

public void doPawAttack()
{
//animation, damage, etc
//there will also be a delay
}
public void damage()
{
//someone is trying to kill the cat
death();
}
public void death()
{

}

public override void OnStart()
{
//initialize stuff if needed
}

public override void OnUpdate()
{
//some optional cat code such as blinking idk
if (isPlayer) {
//input check
//perform actions such as paw attack or move

if(Input.blabla)
{
//move code (better to move to a function controlled with enums/indexes)
}
if(DoAPawAttack && pawAttackTimer <= 0)
{
doPawAttack();
}
} else
{
//ai thinking
//perform actions such as paw attack or move

if(DoAPawAttack && pawAttackTimer <= 0)
{
doPawAttack();
}
if(wantToMove)
{
//move code (better to move to a function controlled with enums/indexes)
}
}
}
}
public class Cat : BaseControllableEntity
{
float pawAttackTimer;

public void doPawAttack()
{
//animation, damage, etc
//there will also be a delay
}
public void damage()
{
//someone is trying to kill the cat
death();
}
public void death()
{

}

public override void OnStart()
{
//initialize stuff if needed
}

public override void OnUpdate()
{
//some optional cat code such as blinking idk
if (isPlayer) {
//input check
//perform actions such as paw attack or move

if(Input.blabla)
{
//move code (better to move to a function controlled with enums/indexes)
}
if(DoAPawAttack && pawAttackTimer <= 0)
{
doPawAttack();
}
} else
{
//ai thinking
//perform actions such as paw attack or move

if(DoAPawAttack && pawAttackTimer <= 0)
{
doPawAttack();
}
if(wantToMove)
{
//move code (better to move to a function controlled with enums/indexes)
}
}
}
}
@cathei this can be the Cat code also I improved the code a bit
canton7
canton72y ago
Mixing the ai and user control into the same method smells
Costin
Costin2y ago
wdym
canton7
canton72y ago
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
Costin
Costin2y ago
why would you prefer 2 different scripts for controlling?
canton7
canton72y ago
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
Costin
Costin2y ago
?
canton7
canton72y ago
!
Costin
Costin2y ago
why hard to maintain fast
canton7
canton72y ago
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
Costin
Costin2y ago
wdym
canton7
canton72y ago
What I said 🙂
Costin
Costin2y ago
idk, I would like opinions from more people and besides, it's not hard to make 2 methods and separate the controls there
Accord
Accord2y ago
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.
Costin
Costin2y ago
Not yet, I want opinions on above aaa this is still active \
Accord
Accord2y ago
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.
Costin
Costin2y ago
what idk I will re-open a new one with a better issue or smh
Want results from more Discord servers?
Add your server
More Posts