Quote: "im sorry for wasting your time"
you're not. i can use the exercise. if i'm not up for a given exercise, i just don't
otherwise, i tend to use a single line to hold all info for a given item/dataype and if "you cant have any spaces" means "blank" lines in a given datafile, just account for it.
as far as "not knowing how many lines", take advantage of dynamic arrays so that it doesn't matter.
the following generates an array of various guns that can be "picked up" or added to inventory (said GunChest), the ability to select which gun in the chest is currently active, remove it, etc. and (manually via [space key]) save the current inventory while marking which one is current, if any.
// Project: GunChest
// Created: 2022-07-24
// By: Virtual Nomad
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "GunChest" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )
// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
Type Gun
GunType$, Damage, RateOfFire#, Active //Remmeber this order for Save/LoadGuns()
EndType
GLOBAL GunTypes as Gun []
MakeGuns() //Generate some guns to choose from
GLOBAL MaxGuns = 4 //Chest holds 5 Guns
GLOBAL GunChest as Gun []
GLOBAL CurrentGun
GLOBAL File$ = "GunChest.dat"
If GetFileExists(File$)
LoadGuns()
Else // Add Starter Pistol
ThisGun as Gun
ThisGun.GunType$ = GunTypes[0].GunType$
ThisGun.Damage = GunTypes[0].Damage
ThisGun.RateOfFire# = GunTypes[0].RateOfFire#
ThisGun.Active = 1
CurrentGun = 0
GunChest.Insert(ThisGun)
SaveGuns()
EndIf
do
If GetRawKeyState(27) then End //[ESC]
Print( "[Space] to Save GunChest | [F12] to open DataFile" +CHR(10))
If GetRawKeyPressed(32) then SaveGuns() //[Space]
If GetRawKeyPressed(123) then OpenBrowser( GetWritePath()+"/media/"+File$ ) //[F12]
If GunChest.Length > 0
Print("Select Gun [1-" + STR(GunChest.Length+1) + "]")
CheckSwap( GetCharBuffer())
EndIf
If GunChest.Length > -1
Print( "RMB to Discard Current Gun" )
If GetRawMouseRightPressed() then DropGun()
EndIf
If GunChest.Length < MaxGuns
Print( "LMB to Add Gun (becomes Current)")
If GetPointerPressed() and GunChest.Length < MaxGuns then AddGun()
EndIf
ShowChest()
Sync()
loop
Function DropGun()
If CurrentGun < GunChest.Length
GunChest[CurrentGun+1].Active = 1 //CurrentGun stays the same
GunChest.Remove(CurrentGun)
Else
If GunChest.Length > 0
GunChest[0].Active = 1
GunChest.Remove(CurrentGun)
CurrentGun = 0
Else
GunChest.Remove(CurrentGun)
CurrentGun = -1
EndIf
EndIf
EndFunction
Function SaveGuns()
ThisFile = OpenToWrite(File$)
If GunChest.Length = -1 //if the chest is empty...
WriteLine(ThisFile,"") //overwrite whatever might currently be in the datafile
Else //GunType$, Damage, RateOfFire#, Active
For x = 0 to GunChest.Length
GunType$ = GunChest[x].GunType$
Dam$ = STR(GunChest[x].Damage)
RoF$ = STR(GunChest[x].RateOfFire#)
Active$ = STR(GunChest[x].Active)
WriteLine(ThisFile, GunType$ + "|" + Dam$ + "|" + Rof$ + "|" + Active$)
next x
EndIf
CloseFile(ThisFile)
EndFunction
Function LoadGuns()
ThisFile = OpenToRead(File$)
Repeat
ThisLine$ = ReadLine(ThisFile) //GunType$, Damage, RateOfFire#, Active
If ThisLine$ <> ""
ThisGun as Gun
ThisGun.GunType$ = GetStringToken(ThisLine$,"|",1)
ThisGun.Damage = VAL( GetStringToken(ThisLine$,"|",2) )
ThisGun.RateOfFire# = VALFloat( GetStringToken(ThisLine$,"|",3) )
ThisGun.Active = VAL( GetStringToken(ThisLine$,"|",4) )
GunChest.Insert(ThisGun)
If ThisGun.Active = 1 then CurrentGun = GunChest.Length
EndIf
Until FileEOF(ThisFile)
CloseFile(ThisFile)
EndFunction
Function CheckSwap(Gun$)
Gun = VAL(Gun$)-1
If Gun > -1 and Gun <= GunChest.Length and Gun <> CurrentGun
GunChest[CurrentGun].Active = 0
GunChest[Gun].Active = 1
CurrentGun = Gun
EndIf
EndFunction
Function AddGun()
NewGun = Random(0,GunTypes.Length)
ThisGun as Gun
ThisGun.GunType$ = GunTypes[NewGun].GunType$
ThisGun.Damage = GunTypes[NewGun].Damage
ThisGun.RateOfFire# = GunTypes[NewGun].RateOfFire#
DeselectCurrentGun()
ThisGun.Active = 1
GunChest.Insert(ThisGun)
CurrentGun = GunChest.Length
EndFunction
Function DeselectCurrentGun()
for x = 0 to GunChest.Length
If GunChest[x].Active = 1
GunChest[x].Active = 0
ExitFunction
EndIf
Next x
EndFunction
Function ShowChest()
Print("")
For x = 0 to GunChest.Length
ThisGun$ = STR(x+1) + "=" + GunChest[x].GunType$ + "|Dam:" + STR(GunChest[x].Damage) + "|RoF:" + STR(GunChest[x].RateOfFire#,2)
If GunChest[x].Active = 1 then ThisGun$ = ThisGun$ + " <<"
Print(ThisGun$)
Next x
EndFunction
Function MakeGuns()
//Make 3 Pistols
For x = 1 to 3
ThisGun as Gun
ThisGun.GunType$ = "Pistol"
ThisGun.Damage = x
ThisGun.RateOfFire# = 4-x
GunTypes.Insert(ThisGun)
Next x
//Make 3 Rifles
For x = 1 to 3
ThisGun.GunType$ = "Rifle"
ThisGun.Damage = 3+x
ThisGun.RateOfFire# = 1+x
GunTypes.Insert(ThisGun)
Next x
//Make 3 Machine Guns
For x = 1 to 3
ThisGun.GunType$ = "Machine Gun"
ThisGun.Damage = x
ThisGun.RateOfFire# = x/3.0
GunTypes.Insert(ThisGun)
Next x
//Make 3 Shotguns
For x = 1 to 3
ThisGun.GunType$ = "Shotgun"
ThisGun.Damage = x*3
ThisGun.RateOfFire# = 2+x
GunTypes.Insert(ThisGun)
Next x
EndFunction
when you run it the first time, it will add a starter pistol to the chest and save the data. pick up/discard/set active guns however you want then [space] to save. [F12] to view it outside of the app.
it's a slightly different approach to what you're currently doing but hopefully there's something in there to help achieve what you're looking for.
and, if you want to add more than the GunChest to a given Save file, you can add things to help with that. ie,
Rifle|5|3.000000|0
Shotgun|9|5.000000|0
Rifle|5|3.000000|1
Shotgun|9|5.000000|0
Rifle|6|4.000000|0
can be something like:
GUN=Rifle|5|3.000000|0
EXPLOSIVE=Large|50|50
HEALTHKIT=Small|10
...
then, when loading the inventory file:
Function LoadInventory()
ThisFile = OpenToRead(File$)
Repeat
ThisLine$ = ReadLine(ThisFile)
If ThisLine$ <> ""
Item$ = GetStringToken(ThisLine$,"=",1)
If Item$ = "GUN" then AddGun(GetStringToken(ThisLine$,"=",2))
If Item$ = "HEALTHKIT" then AddHealthKit(GetStringToken(ThisLine$,"=",2))
If Item$ = "EXPLOSIVE" then AddExplosive(GetStringToken(ThisLine$,"=",2))
EndIf
Until FileEOF(ThisFile)
CloseFile(ThisFile)
EndFunction
basically, something in a given line to tell your program what to do with the data that follows.
i hope this helps