|
Post by Legend on Aug 10, 2018 22:48:25 GMT
hello
so i had found a bug in my code and was wondering if there was an easier way to code this basically buffaction on perform buff action
foreach enchantable target in action targets
i had this:
target.add(action.buff);
when i negated the duration stat it would actually negate the stat multiple times for all the creatures that were being buffed
what i had to do was foreach enchant target in action targets create a new instance Buff buff = new Buff();
set the property one by one then
target.add(buff);
so what is happening is that i am THAT buff object to each of the creatures then each time i go through the creatures it negates one when the turn hits so a 3 turn duration card with 3 creatures on the field could end that turn if it targets 3 creatures.
whereas i create a new instance and then apply it....which is actually what i wanted but didnt reallize...BUT if i want to create an exact copy of that object is there an easier way to do this then actually equalling all the properties out?
for instance buff.stat = action.buff.stat; buff.amount = action.buff.amount;
and like 10 other properties i have
hope that makes sense thanks for always helping!
|
|
|
Post by Legend on Aug 11, 2018 5:59:07 GMT
So upon further review - no there is no actual simple way i am missing - there is some classes that can be made but i was thinking if there was a instant solution that i may not have known
public static T DeepClone<T>(T obj) { using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0;
return (T) formatter.Deserialize(ms); } }
|
|
|
Post by Arghonaut on Aug 11, 2018 9:36:41 GMT
In this situation i just make a constructer that takes an instance of the class, ie: Public Buff (Buff buff) this.amount = buff.amount; this.stat = buff.stat; //etc etc Then you can just do Buff buff = new buff(BuffYouWantToCopy); Admin probably has a way of doing it involving some black magic wizardry
|
|
|
Post by Arghonaut on Aug 11, 2018 10:32:39 GMT
Sounds like you can also get around it by refactoring your code. I'm guessing at present you iterate through a list of each minion in play and reduce the buff durations and because buffs can have multiple targets they get reduced multiple times. If instead you had a BuffSystem class or similar that stored a list of all buffs in play (via subscribing to buffaction etc) you could iterate through the list of buffs instead.
|
|
|
Post by Legend on Aug 12, 2018 4:34:59 GMT
ya actually i thought about using a constructor just this morning and might go that route if i actually have to do it more than once...i do have a buff system in place already. Actually how i wanted it to work was that the card creates a buff desperate for each creature anyways but it was assigning that same buff to each creature - hence why on the change turn it was negating it multiple times...i have a list of buffs assigned to each creature which works for me i just iterate through them if i need - which i only do on minion refresh and the change turn right now
Also that deep copy way might also work in the end - i might try that method too just to see if it works. thanks!
|
|
|
Post by Admin on Aug 14, 2018 14:34:11 GMT
Good comments/advice in here! I definitely agree that each target should get its own buff instance. I might consider configuring it such that the action had a sort of buff "recipe" rather than an actual buff itself. The recipe could be defined in JSON or as a Scriptable Object Asset and could be reusable. The recipe could then be consumed by some sort of factory class or factory method to create the instances of the buff that are actually applied to the targets.
I also agree that having a separate system manage the life span of a buff as a result of observing some other notification, like a turn change, would be a good idea. It would be very easy to simply loop over the list of buffs and apply group logic in this way.
|
|