Are "global" arrays just 1 instance and I do or doNot have to pick?

  • Since I can't return an array of values from a function, I believe my only choice is to set values and in various global arrays. This produces "code" that's insanely hard to read. I like self-documenting, readable "code" in my projects. I use lots of vars with meaningful names in place of magic-numbers/strings to help achieve this.

    So when using "global" arrays, is there only ever a single instance when I add it to the project? I don't have to explicitly create an instance in code? In my functions, and their calling code, do I have keep using "global array pick-by-uid" events to get a reference to the single global array? Or preferably, can I just reference the single global array (no pick-by-uid needed) in expressions and I will always be referencing that one single/same "global instance"?

    When you have couple minutes free, please help me understand above correctly.

    Also...

    When you're building functions that need to return complex(ish) sets of data, how do you do it?

    Thank you for sharing your Construct 3 expertise!

    Mark :-)

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • locohost

    If you only have one instance of an array, you do not ever have to pick it.

    You can add multiple array objects and give them different names (just as you do with sprites), then when you refer to a particular name, you know which one you are working with. eg. playerArray.at(...) enemyArray.at(...) highscoreArray.at(...)

    you CAN use the System Create object to make additional instances of an array at run time... then you DO have to make sure you have the correct one picked. (You can give arrays instance variables which gives you addition ways tell instances apart).

    if you need to store an array of data for each instance of another object or family, you can make that object a container, and put an array object in the container. Then, when you create the object, the array gets created at the same time, and you will automatically have the correct array instance picked whenever the container object is picked. (and the array instance will be destroyed when the container object is destroyed).

    You can have a function return multiple values by concatenating them together (using a delimiter), and then use TokenAt to reference the value you need later... but that doesn't help readability.

    I like to use a lot of instance variables on objects, pass the UID of the object (and any input values as additional parameters) to a function to have it set all the instance variables as required. Then outside the function you reference the instance variables. That works best for self documenting. Using arrays is less readable because you have to figure out what each element of the array is actually storing. Although I have used dictionary objects to help with that, but that makes the lines of code even longer. I have also defined local variables to use as constants just to make lines of code easier to read... eg:

    name=0

    health=1

    strength=2

    set text to array.at(currentplayer, name)

    Set array.at(currentplayer, health) to 5

    ...

    (the variable definitions help document what is in the array. I also add comments at the beginning of every function to say what to pass in to the function, what the function does, and what it returns. If there is an array involved I will also have a comment defining what the elements of the array are - if I don't have the constants).

    but like I said, I would choose instance variables over arrays unless you have a very large amount of data to store for each object. And normally, I will have the objects in a family and put the instance variables on the family.

  • That's exactly the info I needed. Thank you, thank you my friend!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)