Javascript: I want to turn a csv into an array and play with it.

Status
Not open for further replies.

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
This is kinda noobish question really.
Imagine we got a file at the same folder with the game.
Let's say we got a CSV file named ab.csv with a line on it being:
STR STR STR STR STR INT INT INT

text1, text2, text3, text4, text5, 9, 4, 1

Now imagine 500 lines like that.
I want to store them to an array (two dimensions) and get access to the data from there.

How can I import all the lines from a csv file to an array, and how can I have access to a specific value from a specific line?

I want to set variable 21 to 'text4'


$game_variables[21] = How do we refer to text4 value? Remember, not from the file. I want an array in RAM.

EDIT:

I am actually looking for a solution that will be executed through Eventing as a command or a bunch of commands. If that was PHP and not JS I would ask to put a CSV into a two dimension array that is symbolized like this: $DATA[j] in PHP but I don't know if it is similar in JS whatsoever. What I am asking is an equivalent command in JS that will run in MV and make such a data structure, drawing values from rows of a csv file.

EDIT 2:
Not sure if that would work in MV and how it would work:


What I know so far is that if I have this in JS:
var items = [
[1, 2],
[3, 4],
[5, 6]
];

I can easily refer to the items as a two dimension array. Similar to other languages. Okay!
console.log(items[0][0]); // 1
console.log(items[0][1]); // 2
console.log(items[1][0]); // 3
console.log(items[1][1]); // 4

What is missing is the way to make the variable contain a csv row.
 
Last edited:

ShadowDragon

Veteran
Veteran
Joined
Oct 8, 2018
Messages
2,895
Reaction score
1,029
First Language
Dutch
Primarily Uses
RMMV
you can try to study this plugin by Hime (ExpTables) which use that function.
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
Not quite what I need, but I will give it a try. In the meantime, if anyone can suggest another solution, feel free to do so!
As I said, it is a noobish question implying I have never put my hands on JS really. I can play with Python or Java all right. But when it comes to JS I got zero experience, thus it makes the code kinda hard to follow.

Thus I would appreciate if someone would give a clean solution, I will edit OP giving more detailed info.
 
Last edited:

ShadowDragon

Veteran
Veteran
Joined
Oct 8, 2018
Messages
2,895
Reaction score
1,029
First Language
Dutch
Primarily Uses
RMMV
Himes exp tables use a CSV file and import the data from it.
if you can find out how that is done, you can do the same for the stuff you need.
maybe you can modify stuff to your needs :)

I have a plugin that require css, but it aint easy as well to modify the css file.
but its a nice way to study how its done and how they read it.

it's even possible to read other files as well like html, txt for intants which are
used for quest log or guidens, there are more files, but you have to import them correctly.
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
maybe you can modify stuff to your needs :)
Or maybe I can't because I am not familiar with JS syntax.
I can see what HIME does.
What I need is not a plugin though.
Maybe a plugin that can do that with a plugin command, well that would be nice.
And then have plugin commands to set data into variables or something.
But you can manually give these commands.
The only thing missing is how I can put the csv on a data structure and HIME code doesn't help me a LOT on how to ONLY do that because after all it is a plugin, not a set of commands.



I am not sure if that would work inside MV.



Anyway, If I won't find a way, I will have to hardcode all entries one by one. Not what I got in mind really though.
 
Last edited:

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
480
Reaction score
1,467
First Language
Indonesia
Primarily Uses
RMMV
If in the end you are trying to access stored data and put em into in game variables, why not just use the js as a plugin then directly access it?
1588753280085.png
1588753420703.png
1588753461769.png
1588753493874.png
1588753505394.png

I've always do it like that, tryin to spread this breakthrough, but keep it to myself in the end.
Feels like I'm the only one doing it... unless there's a problem doing it like this?
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
O.O

Wait WHAT?
Can you please send me an example code? I wanna see how you implemented it as a plugin and how you made the structure inside it. It could be an alternative.
But to be honest I would prefer a csv approach to have more flexibility on adding and removing stuff ad-hoc from the csv file and have the application being updated with more or less data automatically.
I can always update a js file instead, what am I talking about?
That awkward moment that someone steps into and tells you: "Bro, you are thinking it the wrong way!"



Both @ShadowDragon and @Kuro DCupu thank you, but if you could give me a sample code with a bunch of entries, that would be perfect. I wanna know how a plugin opens and close. Meanwhile I will be actually searching. I can take it from there and start building.
 
Last edited:

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
480
Reaction score
1,467
First Language
Indonesia
Primarily Uses
RMMV
It's just a loose variable inside a function without any accessor.
But I do have a sample exclusively to make tutorial out of it.

There's the attachment.
Just create new project, then overwrite folder data and js. You have to manually include the plugin from the plugin manager tho.
 

Attachments

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
So since I was torturing myself to find a way to add a csv file to an array in RAM.
Then I read about the fabulous idea to just put the data into a plugin.
Then I started thinking to the right direction.
What if We can actually script EVERYTHING using a kinda complex but simple Eventing system?

What I needed, was Script commands.
And then it hit me!!



◆Control Variables:#0001 = [2,2,3]
◆Control Variables:#0020 = 2

◆Script:$gameVariables.value(1)[1] = 1;
◆Script$gameVariables.setValue(2, $gameVariables.value(1)[1]);
◆Script$gameVariables.setValue($gameVariables.value(20), 'Hello');
◆TextNone, Window, Bottom
: :\V[1]
: :\V[2]
: :\V[4]


Works like a charm.
You can iterate, you can change value, you can read value, thus I think this is the best way to play ball.

So now I want to ask about something else.
But that question will be for my next thread... which is the following one, and I share so people here can also benefit:



Closing.

 
Last edited:
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,049
Members
137,570
Latest member
fgfhdfg
Top