I've been having some trouble with the Level-Save Object. I am trying to make a level editor for a game that I'm working on, and so far it has been going well except for a strange crash that keeps happening.
What I want to do with the editor is have it be able to save and load levels that I make, and then also save the changes I make to a level after I have loaded it.
So far it works perfectly saving a level for the first time, and loading it. But when it comes to loading a previously saved file, altering it in a specific way, saving and loading it again, and then trying to save it once more, it crashes the editor.
I have done some tests to determine what exactly seems to cause the crashes, and it seems to have to do with what order I delete and add objects in the level that I edit.
For example, if I make a new level and place three tiles, and save, it works fine. When I load it, it loads properly and works fine. If I add something to it and save and load it again, it works fine. If I delete the last item (the most recently created item) that I created, save and load, it seems to work fine. However, If I load up a level that I have saved previously, and delete the first item that I created (out of 3, for example) and then save and load, it will crash if I try to save it again. It will also do this if I delete an item which was not the first or the last item that I placed (the second object that I placed, assuming I made 3 objects total).
A small workaround is to create a new object for every object that I deleted before saving again, but this eventually causes it to mess up when loading, and some objects start to not load, or get loaded in the wrong place, etc. Obviously this isn't a very desirable workaround. I have tried changing how the editor handles saving and loading, but nothing I have done has really fixed the problem.
It looks as though the specific point that causes the crash has to do with the Output String of the Level-Save object.
I added an event where when I press a key, an edit box will set its text to the output string of the Level-Save object. When I press this key under normal circumstances, the edit box displays the level save data which would be in the file that the Level-Save object saves its data to. But when I press the key after modifying a previously saved level in a way that will cause it to crash if I save it again, it will crash when it tried to display the output string in the edit box.
Here is the copy/pasted output for a level where I do the steps which cause the crash.
(The id|0|, id|1| etc are just something I was trying to do to stop the crashing which didn't work.)
Output after removing the object that was created first:
For the second output, I retrieved it by pressing the key to make the edit box display the output string BEFORE I saved the changes. Once I saved it and loaded it, pressing the key to make it display the second output caused it to crash.
The editor works like this as far as how I have it save/load objects.
Placing an object:
User clicks with left button --->
*Create flatgrass_paste at 0,0 from the cursor object
*Add 1 to id_counter
*Level-Save object Add object of type "flatgrass" from flatgrass_paste, Set value property "id" to value ("id_counter") for object "flatgrass_paste"
Saving the level:
Button save clicked --->
*Powerful Dialogs object Show save file dialog using window handle MainHwnd("powerful dialogs object")
On save file dialog 1 success --->
*Level-Save object Save level to RetString1$("Powerful Dialogs object")
Loading the level:
On Open File Dialog 1 Success --->
*Start loop "load" TotalObjs("Level-Save object") times
*(sets the frame width and height from the save data)
*loopnumber set counter to Loopindex("load")
*Level-Save object Load Level from RetString1$("Powerful Dialogs object")
*Edit Box settext RetString1$("Powerful Dialogs object")
Value of loopnumber_count = value of loopnumber --->
*deactivate group load, activate alot of other groups disabled during loading, and stop loop "load"
*loopnumer and loopnumber_count set to 0
*Level-Save object clear all stored data
On loop "load", ObjType$( "Level-Save Object", ObjAt( "Level-Save Object", LoopIndex("load"))) = "flatgrass" --->
*create flatgrass_paste at 0,0 layer 1
*flatgrass_paste set x postion to ObjX( "Level-Save Object", ObjAt( "Level-Save Object", LoopIndex("load")))
*same sort of thing for y position
*add 1 to loopnumber_count
Deleting an object(I've tried various methods, this is the one I am currently using):
User clicks with middle mouse button on Group.97(all tile objects are part of this group)--->
*Level-Save object set Value Property "id" to -1 for object Group.97
*Level-Save object Remove object Group.97 (Destroy = 0)
There are of course many more events than that in the editor, but those are the basic ones for the problem I am having.
I am probably doing something wrong, but I can't tell. If anyone has encountered this problem before or knows what I need to do to fix it if anything, I would greatly appreciate it.