-SOLVED- Reading from a text file?

Amon

Member
Joined
Nov 19, 2018
Messages
89
I'm trying to read level data from a text file. I am definitely going about this the wrong way and I could do with some assistance. Code below for my LoadLevel function.

Cerberus:
    Function LoadLevel:int(_level)
        Local tempx:int = 0
        Local tempy:int = 0
        Local LevelArray:int[][] = Arrays<int>.CreateArray(15, 9)
      
      
      
        Local CurrentLevel:string = LoadString("levels/level" + _level + ".txt")
        For Local x:int = 0 Until CurrentLevel.Length()
            LevelArray[tempx][tempy] = Int(Mid(CurrentLevel, x, 1))
          
            Select LevelArray[tempx][tempy]
                Case 1
                    Local c:Cube = New Cube(1, GreyBlock, 5, tempx * 64, tempy * 64, True)
                    Cube.list.AddLast(c)
print "GREY BLOCK"
            End
            tempx += 1
            If tempx > 16
                tempx = 0
                tempy += 1
            EndIf
        Next
          
        Return True
      
    End

I'm not getting any output on screen because I think the way I'm reading the text file is wrong as well as how I am loading each int into the array. The loadLevel function is called here:

Cerberus:
    Method Render:Void()
        Cls
        DrawText "GAME SCREEN!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5
        
        If KeyHit(KEY_1)
            LoadLevel(1)
        EndIf
        
        If Cube.list <> Null
            For Local c:Cube = EachIn Cube.list
                c.Draw()
            Next
        EndIf
        
    End
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,497
Did you check that you get the content of the file into the string?
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,497
BTW. Move that KeyHit check into OnUpdate.
 

Amon

Member
Joined
Nov 19, 2018
Messages
89
Ok, I haven't checked if I'm getting the data from the string so I'll do that now. Moving KeyHit check to OnUpdate did something to execute the LoadLevel function but it's constantly throwing an index out of bounds error no matter what I do.
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,497
Your array is 15×9 elements? Not knowing Diddy but I assume your check against tempx should be >14.
 

Amon

Member
Joined
Nov 19, 2018
Messages
89
Yeah I checked that already. The reason I use diddy's Arrays functions is because it sets up a 2d array which isn't natively supported in Cerberus-X because all arrays are 1 dimensional. Unless I'm mistaken?
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,497
Unless I'm mistaken?
Yes you are mistaken.

That line needs to be

Cerberus:
If tempx > 14

Because in your version above you try to access LevelArray[15][tempy] and LevelArray[16][tempy] which don't exist.

Array indexes start at zero, so in your example they go from 0-14 in the first index.
 

Amon

Member
Joined
Nov 19, 2018
Messages
89
Yes, I should have mentioned that I've made modifications since posting that. But yeah you did lead me in the right direction. Thank you. It's all working now but there was weirdness with the displayed data and what was in the text file. I happened to remember, for some unknown reason, that when reading strings, iteration always starts from the number 1 and not 0 so in my for loop I changed local x = 0 to local x = 1 to length().
 
Top Bottom