dMZX Forums: CSV Reader - dMZX Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CSV Reader

#1 User is offline   CJA 

  • «≡larch bucket≡»
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 3,262
  • Joined: 23-June 05
  • Gender:Male
  • Location:......@.c....

Posted 08 January 2017 - 03:01 AM

Well, here it is in all its glory. This snippet loads one or more CSV files as two-dimensional arrays of counters. All it needs is your list of CSV files to load. You might also want to make sure commands is set to something acceptable.

Attached here is version 2. You might want to make sure there isn't a newer version later in the thread. There are a couple extra features that I'll go over later in the thread as well.

Why use this?
It's a quick way to get tabular data into your game.

Why not pull in records only when I need them?
CSV files don't work well as transactional databases. Also, this is MZX.

What if my .csv is in another folder?
Paths! Long Extensions! No Extensions! It Really Works!

Why not add <x>?
Hooray!! Tell me!

This is broken!
Of course it is, I'm CJA.

Does this work with tab-seperated lists?
Yes, but only if one tab separates each item. Just find set "seperator" 44 and replace it with set "seperator" 9. This will work with any character, as long as there's one seperator between each cell, the seperator doesn't appear inside a cell, and the file uses standard line endings (\r, \n, or \r\n). If you're writing dialogue, for example, there are a lot of commas, which you can see in this sentence (as an example, of course), and, instead of commas, you may want to use a better character for serialization, namely, char 9, the Tab character.

I want this to run when my game loads, but my global robot's already doing stuff! OK, do this.
  • Export your global robot to global.txt or whatever.
  • Replace your global robot with this one line: set "csv_reader.txt" "LOAD_ROBOT"
  • Find this line in the CSV reader: . "Done"
  • Replace it with this: set "global.txt" "LOAD_ROBOT" (or whatever)
  • There we go! Global robot will load your CSVs and then go back to its usual business.

Attached File(s)


Need a dispenser here.
0

#2 User is offline   Graham 

  • . "@Master Procrastinator"
  • PipPipPipPip
  • Group: Members
  • Posts: 625
  • Joined: 28-December 12
  • Gender:Male
  • Location:Oregon

Posted 08 January 2017 - 04:55 AM

I'm sorry, I've missed something. What is a .csv file?
Currently working on Servo for MegaZeux, I hope to complete it by the middle of 2015? Who knows...

"Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you’re a mile away and you have their shoes."
-Jack Handey
0

#3 User is offline   KKairos 

  • not an actual chipmunk
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,245
  • Joined: 05-August 00
  • Gender:Male

Posted 08 January 2017 - 05:13 AM

Basically an alternate way of storing tables. Excel and its free/freeware variants can usually export to .CSV instead of a proper 'table file.' It may or may not be ideal for some things, but for lists like monster/item attributes, it could be pretty much golden. (E.g. it would be great if I'd thought to do something like this with Card Knights, it would've saved a lot of grief.)
darganflayer.net kaikairos.dev itch.io
0

#4 User is offline   CJA 

  • «≡larch bucket≡»
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 3,262
  • Joined: 23-June 05
  • Gender:Male
  • Location:......@.c....

Posted 08 January 2017 - 09:48 PM

Example usage


Making the CSV file

I have an RPG and I decided to have 10 inventory slots, each slot contains an item number. But too late, I realized that I want to add over fifty different kinds of items! I want to move this data out of the game to make it easier to work on. So, I create this table in Excel:

Posted Image

I "Save As" this sheet as item.csv, which now contains this text:

ID,Name,HP,MP,Description,Value
0,Empty,0,0,Empty slot,0
1,Potion,50,0,Heals 50 HP!,10
2,Ether,0,50,Heals 50 MP!,10
3,Elixir,*HPLimit,*MPLimit,Heals all HP and MP!,999


Configuring the CSV reader

I change the CSV reader to read one CSV, item.csv, by changing these lines:

. "Starting with $csv0, list your .csv file paths and count."
set "$csv0" "item.csv"
set "csv_file_count" 1


Then I test the board, and look at the counter debugger. There are a slew of counters that correspond to the contents of the file:

Posted Image

Posted Image

Note that the cells' names appear as: (name of the file), (row number), (column name), and the cell's value appears under this cell's counter name.

Using these new counters in MZX

Great! So let's say I store the ID's of the items in slot0 - slot9 counters. In order to show what the player has in each slot, all I need to do is:

* "Slot &this_slot& contains: &$item('slot&this_slot&')Name&"


Or, if it makes more sense:

set "this_item" to "slot&this_slot&"
* "Item number &this_item& is known as: &$item('this_item')Name&"


The ID referencing feature

I'll talk about this later!

Spoiler

Need a dispenser here.
0

#5 User is offline   CJA 

  • «≡larch bucket≡»
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 3,262
  • Joined: 23-June 05
  • Gender:Male
  • Location:......@.c....

Posted 09 January 2017 - 03:46 AM

Cast as counter

If you set the cast_to_counter variable to a character (such as ! or *), it will lookup any cell that begins with that character as a counter, instead of a literal value. So, if cast_to_counter equals '*', and a cell contains "*max_health", then instead of storing the literal value "*max_health" in a string, the reader will look up the counter max_health and use that as the value of that cell.
Need a dispenser here.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users