Reading and writing game files.

Started by hansiec, January 04, 2012, 11:44:10 pm

Previous topic - Next topic

hansiec

January 04, 2012, 11:44:10 pm Last Edit: January 04, 2012, 11:50:53 pm by hansiec
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?

ForeverZer0

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']
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

hansiec

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?

ForeverZer0

array[0] == 'Hello'
array[1] == 'hi'
array[2] == 'Test'
array[3] == 'bye'

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.