Read Records question


#1

A student of mine has created records in the data table, some of the properties hold lists of numbers. When he uses readRecords to get a copy of the data, the lists come back as lists of nulls instead of lists of numbers.
Any questions on how/why this is happening? I know objects and references to objects can act unexpectedly, but I’m not sure whats going on under the hood on this one.

Brian


#2

@fleisch1984 Could you share a copy of the student’s code?


#3

I believe the culprit is in the saveGame and/or loadGame sections, which are in the range line 345 - 400 or so.


#4

I don’t expect that anyone will have time to comb through this program, but essentially what happens is that when you play and then use the game’s save feature, it writes game data to the records. One of the columns is completedStars which is a list like [12, 35, 123]. When the game is loaded, the code attempts to read the table to get back to the game state. The list of completed star numbers comes back as a list like this [null, null, null]. I can look in the table and verify that in the table, the list does contain numbers. They turn into nulls when read from the table.

I wasn’t able to understand why in the time I have looked at it, but I do know that reference types can act in unexpected ways. At least for novice programmers like myself!


#5

Without looking at the code I’m guessing the problem is related to App Lab “stringifying” (or not) the array that you’re trying to save or load. Try experimenting with one or some combination of

JSON.stringify(list) on the way into the database
and/or
JSON.parse(records[i].completedStars) on the way back out.

I think I ran into this problem when I made this app: https://studio.code.org/projects/applab/E9gTMKV_ugNzhnQc5k4YMw/

Poke around in there to see what I did. Hope that helps,

–Baker