User Tag List

Results 1 to 9 of 9

Thread: "Simultaneous" changes to the same variable - Only some changes applied?

  1. #1
    No Products Registered

    Join Date
    Jan 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Simultaneous" changes to the same variable - Only some changes applied?

    I've got a peculiar issue with my logic. I've set up a behaviour on an object (a particle type thing) that when a certain action triggers should increment a global variable. The problem I'm having is that, sometimes there can be quite a few instances of that object so, when the particular event is triggered on multiple instances at the same time only some of the objects actually increase the global variable.

    For example: I've got 3 of this particles in the scene. They all may trigger the event with about half a second time interval between them. So the global variable increases from 3 to 6 after all three particles triggered. Everything is fine. However, let's say I've just created 6 more of those particles, 3 of them trigger the event at the same time (or with extremely short time between them) and the other three trigger with a bit of time in between them. Now the global variable would have gone from 6 to 10 instead of 12 as it should have.

    Here's a gif illustrating my issue:
    [[I would've embedded it but apparently I can't post pictures since I just registered my account here... Silly]]
    i.imgur.com cVIlBWc.gif

    Every time I click on the "bot" thing it spawns 5 of these particles, when they reach the counter, they should increment the global variable by one each and then destroy themselves. They all clearly destroy themselves, but they don't all increment the global variable.

    The relevant event is in a behaviour inside the particle itself and looks like this:

    + Flag 2 is on?
    - Add 1 to Energy
    - Destroy

    Flag 2 is a flag I use to tell the particle it's next to the counter. That clearly work since all particles get destroyed as they should.

    I've included the mfa file, it requires the Advanced Direction Object extension. The particles in question are in the "Game" scene, under a folder called GameObjects.

    Any advice is very much appreciated!

    EDIT: Forgot to mention that I use a custom movement on the particle so some solutions might not work, like checking collision between objects.
    Attached files Attached files

  2. #2
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Location
    UK
    Posts
    1,398
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Firstly, behaviours suck - don't use them.
    The way MMF2 works, if any instance of "particle" has Flag2 on, it will add 1 to energy. It will NOT add 1 for each instance of "particle" that has Flag2 on.
    The simplest solution is just to use the ForEach object to check the Flag of each particle, one at a time.

  3. #3
    No Products Registered

    Join Date
    Jan 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the advice!

    So you're saying I should put all the events of a whole game in the frame event panel? There's already 60+ events in total in my extremely simple prototype, this is going to be very quickly impossible to manage...

    Regarding flags and instances, I have to say that I think you're wrong, at least in behaviours. The particles add 1 to energy each fine if they don't trigger the event at the same time (check the gif, it demonstrates this). So it seems like the flag is being checked per instance.

    Now if the event was in the frame event list I could imagine this to be the case and then I would probably have to use ForEach.


    At the moment I've "fixed" the problem by using the bouncing ball movement instead of my custom movement and then check for collisions with an invisible object that I place around the counter. Now this works well in this case since collision detection is an instant event as opposed to check flag. However, I would like to know how to deal with problems like this when using custom movements.

  4. #4
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Location
    UK
    Posts
    1,398
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Yes, I would put all events in the main event list. I would also only use the "event list editor" and not the "event editor". If you put everything in groups, you can easily hide events away by clicking on the group header - it's far more organized than having events scattered all over the place, with some in the main event list and others spread among various different objects' behaviours.

    The "if they don't trigger the event at the same time" part is what's important - the event can only be triggered once per frame (ie. 50 times per second), whether it's in a behavior or not.
    Like I said, events contained in behaviours are handled in EXACTLY the same way as any other events. If you use the ForEach extension, it will fix your problem.

  5. #5
    No Products Registered

    Join Date
    Jan 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fair enough, I guess it's a matter of getting used to it. I'm coming from an object oriented programming background and isolating behaviours in the objects that relate to them is common practice. But I'll try to clear my mind of that .

    Yes, I see now that I need to take care of execution order and the limit of one event type per frame (again, coming from other, programming based engines, this is rarely an issue), but apparently, instant events (those that show up in red in the event editor) don't seem to suffer from this limitation.

    Now, regarding the ForEach extension, I have to wonder if it's not possible without it. I'm trying to do things without extensions since this is for a University exercise and they encouraged us to avoid extensions. Maybe something with spread values over an ID variable for each object?

  6. #6
    Forum Moderator Fusion 2.5 DeveloperFusion 2.5+ DLCAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleMac Export ModuleSWF Export Module
    AndyH's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,448
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Nelien

    First of all, Event Behaviors are really useful and can help you to reuse event code and keep things tidy so in my experience its not a problem to use them and I would in fact encourage to use where appropriate. There are some limitations as you have probably discovered (for example you don't get to see the alterable value names of other objects, no access to qualifiers and you have no influence on what order they will appear in the game loop events) but they are not to be avoided IMHO, just need to be understood.

    Coming from an OOP background I think I can see your reasoning, but event behaviours are just added to the end of your frame events so will work exactly the same in a behavior as they would in the frame events. One of my long time wish list items would be for Fusion to give us an Instance Behaviour - effectively treating events within as if attached to a ForEach condition. (another would be for a setting on event groups to perform this). However that is not something that Fusion supports at the moment but it is easy to implement with your own events.

    For your problem, I think you are completely right as to why the global value is not incremented due to on some game loops two or more objects are triggering that event condition at the same time. Conditions perform actions on instances, the event condition has been triggered by more than one instance in a game loop so the event is "true" but this causes one action on one counter. So in this case, the advice given to use a ForEach object I think is necessary because you want the event to be tested per instance. I'm sure you could find other ways to do it, but for this method you would need to work on each instance individually. Shame you are not using Fusion 2.5 because ForEach is now built in. If they won't mark you down for using this extension in MMF2 I would recommend sticking with it because you will need to deal with conditions on each separate instance from time to time.

  7. #7
    No Products Registered

    Join Date
    Jan 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply AndyH

    Unfortunately my University doesn't have licenses to Fusion 2.5 as they feel that MMF2 is enough for its purposes. I guess I can see their reasoning, but I, like you, wish they would upgrade. In the end, I guess using an extension isn't going to affect much, especially one so essential like ForEach. For this exercise however, I'll stick with not using it and use my own "fix" for it as I mentioned a couple of posts before.

    At least now, I've gotten confirmation of what's going on and a bit more insight of how MMF2 works behind the scenes.


    I don't want to get too far off topic but you mention that behaviours can help you reuse code. Can you give me an example of this?

    Again, thank you for your help.

  8. #8
    Forum Moderator Fusion 2.5 DeveloperFusion 2.5+ DLCAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleMac Export ModuleSWF Export Module
    AndyH's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,448
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A behaviour can be created on an object to provide some functionality. You can then add this object to any frame and to any game to quickly utilise that behaviour. Results will vary because you may often have references to other objects but it can be easier to share behaviours than it is to copy and paste frame objects and frame events. Here are some behaviours in the widgets forum.

    I use behaviours when working with Global objects. I can, for example put logic for my player in the event behaviour, tick it's global flag and then drop it in all the frames of my game. I could have 10 or 1000 frames, but when I need to change what my player does I can edit the behaviour and all 10 or 1000 frames will instantly get those changes too.

  9. #9
    No Products Registered

    Join Date
    Jan 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaah, between frames of course! I've been working on 1 frame only so I completely forgot the need to have the same objects in multiple frames . I can definitely see the power of behaviours there, it's "code in a box"!

Similar Threads

  1. "Cannot load joystick2.mfx" when "Compress the runtime" unchecked. Fine when checked.
    By DistantJ in forum Multimedia Fusion 2 - Technical Support
    Replies: 3
    Last Post: 14th September 2013, 09:44 PM
  2. "Bug" For Saving and Loading Frame Positions AND "Timer Glitch"?
    By Zarpo in forum Multimedia Fusion 2 - Technical Support
    Replies: 1
    Last Post: 27th March 2012, 12:40 PM
  3. How does the "Iterate Array" action in the Named Variable object work?
    By RGBreality in forum Multimedia Fusion 2 - Technical Support
    Replies: 11
    Last Post: 7th December 2011, 04:31 PM
  4. "Subtract" effect is buggy when applied to layer
    By alxmrg in forum Hardware Accelerated Runtime
    Replies: 6
    Last Post: 8th July 2009, 01:14 PM
  5. Variable "Jump Strength" (Height) for platformer
    By Sebaceous in forum Multimedia Fusion 2 - Technical Support
    Replies: 13
    Last Post: 21st April 2009, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •