C
C#2y ago
Pacrombie

I have a super weird thing I'm trying to do with interfaces

First of all, I apologize for the incredibly vague nature of my question but I'm become obsessed with finding the "best" solution to every problem I encounter. I have IPlayerAbility with an Activate() method and I essentially want Activate() to execute as if it is on the PlayerController object. I had it so that all of the actual body code that made up the logic of these abilities' Activate() methods were actually just in methods on the PlayerController class. The Activate() methods on these abilities simply called the appropriate method on the PlayerController to do what it needed to do. I then refactored it (because I really hated putting everyting in PlayerController) to have it so that every time PlayerController needed to activate an ability, it would pass itself into the method as such: void Activate(PlayerController player) and then called in PlayerController as: ability.Activate(this). But then when I put the logic into the activate method, I would CONSTATNTLY have to put something like player.transform.position ("player." before practically everything). Is there a better way to do what I'm trying to do? Something like having the logic for a function exist in the ability but have it called as if it is in the player controller? I doubt that would be the case, I just hope I'm getting my goal across. Any advice is appreciated! :)
5 Replies
Pacrombie
PacrombieOP2y ago
Maybe I should move the ability fields to their respective ability classes too to reduce the ".player" uses. But then I don't get to edit those values in the Unity inspector :( TL;DR if i want to have my ability logic in my ability classes (which i do want), is there a way to avoid the constant "player." in order to access playercontroller properties ?
Aaron
Aaron2y ago
not really, no other than maybe writing properities that do it for you like
class SomeAbiliity : IPlayerAbility
{
public PlayerController player;
public Transform transform => player.transform;
public GameObject gameObject => player.gameObject;
}
class SomeAbiliity : IPlayerAbility
{
public PlayerController player;
public Transform transform => player.transform;
public GameObject gameObject => player.gameObject;
}
maybe not public, but you get the idea
Pacrombie
PacrombieOP2y ago
so if i pass in "this" from my playercontroller into each ability (either into the method or a constructor) and use that parameter a shit ton, is that "bad code"?
Aaron
Aaron2y ago
not sure why it would be sounds fine
Pacrombie
PacrombieOP2y ago
ok thank you :)
Want results from more Discord servers?
Add your server