How do I load custom levels with JSON and arrays?

1 favourites
  • 5 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I'm implementing a level editor into my game, and I've gotten stuck on the save/load function. I'm trying to make the custom levels as simple as possible by having a single JSON file to load to. To accomplish this, I've made several arrays for each object type (coins, obstacles, etc), and then I have a master array that saves the JSON of each array on its x-axis. Then I add another x value for the JSON of the tilemap. My first few tests seemed to work, I could move objects around and then reload the level from the JSON file. However, I quickly ran into problems that I can't identify the source. The Tilemap does not load any data, but also doesn't clear itself, so the tile data from the previous level remains. Sometimes objects duplicate themselves multiple times, or fail to spawn at all. When they do spawn, their height and width are all messed up.

    capx is here if you wanna take a peek and help me out. All the data you're looking for is in the EDITOR layout and event sheet. There is another saving/loading group that is disabled; it's an earlier system I tried using system save states, but it was also not without its issues.

    drive.google.com/file/d/1qyT8soZvwN-WpgXEh9aUDXVFlMcPDXAy/view

    If someone actually took the time to dig in, fix and optimize my system, I wouldn't mind throwing them some money through paypal or something. Thanks in advance :)

  • I can open your capx as it uses 3 Plugins, but looking at your Picture of the Events this is what I see

    On your Function "On Save" you are downloading the JSON Array "edit_objectdata_AMASTER" before you save all your Info of the Objects of the Level, The Array "edit_objectdata_AMASTER" download AsJson it should be at the last Event after you save all the Info on it.

    Also on the same Function "On Save"

    Make sure you set the size of the Arrays to = 0 before you use the Expression Insert so you don't create extra Empty Cells on the Arrays

    Example:

    Before the Loop set Array Size

    W = 0

    H = 1

    D = 1

    Then use the Loops to save the data example to save the coins:

    -set Array Size

    W = 0

    H = 1

    D = 1

    -Then For Each Coin: Array Coins Push Front Coin.AsJSON

    Note: The Array Sizes you set them Once only and outside the Loops, probably at the Beginning of the Function on Event 78, set all your Array Sizes there

    Also, you don't need to clear the Arrays if you set the Size (W = 0)

    =--------------------------

    Now if you Did what I Wrote above you should have each Array as 1D = 1 Dimensional Array

    so you need to change the Events On Function "BuildLevel" Especificly the Loops that you use on the Array

    -Instead of using Array For Each XYZ you need to replace it by Array For Each X Element

  • I can open your capx as it uses 3 Plugins, but looking at your Picture of the Events this is what I see

    On your Function "On Save" you are downloading the JSON Array "edit_objectdata_AMASTER" before you save all your Info of the Objects of the Level, The Array "edit_objectdata_AMASTER" download AsJson it should be at the last Event after you save all the Info on it.

    Also on the same Function "On Save"

    Make sure you set the size of the Arrays to = 0 before you use the Expression Insert so you don't create extra Empty Cells on the Arrays

    Example:

    Before the Loop set Array Size

    W = 0

    H = 1

    D = 1

    Then use the Loops to save the data example to save the coins:

    -set Array Size

    W = 0

    H = 1

    D = 1

    -Then For Each Coin: Array Coins Push Front Coin.AsJSON

    Note: The Array Sizes you set them Once only and outside the Loops, probably at the Beginning of the Function on Event 78, set all your Array Sizes there

    =--------------------------

    Now if you Did what I Wrote above you should have each Array as 1D = 1 Dimensional Array

    so you need to change the Events On Function "BuildLevel" Especificly the Loops that you use on the Array

    -Instead of using Array For Each XYZ you need to replace it by Array For Each X Element

    I caught the mistake on function BuildLevel, where it was repeating only the coin's x element. I also added events to clear the arrays and set their sizes to 0,1,1. However, the levels are still not loading properly. When I load up a save of the default template, only the coins spawn properly, and one of my trampoline2 objects shows up out of nowhere. I'm really at a loss haha, this is a bit disheartening. Let me know if anything in this updated code stands out to you, cause idk what I'm doing wrong. Something in the load process maybe?

  • > I can open your capx as it uses 3 Plugins, but looking at your Picture of the Events this is what I see

    >

    > On your Function "On Save" you are downloading the JSON Array "edit_objectdata_AMASTER" before you save all your Info of the Objects of the Level, The Array "edit_objectdata_AMASTER" download AsJson it should be at the last Event after you save all the Info on it.

    >

    > Also on the same Function "On Save"

    >

    > Make sure you set the size of the Arrays to = 0 before you use the Expression Insert so you don't create extra Empty Cells on the Arrays

    > Example:

    > Before the Loop set Array Size

    >

    > W = 0

    > H = 1

    > D = 1

    >

    > Then use the Loops to save the data example to save the coins:

    >

    > -set Array Size

    >

    > W = 0

    > H = 1

    > D = 1

    >

    > -Then For Each Coin: Array Coins Push Front Coin.AsJSON

    >

    > Note: The Array Sizes you set them Once only and outside the Loops, probably at the Beginning of the Function on Event 78, set all your Array Sizes there

    >

    >

    > =--------------------------

    >

    > Now if you Did what I Wrote above you should have each Array as 1D = 1 Dimensional Array

    >

    > so you need to change the Events On Function "BuildLevel" Especificly the Loops that you use on the Array

    >

    > -Instead of using Array For Each XYZ you need to replace it by Array For Each X Element

    Just saw what you said about pushing the value in the front of the array instead of inserting one. I also moved the events where the master array is setting its values to the other arrays as json into its own function called "compileArrays". It's getting me a little closer, but now when I load up the project, the scale is off and some of the objects are mismatched.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Just saw what you said about pushing the value in the front of the array instead of inserting one. I also moved the events where the master array is setting its values to the other arrays as json into its own function called "compileArrays". It's getting me a little closer, but now when I load up the project, the scale is off and some of the objects are mismatched.

    Hard to know without seeing your whole Project see if this can help if you do everything the same as this example it should work if it doesn't work it must be somewhere on your project interfering with your Events

    https://www.dropbox.com/s/g8f4rni4bcul9sx/Save%20%26%20Load%20%20Custome%20Levels.capx?dl=0

    Apart from that, a few Tips that is nothing to do with your Last Problem is just to let you know:

    -When you set Array size (W = 0) you don't need to clear the Array Before as it becomes Empty anyway

    -When you try to Load an Array from Json you don't need to (Clear or Set Size) before that as it already loads the correct size from the JSON File

    -You can use Families also

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