[CLOSED] Is it possible to further optimise my loading system events?

My game uses a saving and loading system that saves levels created in the level editor and loads them to be played.

I implemented an undo and redo system that works by using the save function to save current progress and loads the save code when you want to undo/redo.

The problem is, the loading is so slow on large levels, 50 or more blocks, to the point you need to wait seconds for it to undo.

I tried optimizing it but honestly, I have no idea on how I could optimize it further.

Events of loading system:

Explained:
Splits save code by /. Which is a separator for each block.
Each item in the array is a block and its properties. It creates them and modifies them depending on the block name and assigned properties.

1 Like

The string manipulation can’t be helping… all of this data could be already stored in arrays and structures while the game is running. When it’s time to save to file, then convert the data with ToJSON().

Anyway, loading the entire game state for an undo is not the most efficient way to go about it. It would be better to only save what changed on each step. This is how programs can save a large number of steps… each one is a small piece of data that is used to reverse only the specific action taken, and since they are already in order you can step back through them to get to an earlier state.

It is definitely more challenging to set up, depending on the variety of actions that you need to save. For example if the user deleted a block, you might only need three data points, one to mean “delete” and two coordinates. But placing a block with other properties such as color, type, will need additional fields for saving those properties.

Basically you would want to either come up with a standardized way to save all the necessary data for any action, or have a “parser” that reads what type of action it is and then interacts with the array based on the type.

1 Like

What?? I didn’t understand. What do you mean?

Yeah, I’ll consider changing this system to what you suggested. However, I want to optimize this system because it’s also used in other places in the game.

I mean that splitting strings is expensive and should be avoided unless you have a specific reason for it. In the snippet you posted, you are converting a string into an array, when you could have just stored the data as an array to begin with, removing the need for conversion.

I wasn’t suggesting that you change the saving and loading system… the undo function would be a separate set of events. If you wanted to save the undo history so that you can close and re-open the editor and still undo, you would simply save the array that contains the undo history.

EDIT: Some more points about efficiency and/or best practices…

  • For some reason you are disabling the smooth camera behavior after EACH block. I assume this should be above the “for every child” event.
  • It isn’t necessary to use “rotate blocks toward” action here. Just set the angle directly.
  • It shouldn’t be necessary to convert the X/Y coordinates to a number. Save them as a number and read them as a number.
  • Avoid using conditions whenever possible. For example you have “If var is TRUE, change FINISH to TRUE…if var is FALSE, change FINISH to FALSE”. This should just be “Change FINISH to Array[3]”.
  • Similarly, instead of checking for “TrackConf” or “ColorBlock”, it would be better for performance to just have positional properties. In other words, Laps is always in position 2, Acceleration is always in position 4, and so on.

I would keep in mind that saving and loading is usually expected to take a little bit of time and that freezing the game during that time is generally acceptable as long as it doesn’t take absurdly long to complete. But, it is nice for an editor to be able to do it fast so that you can autosave without being annoying.

Also, it looks like you only took a screenshot of the first part of the loading events, so if you want to share more of it I’d be happy to look it over.

I cant, the save code is a string that i need to convert to array.

Thanks for the helpful list but some points can’t be applied because i don’t think you understand how the loading works.

This is the complete loading function or also called external event. Nothing is cut off. This is the full loading system. I could provide the external events for generating the save code

How can i do that? I used to do this before but the new update changed the way these work

What they are saying is that the problem is you are using a string to begin with. Your data should be structured as an array already in your variables instead.

Got it… for some reason I looked at the picture and assumed there were a bunch more events coming.

The one other way I can think of to optimize this would be to use object pooling. Creating new blocks takes more processing than modifying an existing one, so what you could do is create a sufficient number of blocks at the beginning of the game (the “pool”) and then instead of creating you just take one out of the pool and set its properties.

Ah, I guess you can’t set a boolean with an expression… I didn’t know that! In any case, you’re not doing this enough times to matter, I was just mentioning it as a general principle. For this instance I think that string splitting and object creation are the two biggest factors.

Thanks for the responses.


I didn’t understand. What should I do with the save code?

How can I do that? The blocks are in an object group.


Below are 2 GIFs showing how I use this loading system:
untitled-ezgif.com-optimize
untitled-ezgif.com-speed

The text copied from the level editor is used to load the level.

JSONify it, as others have suggested. Here’s a link on how to save your array or structure to storage, and retrieve it.

Thanks @magicsofa @Silver-Streak @MrMen for the effort to help!

But I can’t seem to figure out how to do this jsonify thing with a save code string, or whatever you are trying to tell me to do.

I’m giving up, this topic has gone on for too long, the community is trying to help me, but I’m not sure what they’re talking about.

Perhaps later, when I need more optimization and I don’t know how to, I will ask again.
Thanks.


This link isn’t useful for the current problem, but it may be useful for the results feature I implemented.


Should I mark this topic as closed or solved, or do nothing?

@v9um It’s ok if you don’t get something right away… it’s called learning :stuck_out_tongue:

The thing is, you are already working with an array. You are properly testing values (such as condition SaveLoadArrayPos[0] = “ColorBlock”) as well as transferring them to an object (set Laps of Blocks = SaveLoadArrayPos[2]).

So, to save your data, you can just do the opposite - write the values into the array. It will be a two-dimensional array, so for example the first property of the first block will be at Array[0][0]. The second property will be Array[0][1]. The second block will be Array[1][0], Array[1][1], and so on.

You don’t need to convert any of this to a string until player hits the share button. When they do, you can either use your method to encode it, or just use the expression ToJSON(Array) which returns a JSON string containing the array data.

1 Like

I wasted so much time working on trying to convert my system to work with arrays,
I always feel that I’m near success, but I’m welcomed with a new bug.
The most working I was able to get it was by it only doing one undo. only one.
This time could have been instead used for new features.

I will quit and return to the last save of my game and instead try to do the idea below suggested by magicsofa.

Anyway, Thanks for the help.