How do I use Arrays like variable (Pass them as parameters, set one equal to another, etc.)?

0 favourites
  • 6 posts
From the Asset Store
The game will give you a dog that appeared and you must say if he appeared more, less or equal.
  • I would like to use arrays like variables. The four things I would like to do are:

    1. Pass them as a parameter in a function
    2. Make new ones as local variables (or the output of functions) to handle data processing on the fly
    3. Copy one to another, (or add the end of one to another)
    4. Check to see if one is equal to the other

    3 and 4 I can obviously do for 1-D arrays using the splitstring, but sometimes it would be nice to do with 2-D arrays. Maybe I could do it with JSON?

    My best solution for passing them as parameters is to make a parameter that holds the UID and make arrays a family. This works but it is clunky and the data I want to pass needs to be in a proper array.

    Temporary Arrays as local variables I have no solution for.

    I first tried to solve this problem years ago in the C2 era, but I eventually got frustrated enough to start using token separated strings as arrays. Back then there was no split string function so what should have been simple like adding one array to the end of another was super annoying. I wrote myself an event-library of functions to handle the missing functionality on token separated strings and it has worked quite well.

    I would like to go back to using the array object but not being able to use them as local variables is quite frustrating. I wish that arrays were just a variable type in C3 rather than an object.

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can pass Array.AsJSON and set array from JSON string. But of course it's still cumbersome to use.

    Another option is to create multiple instances of array object, then you can store different data in each instance and pass array.UID as a parameter.

  • Using some kind of id to reference each array and having helper functions to access or manipulate them is probably the easiest way to go. Uid is good as an id since you can access newly created instances immediately. Iid is an alternate idea where you can access data without a helper function. For ex: array(a).at(5). But for iid you’d not want to be creating instances on the fly, instead you’d want an object pool that you’d allocate from.

    Global, static or instance variables would require you to create arrays. And for instance variables you’d need to manually destroy them.

    For local variables you could either manually destroy them when done with them (tedious to keep track of) or just mark them as temporary and then clean up all the temporary instances at the end of the tick. Still it will be on you to make sure you don’t store temporary arrays in non local places.

    Another strategy for locals is to just have a small amount that you juggle the use of. Typically you don’t need too many.

    You mentioned json. You could use a single json object to store all the arrays instead instances of the array object. I say a single instance because that could be simpler to work with than dealing with picking.

    If you use helper functions to access stuff you can implement things anyway you like under the hood. Here’s a possible design of the helper function.

     new(tempBool) -> id
    delete(id)
    isTemp(id) -> true/false
    clone(id) -> newId
    copy(id, other)
    equals(id1, id2) -> true/false
    clearToValue(id, value)
    mergeX(id, otherId)
    mergeY(id, otherId)
    getwidth(id) -> width
    getheight(id) -> height
    setSize(id, width, height)
    get(id, x, y) -> value
    set(id, x, y, value)

    Anyways. Just some ideas. I’ll have a look at an implementation later. There are usually simplifications that can be done.

  • Here's an example. Uses helper functions with uids to access 1d arrays. local arrays are created as temporary and are automatically cleaned up each tick. Shows examples of global, local, static and instance variables.

    https://www.dropbox.com/scl/fi/92uyaria57umj85d6vsps/arrayVariables.c3p?rlkey=kcnxwxheph0g7poe3lo7h7qc1&dl=1

    Could be modified for 2d arrays.

  • Thanks for the help everyone. For some reason it never occurred to me that I could create multiple instances of objects that don't have a location on screen.

    I think I will go with creating instances of array that have a instance variable called name and making sure I name them each time. That way I can create and destroy them as needed

  • For some reason it never occurred to me that I could create multiple instances of objects that don't have a location on screen.

    Yeah, it's a forbidden knowledge haha

    It's also possible to add an array to a container with other objects. For example, if you add StatsArray and Enemy sprite to a container, then each enemy instance will have its own instance of StatsArray.

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