How to Store Boolean Data to a Table?

I've got a little prototype game where the player encounters animals; I am currently using saveTable and loadTable to both generate the animals from a CSV file of attributes, and save/load the current game state to/from a CSV. However, I wanted two of the animal attributes to be boolean values. Trying to use newrow.setBoolean(...) didn't work, apparently this isn't a Table class function. Currently I'm using an integer value that can be either 1 for true or 0 for false; I could do the same thing with a string if I really wanted to see "true" and "false" but I'm thinking I'd have to turn it into a boolean variable before I could use it as such which seems cumbersome. I'm just wondering, is there a more natural way to pass boolean values into and out of a CSV table? No big deal either way; thank you!

Answers

  • Answer ✓

    I looked, and couldn't find a more natural way. Your 0/1 solution seems fine to me. I would avoid using "true" and "false", because "false" might mistakenly evaluate to true.

  • Alternately you could extend or fork Table and TableRow and add your own boolean getters and setters. You could even submit a patch.

    Casting int to boolean is a lot less work, though....

  • Again, people are trying to use a heavyweight class to store data that better suits a built-in type. Just use an array of bytes.

  • @koogs -- I thought that the OP wanted two columns of booleans mixed in with other attributes of other data types. During game prototyping one advantage of the heavy-weight Table type would be that you can keep changing the data format as the game design evolves, and the extra columns will be automagically available through the Table on read-write round-trips without changing any load / access / store logics or worrying about column order, entry alignment etc.

    @ComfyCat -- Is your Table of a fixed number of animal types, or is it of animal instances? If you won't be inserting or deleting animals then on csv read or on loadTable you could copy the boolean columns out to an array -- or you could just go with a collection of parallel arrays, I suppose....

  • Greetings All! Thanks for your suggestions. I should have mentioned that I am a total beginner; I have only been messing around in Processing for three weeks now, and it's my first real programming language beyond HTML/CSS. I don't know what an array of bytes would be... And I'm not familiar with parallel arrays. I think I'm probably best to stick with my 0 or 1 solution, but I really appreciate the suggestion to fork the Table and TableRow functions; it's nice to know that advanced tinkering is possible that way! Thanks again.

  • @ComfyCat -- if you have further questions on this thread, be sure to post your actual complete code -- it is much easier for the forum to give you feedback and advice then if you try to describe things.

    If you are a beginner it might actually be easier to use simple arrays as @koogs suggests than to use Table (which can be quite complicated to use). Check out the Arrays tutorial if you are interested...

    https://processing.org/tutorials/arrays/

Sign In or Register to comment.