?Topic on Save?

Previous topic - Next topic

Ian Price

Nothing is saved at the beginning of those games.

Do you mean the level is loaded at the start and once it's beaten you move onto the next?

What do you think is saved at the beginning of the level in Sonic & Mario?

Are you just needing some form of level editor to create, save and load levels (which can be used in-game too)? If so, my example a very basic loading/saving of the level.
I came. I saw. I played.

Slydog

#16
I think what you want is to just save the player's progress through your game.
If the player just finished Level 6, you want to save that progress info to a file.
Then the next time the player plays the game, you start him / her from that level.

For that, just save the highest level the player reached to a file.
Each time he finished a new level, update the save file.
When the game starts, read that progress file to know which level to start the player.

You need to know how to read and write to a file for this.
Choose a file name such as "player_level.txt" or something meaningful.

If you want to see some file reading and writing code, take a look at my FILE library (below the following example).
To use it for your problem, try something like:

[EDIT] This doesn't allow for skipping levels, and knowing which levels where done, and which were skipped.

Code (glbasic) Select
LOCAL playerLevel AS TFile
LOCAL levelAchieved% = 0
playerLevel.name$ = "player_level.txt"

// Read Player Level Progress
FUNCTION PlayerLevelRead:
IF playerLevel.Get()
levelAchieved = playerLevel.data$ // Converts data$ string into a number
ELSE
levelAchieved = 0
LOG("*** Error Getting PlayerLevel! ***")
ENDIF
ENDFUNCTION

// Write Player Level Progress
FUNCTION PlayerLevelWrite:
playerLevel.data$ = levelAchieved
playerLevel.Write()
ENDFUNCTION

// To read the player's progress (such as at the start of the game):
// ('levelAchieved' will now be which level to start with.)
PlayerLevelRead()

// To write new player's progress (such as when he finished a new level):
// (make sure 'levelAchieved' already has the new level number)
PlayerLevelWrite()



My TFile Library code:

Code (glbasic) Select
//---------------------------------------------------------------------- F i l e
CONSTANT FILE_MODE_APPEND = -1
CONSTANT FILE_MODE_WRITE = 0
CONSTANT FILE_MODE_READ = 1

CONSTANT EOF$ = "<EOF>"

TYPE TFile
fh% // File Handle
name$ // File Name
data$ // File Contents
eof% // File is at end?

// Reads entire file into 'self.data$'.  Closes file when finished.
// 'name$' > Optional.  Will use previously set 'self.name$' if blank.
// Returns > [TRUE if succeed | FALSE if fail]
FUNCTION Get%: name$=""
IF name$ <> "" THEN self.name$ = name$
self.data$ = ""
LOCAL line$ = ""
IF self.Open(FILE_MODE_READ)
WHILE ENDOFFILE(self.fh) = FALSE
READLINE self.fh, line$
INC self.data$, line$
WEND
CLOSEFILE self.fh
ELSE
LOG(">FIL:{TFile.Get$      } *** Error Getting File: [" + self.name$ + "] ***")
RETURN FALSE
ENDIF
self.Close()
RETURN TRUE
ENDFUNCTION

// Reads the next line in an already opened file.  Closes file when end is reached.
// Returns > [line data if succeed | 'EOF$' if fail]
FUNCTION GetLine$:
LOCAL line$
IF ENDOFFILE(self.fh)
line$ = EOF$
self.eof = TRUE
self.Close()
ELSE
self.eof = FALSE
READLINE self.fh, line$
ENDIF
RETURN line$
ENDFUNCTION

// Writes 'self.data$' to file
// 'name$' > Optional.  Will use previously set 'self.name$' if blank.
// Returns > [TRUE if succeed | FALSE if fail]
FUNCTION Write%: name$=""
IF name$ <> "" THEN self.name$ = name$
IF self.Open(FILE_MODE_WRITE)
WRITESTR self.fh, self.data$
ELSE
LOG(">FIL:{TFile.Write$    } *** Error Writting File: [" + self.name$ + "] ***")
RETURN FALSE
ENDIF
self.Close()
RETURN TRUE
ENDFUNCTION

// Appends string to end of file.
// 'extra$'> Data to append
// 'name$' > Optional.  Will use previously set 'self.name$' if blank.
// Returns > [TRUE if succeed | FALSE if fail]
FUNCTION Append%: extra$, name$=""
IF name$ <> "" THEN self.name$ = name$
IF self.Open(FILE_MODE_APPEND)
INC self.data$, extra$
WRITESTR self.fh, extra$
ELSE
LOG(">FIL:{TFile.Append$   } *** Error Appending File: [" + extra$ + "] ***")
RETURN FALSE
ENDIF
self.Close()
RETURN TRUE
ENDFUNCTION

