- Home /
How can I best update and compare a struct?
Hello all
I'm using a struct to encapsulate a host of variables for a flocking algorithm. I have a class for the flock manager set up in the observer pattern that has all of my public variables that affect how the flock behaves.
Currently, I'm having to compare all of my variables to the stored info in my struct which is tedious to write. I have a huge f(a != struct. || b != struct.b || c != struct.c || d != struct.d) statement comparing 20 variables to each corresponding variable in the struct, and if any of them are different, it updates the struct.
Is there a better way to monitor a list of variables (they must be serialized so that I can adjust them for preview) without a gigantic if(a != struct. || b != struct.b || c != struct.c || d != struct.d) statement?
Or, even if I weren't using a struct, how could I best monitor a large set of variables for changes?
Thanks in advance
Answer by mindlube · Sep 13, 2012 at 05:30 AM
Depends what you mean by "large set" but , I recommend a C# flags enum for your implementation. You can efficiently use bitwise operations to test for changes in the state of your flocking behavior. You can still use a struct and pack it all into one variable.
http://msdn.microsoft.com/en-us/library/cc138362.aspx http://stackoverflow.com/questions/8447/enum-flags-attribute
Hmm, ok. Well, looking at that page and researching a bit on [Flags] for structs, I'm still not sure how that works. Thanks for the tip. I'm just not sure what to do with it :)
Hi zombience, a flags enum is just an integer value, but because of how it's defined you can also test it for whether it contains various combinations of the enum. Here is another article about it http://geekswithblogs.net/BlackRabbitCoder/archive/2010/07/22/c-fundamentals-combining-enum-values-with-bit-flags.aspx Whether this will work, I guess it depends on what types and values you are packing into the struct. If you cannot fit all the possible values into a flags enum (limited to power of two values - 0, 1, 2, 4, 8, 16, 32, ... Int.$$anonymous$$axValue) then I suppose you can throw this idea out :)
ah, yeah, that may not work then. I'm basically looking for a way to have a manager script that has one set of variables (around 20+ float variables) that all get saved into a struct and then each individual object in the flock references those variables to perform it's own calculations. I guess ins$$anonymous$$d of referencing a struct, they could reference the manager script. but then i'd need to make it a singleton pattern, and i'd like to have multiple managers. So, when I update the manager script's variables, I'd like it if the main script knew that a variable had changed, and that it should update the struct. I have it curently set up, but I have it running a very ugly looking and tediously long if( a != struct.a || b != struct.b ....) statement. To slow down the checking, I have a coroutine running that only checks every .3 seconds since these are just main initializing variables. Anyway, thanks for the info! I'm sure I'll have use for using [Flags] with enum in the future. I like using enums, but I didn't know about this functionality.
now that i've been doing this a bit longer, yeah, this hardly qualifies as "large set". heh! thanks again for the info