ID:804243
 
(See the best response by DarkCampainger.)
Code:
world
New()
..()
world.log = file("Debug.txt")
spawn(200)
AutoRepop()
XD
spawn(200)
for(var/mob/Enemy/M in world)
if(M.AItime > 0)
M.AItime -= 20
spawn(200)
for(var/mob/player/M in world)
if(M.key)
M.Save()
goto XD
if(BerryC == 1)
return



mob
proc
Save()
if(src.CANSAVE == 1)
return
fdel("savefile/[src.ckey].sav") // deletes the old save file
var/savefile/F=new("savefile/[src.ckey].sav") // creates a new save file
src.Write(F) // writes your variables/lists onto it
F["lastx"] << src.x //saves your x coord
F["lasty"] << src.y //saves your y coord
F["lastz"] << src.z //saves your z coord
Load()
if(fexists("savefile/[src.ckey].sav"))//if an existing save file exists in the folder located in the game folder
var/savefile/F=new("savefile/[src.ckey].sav") // it creates a new save file
src.Read(F) //it reads it
var/newX
var/newY
var/newZ
F["lastx"] >> newX//it takes the lastx variable in the save and puts it into the newx variable
F["lasty"] >> newY//same as above with a new variable
F["lastz"] >> newZ//same as above with a new variable
src.loc=locate(newX,newY,newZ)//makes the player located in those locations
src.rest = 0
new/obj/hudMeters/hpbar(src.client)
src.updateHealth()
if(src.Spatt1 >= 1)
src.Spatt = src.Spatt1
src.Spatt1 = 0
if(src.Spdef1 >= 1)
src.Spdef = src.Spdef1
src.Spdef1 = 0


Problem description: I have no idea why but at random times the save files will be corrupted, whenever load is pressed it takes you to a black area and you can't do anything there, I'm not sure what's wrong, any help would be appreciated ^^;

And yes I know I'm lame for using goto, idk no better lol

I am new to BYOND development, but I have a little bit of experience with programming. I'm not sure about your save problem, but I might be able to assist with your goto "problem." (It might work fine, but it's generally considered sloppy coding.)

Would this still give the desired result?

        while(1) // instead of XD
spawn(200)
for(var/mob/Enemy/M in world)
if(M.AItime > 0)
M.AItime -= 20
spawn(200)
for(var/mob/player/M in world)
if(M.key)
M.Save()
// You no longer need "goto XD"
if(BerryC == 1)
return


If I understand BYOND indents properly, this should loop what used to be inside the goto statement indefinitely (I think that's what you meant to do?), as 1 will always have a value of true. If the indents don't work like that with while loops (I've only tried for loops so far), I think encasing it in a block (uses braces { } around everything in the loop) will still work.

If you did want to loop it definitely, then you might want to move the BerryC check thing you have up a bit, otherwise it'll never be reached (as the loop will go on forever). I'm not sure what it's purpose is though from just this bit of code. I looked at it from purely trying to translate your code from a goto statement to a loop - so be warned if the actual implementation isn't what you wanted. You might have to edit a little bit.

If someone else comes along and corrects me then take their word; they probably have more experience in BYOND than I.
Well I'm not sure if it helps figuring it out but opening the corrupted save file and the normal one I find out that the corrupted one has only 15% of the text the normal one has, so alot of stuff is missing from it I guess.
In response to MilleniumMage
Best response
DM is indentation-based, so you shouldn't have to use braces unless you want to compress some code to a single line. Your loop is structured correctly, but there is one vital difference between your code and his: your loop has no delay in it and will freeze/crash the game. This is because of the way the spawn() process works: the delay is applied only to the code indented under it.

Instead, you could restructure it to use sleep() relatively easily:
world
New()
..()
world.log = file("Debug.txt")
spawn(200)
AutoRepop()
spawn()
while(1)
sleep(200)

for(var/mob/Enemy/M in world)
if(M.AItime > 0)
M.AItime -= 20

for(var/mob/player/M in world)
if(M.key)
M.Save()

if(BerryC == 1)
return



As to the savefile problem, a lot of people have been saying recently that you shouldn't call Read()/Write() directly, but instead use the output operators (<<). I don't know if there's any truth to that (I don't tend to work with savefiles much), but it might be worth trying.