Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: hansiec on January 04, 2012, 11:44:10 pm

Title: Reading and writing game files.
Post by: hansiec on January 04, 2012, 11:44:10 pm
So I'm just making sure this works with normal XP projects cause I'm using one that can do this


File.open(filenamehere,"wb"){|f|
       f.write("Hello")
       f.write("hi")
       f.write("Test")
       f.write("bye")
       }

File.open(filenamehere,"rb"){|f|
      hello=f.read(5)
       hi=f.read(2)
      test= f.read(4)
       bye=f.read(3)
       }


Would that work?
Title: Re: Reading and writing game files.
Post by: ForeverZer0 on January 05, 2012, 12:00:23 am
It would be easier to simply write each word on a line, and read them into an array. Very rarely will you ever be using the seek functions for grabbing only a certain number of bytes when dealing with text.

file = File.open('text.txt', 'wb')
file.write("Hello\nhi\nTest\nbye") # \n is a newline symbol (must use double quotes, not single)
file.close

array = IO.readlines('test.txt')
p array

--> ['Hello', 'hi', 'Test', 'bye']
Title: Re: Reading and writing game files.
Post by: hansiec on January 05, 2012, 09:49:55 am
Could you possible teach me arrays, I have no idea how to use em.... I think i've got it right, but I'm just making sure.

p array

--> ['array1', 'array2', 'array3', 'array4']


but how do I use them in the array?
Title: Re: Reading and writing game files.
Post by: ForeverZer0 on January 05, 2012, 10:39:41 am
array[0] == 'Hello'
array[1] == 'hi'
array[2] == 'Test'
array[3] == 'bye'