I would like to get the Sum of all Alterable Value Bs for a particular object. What is the easiest way to do this?
Printable View
I would like to get the Sum of all Alterable Value Bs for a particular object. What is the easiest way to do this?
The best way is to spread an ID value into another alterable value, say A for each of the objects. Then run a loop as many times as there are objects. Using the loopIndex function and with only one line of code you can make it so that on loop 1 the alterable value B of the object with ID value 1 is added to a counter , on loop 2 the alterable value B of the object with ID value 2 is added to a counter and so on.
Let me know if you need an example.
- Give your Object a new value called Index_ or ID_ or somthing similar
- At some point ( before you want to sum up the values ) use 'spread value 0' in Objects Index_ to give each Object a unique identifier
- Now you can run a 'fast loop' ( you should run it 'number of Object' times ) and compare the fast loops 'Index' value to the Objects Index_ value to retrive each Objects value B
...
The ForEach extencion will also do the trick, with slightly less effort
That's what I said.
Here's an example for if you are new to fast loops or just don't have any idea what we are talking about.
http://www.whenthereisnoroominhellth.../addvalues.mfa
What I would do is use the ForEach object, as King_Cool mentioned, and loop through each object and add the sum to a value/counter.
Why use the ForEach object? Fast loops work fine.
ForEach is designed for object selection, and its tests proved faster than fast-loops.
Oh really? I haven't used it before. I try to avoid using extensions if I can help it.
Did you check out my example? In 3 lines of code it added up all Alterable B values. I refined it now down to 2 lines. Click the same link if you want to see. How many lines for the ForEach extension? Also, unless you are adding a very large amount of object values together I can't imagine speed between the 2 methods noticeable in any way.
I'll be happily prove wrong.
Well, let's say you have 10 active objects and you want to loop through them with a fastloop. When this happens the "on loop" action will trigger 10 times and each time there'll be 10 value comparasions (one for each object) to filter out all but the correct object for this loopindex. That's 100 comparasions. If you have 50 objects, there'll be 2500 comparasions. For 100 objects, 10000 comparasions. It gets exponentially slower the more objects you add.
Sure, looping through objects with fastloops work for very simple operations, but for more demanding operations, or just games where you iterate through objects a lot, the ForEach object is absolutely essential.
In every game since Knytt Stories, the slowness of fastloops have been a problem for me, in Saira it's the main CPU eater of the entire game. Then, the ForEach game came along, and my new Knytt game wouldn't even work without it.
That's good to know. I'm working on a pretty big project at the moment but I am yet to notice a decrease in speed from using fastloops (and I use a lot) but when I do I know what to use instead. Cheers.
I do find ForEach extencion easier to use than fast loops
My only concern about using the ForEach object is that i dont 'think' its supported when exporting application to flash
While i 'think' fastLoops are supported
The ForEach object IS supported by flash and FastLoops definitely are because they are part of MMF2.
Does anyone know if the ForEach object has received any updates for flash during the last year or so? I find it quite slow and buggy in flash.
I dont own the flash converter
What exactly do you mean when you say slow and buggy?