Question on Arrays

0 favourites
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • okay, so,

    This is technically a C2 question, but the response time for C2 is incredibly slow.

    And no, I'm not updating to C3 and this question is still valid in C3,

    With that out of the way,

    I have an array, this array will be used to store positional data ( X, Y, Zindex ) and the instance type as a string.

    I haven't gotten to the string part, as it's a last step.

    The point of the array is to save this data to a Json file, which I would then use; how is irrelevant.

    I'm using this for a ship construction system where parts of a ship can be moved around and built, so obviously it would be nice to have a way to save your ship.

    I picked this method as I can expand its functionality to whatever I want, character customization, ship customization, planets, whatever.

    My issue:

    I'm trying to save multiple object's positions to an array, unfortunately, every time I do, it saves it to only one row on the X axis, each row is supposed to be its own instance, the rest of its information is on the Y axis.

    Heres the script:

    Heres the result:

    Heres how it should look like:

    The last image ( how it should look like ), obviously has made up numbers, but it shows how I want it to look, By the way, I only roughly understand how to use arrays, I've been using tutorials, documentation,

    and this is as far as I got; which is pretty far because it almost works.

    So, how would I add information for each new entry?

    I've tried " For each Element " couldn't get it to work as intended but thats likely because I misunderstood how its used.

    To make this clear, I just want to know how to fix this issue, so try to stay on subject, I am well aware there are multiple methods to do this, and that there are also multiple ways I can carry this out from here, but to make sure your point is understood some help on this would be appriciated.

    If you have to correct my use case, thats fine too; the goal is to learn.

  • each row is supposed to be its own instance, the rest of its information is on the Y axis.

    I think you meant each Column is supposed to be its own instance, this is if you want to save the rest of the instance info on the Y axis as you mention.

    At the moments:

    -You are inserting values only on the Y of the first raw as you are not defining the X.

    If you want to save each instance on its own column like (X0 Instance1, X1 Instance2,X2 Instance3, etc...)

    Here is a better approach:

    Before you start the loop to insert values you need to set the size of the Array once and do it outside the loop

    To a size(0,3,1)

    Then in the loop:

    For each Armor is overlapping:
    -----> Array Push Back Armor.X
    -----> Array Set at (Self.width-1, 1) Armor.Y
    -----> Array Set at (Self.width-1, 2) Armor.Zindex
    
  • Array set at ( XY )? I don't know which type of setting your using.

    the next part containing the width, not sure what this is for? Is this to get the next column down? If so, what's the point of having a push back at the start?

    both the X and Y data of the object must be stored on the Y axis, which we seem to be on the same page on, but I don't quite understand the steps shown in your example.

    So I'm pushing back everything on the X column ( I assume that's what this means ), so that I can add a new entry, then I'm setting on a new entry my X, Y and ZIndex information on the Y axis?

    Perhaps some more clarification is best.

    Edit: I just realized this isn't going to work anyway, your suggesting I keep the size set outside of the loop, but when I do this in practice, it never adds a new column since I'm using the size set to ADD a new entry.

    I assume somewhere in this there is a different way of doing it?

    To be clear, I can't set the exact size of my array, this is a ship customization system where a player can always add more parts to a ship, so I won't know how many parts are going to be listed unless I add onto the size each time they create a new part.

  • push back inserts a new row at the bottom of the array, which is what you need here, but it doesn't set all the values, instead you would see armor.x,armor.x,armor.x. So to set the Y and Z in columns 1 and 2, they are using set at X,Y.

    self.width is the number of rows and since the rows likely start at 0 index it needs to be self.width-1, basically self.width-1 is the number of the bottom row, the one you just added.

    So the logic above is saying - insert a new row (push back a value) which sets (new row,0) to armor.x, then set (new row,1) to Y and (new row,2) to Z.

  • Wait never mind I think I got it to work, I had to tinker around with it but it seems you set its size to zero,

    but pushing back on the X axis with Armor.X adds a new entry, in my opinion is super weird.

    Yeah it works and I sort of understand whats going on but it isn't very intuitive.

  • Lmk if this looks right, it looks right to me and it seems to be adding a new entry, so now If I wanted to add the instance name ( which in this case since C2 doesn't have instance naming ) Ill just make a variable which does that for me.

    so itll be, X, Y, ZIndex, InstanceName(A Variable containing the instance's name, because construct runs on reference, if I define what that instance is in the loop, I can set the value to the variable stored in the instance.)

    When I start loading, I check the array, I look across its indexes for the name through a comparison, if object = object then spawn object, then I set the position of object with its other info.

  • That looks right. Btw just in case you are just trying to create a save game here there is a simple option of system 'save'.

  • Not scalable, I had a feeling this would be mentioned but learning to save stuff by array gives me more avenues.

    Also, it consumes alot of memory from my tests, I had to run javascript code to clear memory for my game's save file system which is incredibly dangerous.

    So technically I already use this but not for this purpose.

    I'm doing a precision save on a group of objects which allows me to do a whole slew of things,

    like making a ship purchase system where instead of buying parts separately, buy a whole ship.

    I could use arrays to save enemy ship designs, making encounters possible.

    I could even repurpose it to make planetary generation with presets, or maybe use it for character customization, hotbars for items or even inventories.

    Anyway, before this comes to a close, ( assuming I don't find any issues ) here is my save system partially set up in action:

    Thank you both of you for your help, I learned abit today.

  • You have set up the saved items but you will also need to create the logic for spawning, which means checking and using the array, so do post here again if you have any trouble with that.

  • Perhaps some more clarification is best.

    Yeah, great explanation from lionz, I dont think it needs more explanation than that:

    A few points that I think are important:

    but pushing back on the X axis with Armor.X adds a new entry, in my opinion is super weird.

    Is exactly the same thing you were doing:

    You were using "Insert" in the Y so what this does is insert a new Row.

    I use "Push" which is the exact same thing I push a new Row or column.

    The only difference between the two is that "Insert" lets you pick the (Y or X) index where you want to insert the Row or column. And push just insert the new (Row or column) at the back or the front.

    "Yeah it works and I sort of understand what's going on but it isn't very intuitive."

    Is quite intuitive and safer than the system you were using as By inserting always back you protect all the Rows and values that you already had in place for the old parts, especially since you say that ober time you gonna keep adding new parts, therefore you better no change any index reference. So if you you keep inserting in any index you will have the risk of breaking your code as you gonna have to constantly change the references that you already had in place to get the old part values.

    On the other hand, if new parts get inserted in the back then all the old parts are protected since you do not change their index reference ever.

  • Awesome.

    Yeah, it works perfectly.

    I went ahead and made a paste-bin style loader, using the Ajax method.

    There are two arrays, one which stores data and can be cleared for new save data, lets call it " ShipArray ", this is the ship's current layout, technically.

    The next is called " ShipPaste ", this is the paste bin which I'm reading from.

    I got this working here, now, how would I read the data on this sheet for each entry on it and create objects?

    ( Im not sure if this gif is gonna work, I hope it does )

    Edit: It did not, hold on let me just send the link by itself

    i.gyazo.com/38ca21f14db7d602bce5879a93aee365.gif

  • Shrug, I have no clue and I'm not gonna pretend I do.

    How do you illiterate and do an action for each instance on the X axis?

    This was my attempt, to be crystal clear, this is a downscaled loader, I hooked it up to clicking for testing purposes, I have the save file for simplicity.

    Now that its loaded onto my paste-bin array, how do I read it?

    I tried looping and comparing it with " Compare Current Value " on the X axis,

    but uh, its doing ??? idk nothing I guess.

    Doesn't seem to do anything anyway,

    Its description explicitly states it compares a value with a value through a for each loop.

    Doesn't seem to do that at all in practice, perhaps it doesn't know where to look?

    Edit: I got it working lol

  • Remove the trigger once. It does nothing when under a loop.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Documentation states it only works in a loop, I accidentally posted a picture which was before I figured that out.

    Here is it working now, I have the correct *count* of instances being created, now that I can read it,

    Currently I have it set to " CurY " which is obviously gonna give me the current value being read, being 0-based, it is only reading the first value of the Y axis.

    how do I specify which index on the Y axis to read? ( In an expression, to make sure everyone is looking in the right spot, look where I'm spawning the armor piece, this is where I set my filler "CurY" values, which I wish to change to a proper index search )

  • So you’re using the array so that its size is:

    (Count, numProperties,1)

    And you access values with:

    Array.at(n,property)

    Where n is the index to the object and property is:

    0 for x

    1 for y

    2 for zindex

    3 for name

    … and so on if you add more properties later on.

    Looks like you already have saving down but here is an example. You can set the array size first and just fill in values but I find it’s easier to increase the size as you add objects, especially if you have multiple types you’re saving. Anyways, “push” or “insert” basically will change the size of the array, but to be super clear about it you can just set a new size like here.

    On function “save”
    — array: set size to (0,4,1)
    — for each sprite
    — — array: set size to (self.width+1, self.height,1)
    — — array: set at (self.width-1, 0) to sprite.x
    — — array: set at (self.width-1, 1) to sprite.y
    — — array: set at (self.width-1, 2) to sprite.zindex
    — — array: set at (self.width-1, 3) to “sprite”

    For loading you’d still do “for each x element” instead of xyz. And you’d use array.at(n,3) to get what the name of the current object is.

    On function “load”
    Array: for each x element
    — compare: array.at(array.curx,3) = “sprite”
    — — create: sprite at (array.at(array.curx,0), array.at(array.curx,1) 

    Now a further improvement so it actually loads the objects in the same zorder as you save it is to rearrange the order of the values to {zindex,x,y,name} and sort it. That would only change the save function.

    On function “save”
    — array: set size to (0,4,1)
    — for each sprite
    — — array: set size to (self.width+1, self.height,1)
    — — array: set at (self.width-1, 0) to sprite.zindex
    — — array: set at (self.width-1, 1) to sprite.x
    — — array: set at (self.width-1, 2) to sprite.y
    — — array: set at (self.width-1, 3) to “sprite”
    — array: sort by x
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)