// Opens file for procesing.
// 'mode%' > [FILE_MODE_APPEND | FILE_MODE_WRITE | FILE_MODE_READ]
// 'name$' > Optional.  Will use previously set 'self.name$' if blank.
// Returns > [TRUE if succeed | FALSE if fail]
FUNCTION Open%: mode%, name$=""
IF name$ <> "" THEN self.name$ = name$
self.fh = GENFILE()
self.eof = FALSE
IF OPENFILE(self.fh, self.name$, mode) = FALSE
LOG(">FIL:{TFile.Open      } *** Error Opening File: [" + self.name$ + "] ***")
self.eof = TRUE
RETURN FALSE
ENDIF
RETURN TRUE
ENDFUNCTION

// Closes file to further processing
FUNCTION Close%:
IF self.fh >= 0 THEN CLOSEFILE self.fh
self.fh = -1
self.eof = TRUE
ENDFUNCTION

// Checks if file exists
// Returns > [TRUE if exists | FALSE if doesn't]
FUNCTION Exists%:
RETURN DOESFILEEXIST(self.name$)
ENDFUNCTION

// Returns file name portion from a longer file path
// eg. For a 'self.name$' of [C:/Programs/GLBasic/glbasic.exe] will return [glbasic.exe]
// 'slash$' > Optional.  Folder Seperator.  Use if your path uses '\' instead. Uses '/' as default
FUNCTION GetName$: slash$="/"
LOCAL slash%
slash = REVINSTR(self.name$, slash$)
IF slash <=0 THEN RETURN self.name$
RETURN MID$(self.name$, slash+1)
ENDFUNCTION

ENDTYPE


FUNCTION LOG: message$, add_linefeed% = TRUE, show_time% = TRUE
STATIC datetime% = -1 // Log timer
LOCAL elapsed# // How much time has elapsed since program start (or manual reset)
IF datetime < 0 THEN datetime = GETTIMERALL() // Initialize log timer if not already set
IF message$ = "RESET" THEN datetime = GETTIMERALL() // Manually reset log timer
elapsed = (GETTIMERALL() - datetime) / 1000.0
?IFDEF WIN32
//?IFDEF GLB_DEBUG
//?ENDIF
IF show_time = TRUE THEN DEBUG "[" + FORMAT$(8, 3, elapsed) + "] "
DEBUG message$
IF add_linefeed THEN DEBUG "\n"
?ELSE
INLINE
STDOUT(message_Str);
ENDINLINE
?ENDIF
ENDFUNCTION
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Ian Price

That looks a lot like overkill to me!

If all he wants is to save the last level that the user reached then he could have used what was in my source code. If that's the case, he didn't even read through it. The "SCORE" variable would do exactly that (changed to LEVEL).  If he didn't read through something using only BASIC commands, then using inline commands is waaaaay OTT.
I came. I saw. I played.

JonVelop

#18
Yes, but I would just like when the player stops in Level 6 and the game is closed that can start when it starts again in level 6 again.

Villeicht also you can call it in the menu.

The Level Change:

----------------------------------------
IF Map.Tile (self.X + self.Width / 2, self.Y + self.Height / 2) = 2
IF Nexxt $ = "Map2.txt"
ELSEIF Nexxt $ = "Map2.map"
ELSE
Map.Init (Map.Nexxt $)
ENDIF
ENDIF
----------------------------------------

And if in the level should be able to play at the next start on.
How do I build it and how can I retrieve? (CONSTANT?)

Ian Price

Quotees, but I would just like when the player stops in Level 6 and the game is closed that can start when it starts again in level 6 again.

:/

My demo shows EXACTLY that. Did you actually play it? The only difference is that in my demo you click L to LOAD, rather than it starting automatically from where you left off.
I came. I saw. I played.

JonVelop

Sorry, your demo I've never played
but I want to learn the yes and not copy you can perhaps explain in your demo?

Please :)

spacefractal

test it out. we have explained, then you need to experimentere. This is way to go. The demo was made to show of how its can been done and change what it needs to suit your needs.

Its the best way to explain thing, that is simply a demo with a source code, which is much easier to explain thing. Now its up to you.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Ian Price

QuoteSorry, your demo I've never played
WTF!!!

SERIOUSLY?

What was the point in us trying to give you answers? How do you even know then that your question wasn't answered already?

:blink:  :blink:  :blink: :giveup:
I came. I saw. I played.

JonVelop

#23
(yes but I do not mean that. I'm stupid)

To Save-Demo:

The is really good.

My problem is but I have everything in multiple tabs in my project and not all on one side.

How to do that best?
I own the TAP: Player, Map, Menu.

Every time I try:

error: invalid initialization of reference of type 'DGNat&' from expression of type 'DGInt'