Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Level Editor - Need Help - Saving / Loading

Author
Message
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 04:56
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.



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!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 4th Jan 2016 09:34
Change ID# to just ID (the # would indicate it is a float, but you are using it as an int).

In your save section: WriteInteger(SaveFile,ID#) is going to write the same number (ID#) over and over again at each iteration of your FOR loop. You only need to write it once.

If savekey = pressed
SaveFile = OpenToWrite("Save.txt",0)
WriteInteger(SaveFile,ID)
for num = 1 to ID
WriteFloat (SaveFile,GetSpriteX(Sprite[Num]))
WriteFloat (SaveFile,GetSpriteY(Sprite[Num]))
Next num
CloseFile (SaveFile)


SaveFile = OpenToRead("Save.txt")
ID = ReadInteger(SaveFile)
For Num = 1 to ID
XPos# = ReadFloat(SaveFile)
YPos# = ReadFloat(SaveFile)
Sprite[Num] = CreateSprite(0)
SetSpriteSize(Sprite[Num],SizeX#,SizeY#)
SetSpritePosition(Sprite[Num],Xpos#,YPos#)
Next Num

Alternatively, you could drop saving the ID to the file altogether, and just read the x# and y# floats until FileEOF is true.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 4th Jan 2016 10:30 Edited at: 4th Jan 2016 11:37
You might find this module useful. It allows you to get and set parameters by name.

If you continue with your current method, then I recommend that you make the first parameter the file version. When you need to change it later you can use the version number to read the parameters correctly in your code, and to write them back out with an updated version number.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 18:53 Edited at: 4th Jan 2016 19:17
The problem I am having is reading the multiple integers and possibly writing them. I know how to write multiple lines. I may be writing multiple floats I may not be. The way I am reading it does not read everything. It only reads one value.


so lets say I have 3 sprites.

with the 3 sprite locations.

SriteNum = 3
Sprite 1.
X# = 39
Y# = 31

Sprite2
X# = 68
Y# = 23

Sprite 3
X# = 30
Y# = 89

I want to save all the locations to a file then read them in the future to create the level.

If Save Key is pressed......
OpenToWrite(MyFile,"Save.Txt",0)
For Num = 1 to SriteNum
WriteInteger (MyFile,SriteNum)
WriteFloat (MyFile,GetSpriteX(Sprite[Num]))
WriteFloat (MyFile,GetSpriteY(Sprite[Num]))
CloseFile(MyFile)
Next Num
Endif

If Load Key is Pressed.....

This is where I think I need help. Possibly writing too.

When trying to read the floats as showed in the first post. It is only returning the first (or last idk which.) x / y values. It is not reading them all.

From reading the help docs it should read the file as.

X# = 39
Y# = 31
X# = 68
Y# = 23
X# = 30
Y# = 89

And store the values / create the sprites and repeat until each value has been read.

I'm sorry for the lack of being able to describe the situation any better. I know it sounds confusing atleast to me it does.

Thanks for any advice in advance. I really appreciate all the help.


Code sample that you can run with no media that isn't working.
I have butchered it so many times I don't know which way is up or down any more

Click the mouse to create sprites any where on the screen at the mouse x/y position.
Hit F1 to save the sprites locations / F2 to Load the sprites locations.

Sign up for NaGaCreMo!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 4th Jan 2016 19:45
Did you see my previous post?

Try this:

V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 20:05 Edited at: 4th Jan 2016 20:07
Thanks very much CJB. ID# was declared an Integer at the top of the code source. I figured it wouldn't matter.
The part I'm guessing that does matter is having the Integer written / read before the for loop.
Something so simple can cause the biggest head ach.

Thanks again!!!

Sign up for NaGaCreMo!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 4th Jan 2016 20:10
Quote: "OpenToWrite(MyFile,"Save.Txt",0)
For Num = 1 to SriteNum
WriteInteger (MyFile,SriteNum)
WriteFloat (MyFile,GetSpriteX(Sprite[Num]))
WriteFloat (MyFile,GetSpriteY(Sprite[Num]))
CloseFile(MyFile)
Next Num
Endif"


You have closed the file after writing the details of the first sprite.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 20:18 Edited at: 4th Jan 2016 20:19
Quote: "You have closed the file after writing the details of the first sprite."



Thanks guys very much. I am just now having the time to go over your tutorial BatVink, as it will be needed when the editor gets more advanced.

Sign up for NaGaCreMo!
Yodaman Jer
User Banned
Posted: 4th Jan 2016 21:02
One thing of note.

I noticed that you are using three different arrays to store tile information. Is there a specific reason you are doing it this way?

The way I am using is one that good ol' Clonkex taught me a while ago, and it's pretty good. It keeps things a lot neater, and makes adding more information in a lot easier!

First, you'll want to declare a UDT (user-defined type)



Next, declare a two-dimensional array, which will making storing the tile positions much easier as well!



roomWidth and roomHeight are constants, so always determine the size of the level you want!

In your main loop, you'll want to have some sort of grid-snapping function. In this example, which I am copy>pasting from my current project, you will see it in use, and I will do my best to explain them here in a moment!


So, how this works is that when the player clicks the mouse, it will determine the cursor's position and snap it to the nearest point of the grid. Then, it will check if that place you just clicked is "in use", which I have as a tag in my UDT. If it is 0, I know there is no tile there, so we set the flag to 1 (true), and create the sprite/tile, then position it using the mouse position which is fed into the GridSnap() function. To check that the cursor is actually inside of the placeable grid, we just use this line:



And that's it! Pretty dang easy, and this is where it really gets advantageous!

When saving a file...



And loading the file, you just replace whatever may be in your map array already!



And wah-lah! It may take a bit of work to get it implemented, but if you use this technique it will make things so much easier!

Just my $0.02

Sign up for NaGaCreMo!
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 21:36 Edited at: 5th Jan 2016 01:22
Thanks yoda, I just have not got around to this. But as you will see in a few minsI will post you the complete demo I have made after discovering my dumbfounded mistakes. You will notice also in that code you see above. Only 1 array is actually being used. Sprite[]



Revised Code / commented.



All this does is position the sprites on the screen 4x4 sprites. Saves the Position of each sprite / ImageID. Then Load them to a blank level.


Move the tiles using the arrow keys. 1 grid at a time.
Z key = Undo last placed sprite.
Left Shift Key = Create Tile Sprite Under Display Image Sprite.
F1 = Save Level.
F2 = Load Level.
Space = Change Display Image ID.

I may have forgot to mention something. If I did, I know I commented all the actions.

Project Files Attached.


Some Images may not be sized correctly to move along the grid. These Images are 13,14,15. the rest will work properly.

Sign up for NaGaCreMo!

Attachments

Login to view attachments
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 4th Jan 2016 22:22
Excellent progress being made there Crazy Programmer! Can't wait to see the finished game
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Jan 2016 22:50 Edited at: 5th Jan 2016 01:23
Thanks CJB your encouragement is truly appreciated.


Edit: Heres a sample level I just made. Saves and loads.

Sign up for NaGaCreMo!
SpecTre
Developer
21
Years of Service
User Offline
Joined: 24th Feb 2003
Location: UK
Posted: 5th Jan 2016 02:14
Just got back from work, first day back after xmas leave. Got AppGameKit website withdrawal I think! Seems like you have this sorted now anyway.
The level is looking good, keep up the good work, you will soon have your NaGaCreMo done! I think I will have to retract my submission as I will have no chance by the looks of it to get anywhere started on it with work now!!!

Not sure if it is just my web browser but I can't seem to open any of the code snippets hmmm

The Amiga and Amos were great!
My website LEAP - Download Paint Pot here!
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 5th Jan 2016 02:39
Quote: "soon have your NaGaCreMo done!"
,
Well my goal isn't to complete a game in 30 days any more. I'm going to take baby steps. I'm in no rush like I was last time. I want things done right

NaGaCreMo has done its job....Motivated me to start a project


Sign up for NaGaCreMo!
Yodaman Jer
User Banned
Posted: 5th Jan 2016 05:40
Quote: "Not sure if it is just my web browser but I can't seem to open any of the code snippets hmmm "


Ah, so I'm not the only one! It seems to be that if you hit a certain amount of code snippets in a page, they will just refuse to load, so I'm going to report a bug in the bug reports thread.

Crazy Programmer, this looks great! Very glad to see your hard work come to fruition

Sign up for NaGaCreMo!

Login to post a reply

Server time is: 2024-09-29 09:24:41
Your offset time is: 2024-09-29 09:24:41