Search code examples
arraysactionscript-3

Can I manipulate a single array from multiple classes in AS3?


I have a class which contains an array. I seem to be able to manipulate that array fine from multiple classes, but it is not without bugs. In my debugging efforts, I came across this which makes me think that what I'm trying to do is the "wrong" way. Am I to understand that I can access an array from multiple classes (via getter/setter functions or making it a public property) but that if it is manipulated by 2 different classes in one frame/loop/tick, that the Array "reference" (or whatever) will not be up to date across all "references"?

If I've understood this correctly, a simple "yup" will do. If I'm missing something, please help.

Any tips on how to structure a program so that you don't run into this kind of issue?


Solution

  • No. AS3 main loop is synchronous, so you can't get two different classes conflict over setting that array at the same time, unless they both won't use its current state when deciding what to change, like storing an action based on that array's data to do after returning control to main event loop. Otherwise, if you have multiple event listeners in multiple classes/instances that all listen to the same event, they all get queued somehow (details depend on adding event listener order, display list structure, listener type of capture/bubble if applicable, etc), and when a certain class gets its event listener run, it can be sure that the array in question isn't manipulated elsewhere but its own event listener. So, with normal event listeners you are safe to employ simple access model to your shared variables of any kind.

    The Worker is a different thing, these use separate threads and thus are truly parallel, so extra security is required to both plan and implement parallel processes accessing the same entity for read and write.

    UPDATE: I have read the referred answer and have found it confusing. That one means that if you don't update something regularly based on some global variable's content, you will have to tell a displaying entity that the global data has been changed if you need it to update itself immediately. This is not the exact conflict, but rather an inconsistent state across application if your view is not updating its data from model. It's rather a design flaw than an actual access conflict. Skip it.