|
Post by BatCrazy on Jul 14, 2019 7:04:43 GMT
Hello I was wondering if you know where i could get some help with setting up abilities
if (card.GetAspect<Abilities>() != null || card.GetAspect<TriggeredAbilities>() != null)
{
var abilities = card.GetAspect<Abilities>().abilities;
if (card.GetAspect<TriggeredAbilities>() != null)
abilities = card.GetAspect<TriggeredAbilities>().triggeredAbilities;
foreach ( var ability in abilities)
{
I am trying to do something like say if the card has abilities OR triggieredabilities assign it to abilities then run it through a foreach statement
I know the above wont work but have any clue how this is normally done without having to write a whole other foreach statement for the other....there will never be a point where a card will have triggeredabilities AND abilities...
I was thinking maybe i could through the portion into a seperate method but im not sure if there is a way to pass it to the method any hints would be awesome! thank you!
|
|
|
Post by Admin on Jul 18, 2019 13:54:28 GMT
I dont know what your "Abilities" and "TriggeredAbilities" components look like, so I cant say for certain the best way to achieve what you are going for. Do they share a common base class? Conform to a shared interface? If I can assume that they do, then I could do something like this:
var abilities = card.GetAspect<Abilities>().abilities ?? card.GetAspect<TriggeredAbilities>().triggeredAbilities; if (abilities != null) { foreach (var ability in abilities) { // Your code here } }
|
|
|
Post by BatCrazy on Jul 23, 2019 17:34:10 GMT
Thanks for the help on this and the command
- the triggeredabilities and abilities class dont share the same type - but they do share the IAspect class which has the IContainer type which not sure how i would still take that and make it an abilities or triggeredabilities type because i think with C# the type will need to be known for the foreach statement... but i dont know this stuff inside and out just from what i have read so far - might be old info though so not sure if anything has been updated since
|
|
|
Post by Admin on Jul 24, 2019 13:22:37 GMT
Let me verify that I understand correctly what you are trying to achieve. Essentially right now you have something like (pseudo code): if I have abilities then { for each ability in abilities { do some setup } } if I have triggered abilities then { for each ability in triggered abilities { do some setup } }
Currently you need a separate loop for each type of ability because they dont share a common "ability" interface. It sounds like you want to do something like this... if I have abilities or triggered abilities then { for each ability in either collection { do some common setup } }
If I am correct, then the solution is to make a new base class that both Ability and TriggeredAbility inherit from, OR to make an interface that they both conform to. Then you grab a reference to either collection as if it is a collection of the base class or interface and you loop on that. If you need a refresher on polymorphism, I wrote this blog post that might help: theliquidfire.com/2014/12/01/random-encounters-and-polymorphism/
|
|