Posted: 10th Mar 2023 21:47
Can an array save the names of a player for the next game even if the game is shut down and re-started?
or should I use a 'open to write to text-file' instead?

This game would just be for a family who reside in the same house, and not for online players.
And I want to have 4 players for the game and a way to edit or delete the names to add new names.
And when they re-start the game later, I want them to be able to choose their name on the main screen,
so they can continue where they left off, and with the same score as they left the game last time.

I have a hard time finding a sample program the tells how to do it.
I have finished the game, and this is my one 'sticking point'

I would appreciate the help very much if anyone show a sample program?


But here's what I have so far:

+ Code Snippet
User_Controll:
`-----------------------------Enlist new PLAYER---------
  name1 as string
 Round_  as integer
 Score as integer  

setprintsize(20) 
Setvirtualresolution(640,480)
   
If GetEditBoxExists(1)=0
CreateEditBox(1)
SetEditBoxPosition(1,250,120)
SetEditBoxCursorColor(1,0,0,0)
endif
 
do
    
     name1 = GetEditBoxText(1)
    print("")
   print("")
     
    Print ("Welcome, New Player  ")
    print (" please type your name")
    print(" and press the 'return key' ") 
        Sync()
  
  if GetRawKeyPressed(13) //  'Return Key'
exit
 endif
   loop 

 Write_name1_round_Total:   //------------------------( also is a separate goto for saving anytime later )

 file = OpenToWrite ("player1.txt",0) // ------------or put ("player1.txt",1) if you want to append to the end of each line.
sleep(20)
Writestring(file, name1)
sleep(20)
WriteInteger ( file, round_ )
sleep(20)
Writeinteger (file, Total )
sleep(20)
CloseFile ( file )
sleep(20)
SetEditBoxvisible ( 1, 0 )
setVirtualResolution(1024,768)
   
 setprintsize(28)      
  Sync()       
    
return


It is just a 'one player game' and the person plays against the computer.
So, I just need to save the names and the scores for each name. Because when the game starts again it is a 'table game' and re-organizes itself for the next 'round'.
So, yes, that's all I need is to save up to 4 names so they could, whoever wants to play it, just tap on their name again to go to the 'next round'.... that he left off.
Or to start a new round,(Level)
So, I need to save 3 things. the 'player name' and his 'Round_..(level)' that he's on and his current Total for that level.
Posted: 11th Mar 2023 9:57
In my experience array info is lost when the program is terminated, you need to store the info into a file and load it back into the array when the program is started.
Posted: 11th Mar 2023 14:14
by file, you mean text file?
you need to store the info into a file
Posted: 11th Mar 2023 19:01
Yeah a text file, it can contain numerical, string or char codes and doesn't specifically need to have the .txt extention. I normally use the extention .dat for storing stuff such as level data.

I've attached a file example, ignore what I called it (ABC), I just chose a quick and dirty file name for the project file.
Posted: 11th Mar 2023 19:09
I forgot to put the save bit in the code but there is a file already included, that's when the attachment finally appears. I do need to menstion that when writing to a file you will need the entire path and include "raw:" at the beginning of it. Something like "raw:C:\MyProjectFolder\media\MyData.dat"
Posted: 11th Mar 2023 19:14
If you don't include raw: and the complete path the data will be saved to (for example) " C:\Users\UserName\AppData\Local\AGKApps\MyProjectFolder\media"
Posted: 11th Mar 2023 19:29
The zip file I attached didn't show so here's a code example instead.

+ Code Snippet
path as string
file as integer
data as string[]

data.insert("Data")
data.insert("More Data")
data.insert("Even More Data")
data.insert("And Even More Data")

path = "raw:" + GetReadPath() + "media\" + "TestFile.lvl"
file = OpenToWrite(path)

for i = 0 to data.length
	WriteLine(file, data[i])
	
next i

CloseFile(file)


file = OpenToRead("TestFile.lvl")

while not FileEOF(file)
	data.insert(ReadLine(file))
	
endwhile

CloseFile(file)

do
	for i = 0 to data.length
		print(data[i])
		
	next i
	
   Sync()
    
loop

end
Posted: 11th Mar 2023 21:43
Thanks Steve Ancell,
That's really great, I think I'll try your version,
In makes more sense than my version.
and the file location part is very helpful too

I'm sure it will save me a lot of 'digging' and 'trial and error'.
I see some things in your coding that I overlooked already, I appreciate it.

.

Anybody else have something to contribute? if so, feel free to add a post.
I need all the help I can get in this area. Please, Thank you.
Posted: 12th Mar 2023 8:47
A couple of functions I rattled out might come in handy if you ever need a situation when you want to detect when any key is pressed or released, like transitioning on at the end of levels and gameover and cutscenes, etc...

+ Code Snippet
function AnyKeyPressed()
	local flag as integer
	
	if GetRawKeyPressed(GetRawLastKey())
		flag = 1
		
	else
		flag = 0
		
	endif
	
endfunction flag


function AnyKeyReleased()
	local flag as integer
	
	if GetRawKeyReleased(GetRawLastKey())
		flag = 1
		
	else
		flag = 0
		
	endif
	
endfunction flag


Typical usage examle:
+ Code Snippet
while AnyKeyPressed() = 0
	sync()
	
endwhile

do
	if AnyKeyReleased() = 1
		exit
		
	endif
	
	sync()
	
loop
Posted: 12th Mar 2023 13:33
If you ever get stuck feel free to PM me so I don't wreck this thread by going too far off topic, you'll be surprised what I can sometimes come up with considering I'm not professional. Most of it is just common sense.
Posted: 13th Mar 2023 1:25
Array.Save( Filename$ )
Array.Load( Filename$ )

Are the easiest approach...

+ Code Snippet
Type Vec3
	X As Float
	Y As Float
	Z As Float
EndType

Function NewVec3( X As Float, Y As Float, Z As Float )
	Local Output As Vec3
	
	Output.X = X
	Output.Y = Y
	Output.Z = Z
	
EndFunction Output

Type PlayerInfo
	Position As Vec3
	Rotation As Vec3
	Handle As String
	Lives As Integer
	Health As Float
EndType	

Function NewPlayerInfo( Position As Vec3, Rotation As Vec3, Handle As String, Lives As Integer, Health As Float )
	Local Output As PlayerInfo
	
	Output.Position = Position
	Output.Rotation = Rotation
	Output.Handle = Handle
	Output.Lives = Lives
	Output.Health = Health
	
EndFunction Output

Player As PlayerInfo[]

Player.Insert( NewPlayerInfo( NewVec3( 1.0, 2.0, 3.0 ), NewVec3( 1.5, 2.5, 3.5 ), "Jim", 5, 0.8 ) )
Player.Insert( NewPlayerInfo( NewVec3( 0.4, 0.2, 0.1 ), NewVec3( 1.3, 0.7, 0.4 ), "Bob", 3, 1.0 ) )
Player.Insert( NewPlayerInfo( NewVec3( 0.8, 1.6, 3.2 ), NewVec3( 3.2, 1.5, 0.5 ), "John", 9, 0.4 ) )

Player.Save( "Players.Roster" )

Player.Length = -1

For Index = 0 To Player.Length
	Print( Player[Index].Handle )
Next
Print( "Nothing should've Printed as we have a blank Array" )

Player.Load( "Players.Roster" )

For Index = 0 To Player.Length
	Print( Player[Index].Handle )
Next
Print( "Now we have our loaded information" )

// Ghetto "WaitKey()" Function
Sync()
Sleep( 5000 ) // Wait 5 Seconds

End


This saves and loads from a Json File.
We can do the same with Types to and from Json formatted strings with the string = typedvariable.toJson() and typedvariable.fromJson( string ), if for example you wanted to handle the individual datasets rather than an array.

These files however are not (by default) stored in the Application\Media Folder... instead they're stored in the User\Application\Media Folder., which is hidden on most OS., although on Windows is easy enough to find.
I mention this, because the Json Format is readable in ANY text editor... so, you might want to add a layer of obfuscation or such for information you don't want tampered with.
Posted: 13th Mar 2023 2:58
@Raven

I never knew about array.save and array.load, I've never come across them in the AppGameKit documentation. I've just tried those two commands out for myself and have to admit that is a far easier way.

Thanks for bringing that to light.
Posted: 1st May 2023 0:07
I use the array.save function to make json skeletons for complex structures of data files I'm going to load in later. Just define the structure and make sure it has at least something in it, then save out.
I'm always forgetting a comma here and there in json files if I hand edit, so it's a nice shortcut.

Steve - look at the "arrays in version 2" section in the help, which shows some methods for arrays which aren't actual commands, so don't show up in the regular help.
Personally, I feel this was one of the biggest boosts to AppGameKit functionality when it was introduced, but as they are methods as opposed to commands, the info is a little bit buried in the help.