How to write and read multiple Floats / integers from 1 single file.
I am creating a new level editor for my Platformer and wish to save / load levels.
I am having trouble with writing / reading multiple lines of code.
This is my old BreakOut level editor.
// Project: BreakOut Level Editor
// Created: 2015-09-27
//Set Display Variables
SetDisplayAspect(-1)
DeviceWidth# = GetDeviceWidth()
DeviceHeight# = GetDeviceHeight()
W# = 100 * GetDeviceWidth()/DeviceWidth#
H# = 100 * GetDeviceHeight()/DeviceHeight#
//Load up to 100 sprites....If you wish to change this you will have to Increase Sprites 101 number (Sprite that has view offset so we can move around the level)
Global Dim Sprite[1000]
Global ShowText = 0
Global PointerImage
Global ID = 1
//Define IDs of Sprites being placed on screen (Props, Platforms, Monsters)
//Platform ID = SID, PropID = PID, Monster ID = FID
Global SID = 1
//Load your images here....make sure you number them like i have. (Start at 1 and work your way up to How ever many you wish.)
LoadImage (2,"bricks/1.png")
LoadImage (3,"bricks/2.png")
LoadImage (4,"bricks/3.png")
LoadImage (5,"bricks/4.png")
LoadImage (6,"bricks/5.png")
LoadImage (7,"bricks/6.png")
LoadImage (8,"bricks/7.png")
LoadImage (9,"bricks/8.png")
LoadImage (10,"bricks/9.png")
LoadImage (11,"bricks/10.png")
LoadImage (12,"bricks/11.png")
LoadImage (13,"bricks/12.png")
LoadImage (14,"bricks/13.png")
LoadImage (15,"bricks/14.png")
LoadImage (16,"bricks/15.png")
LoadImage (17,"bricks/16.png")
LoadImage (18,"bricks/17.png")
LoadImage (19,"bricks/18.png")
//Load the starting Pointer Image
PointerImage = CreateSprite(0)
SetSpriteSize (PointerImage,4,-1)
//Get Map$ Name
MapName = 0
If MapName = 0
StartTextInput ("Level Name.txt")
endif
do
//Complete Text Input to get Map$ name
If MapName = 0 and GetTextInputCompleted() = 1
Map$ = GetTextInput()
MapName = 10
endif
Print("Brick ID:" + str (SID))
//Change Image ID +1 //Set New Pointer IMG //Delete Pre-Exisiting Pointer IMG
If GetPointerPressed()
DeleteSprite (PointerImage)
ID = ID + 1
PointerImage = CreateSPrite(ID)
SetSpriteSize (PointerImage,4,-1)
SetSpritePosition(PointerImage,X#,Y#)
endif
If ID = 20
ID = 1
PointerImage = CreateSprite(0)
SetSpriteSize (PointerImage,4,-1)
SetSpritePosition(PointerImage,X#,Y#)
endif
X# = GetSpriteX(PointerImage)
Y# = GetSpriteY(PointerImage)
PointerImageWidth# = GetSpriteWidth(PointerImage)
PointerImageHeight# = GetSpriteHeight(PointerImage)
If GetRawKeypressed(39) = 1
SetSpritePosition(PointerImage,X# + PointerImageWidth#,Y#)
endif
If GetRawKeypressed(37) = 1
SetSpritePosition(PointerImage,X# - PointerImageWidth# ,Y#)
endif
If GetRawKeypressed(38) = 1
SetSpritePosition(PointerImage,X#,Y# - PointerImageHeight# )
endif
If GetRawKeypressed(40) = 1
SetSpritePosition(PointerImage,X#,Y# + PointerImageHeight# )
endif
If GetrawkeyPressed(16)=1
Sprite[SID] = CreateSprite(ID)
SetSpriteSize(Sprite[SID],4,-1)
SetSpritePosition (Sprite[SID],X#,Y#)
// Increase Sprite ID to create next sprite
SID = SID +1
endif
//Undo Delete SPrites With the Z Key///WHOOOOOOOOOPS FEATURE
If GetRawKeyPressed(90) = 1
SID = SID - 1
DeleteSPrite(Sprite[SID])
endif
If GetRawKeyPressed(112) = 1//f12
// Num = 1 to SID - 1 (-1 is to make sure you dont print a blank sprite position because of the Curser sprite that has not yet been place)
For Num = 1 to SID -1
OpenToWrite (Num,Map$,Num)
WriteLine (Num, "Brick["+str (Num) + "]" + " = CreateSprite( " + str (GetSpriteImageID(Sprite[Num]))+" )"+ " : " + "SetSpritePosition(Brick[" + Str (Num)+"]" + "," + str (GetSpriteX(Sprite[Num])) +"," + str (GetSPriteY(Sprite[Num])) + ")" + " : SetSpriteSize (Brick[" +str(num)+"]" +",W#/22,H#/22)")
closefile(Num)
Next Num
endif
If GetRawKeyPressed(82) = 1
SetSpritePosition(PointerImage,0,0)
endif
Print(GetRawLastKey())
Print ("Pointer Image ID: " + str (ID) )
Print( ScreenFPS() )
Sync()
loop
As you will see the way I am saving it is writing out generated lines of code just to copy and past into AGK.
What I am trying to do is grab each sprites Xpos / Ypos and write them as floats into a file.
which I would assume would be.
If savekey = pressed
SaveFile = OpenToWrite("Save.txt",0)
for num = 1 to ID#
WriteInteger(SaveFile,ID#)
WriteFloat (SaveFile,GetSpriteX(Sprite[Num]))
WriteFloat (SaveFile,GetSpriteY(Sprite[Num]))
Next num
CloseFile (SaveFile)
Then when load key is pressed
SaveFile = OpenToRead("Save.txt")
ID# = ReadInteger(SaveFile)
XPos# = ReadFloat(SaveFile)
YPos# = ReadFloat(SaveFile)
For Num = 1 to ID#
Sprite[Num] = CreateSprite(0)
SetSpriteSize(Sprite[Num],SizeX#,SizeY#)
SetSpritePosition(Sprite[Num],Xpos#,YPos#)
Next Num
I have been going over this for awhile now, searched the fourms and found
http://davidjohnwheeler.blogspot.co.uk/2015/04/level-editor.html but just cant make and since of it.
Any help would be much appreciated. Thanks in advance!!!
Sign up for NaGaCreMo!