.txt files in rgss

Started by Apidcloud, February 06, 2011, 03:49:10 pm

Previous topic - Next topic

Apidcloud

Hi there everyone (:
Im here to ask if any of u can recomend some links or scripts with stuff with .txt files and that... (open, using stuff inside it, etc)

I really want to start studying it (:

Thanks
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

wowa xD
Thanks a lot, I'm sure it will help me ^^

By the way, do you know any script that uses .txt file as a 'source'?
For instance, imagine that a script needs numbers and names for a lot of stuff.
Then, the scripter using rgss, reads that codes ans uses them for something.

That's what I'm more interested with xD
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Storm

You can use FileTest.exist?(filename) to check file if it is exist.
And use
Dir.glob("*."+filetype){|f|
  //Command (f = eachfile)
}

To each everyfile in the folder, you can put Dir.chdir(foldername){ to select a folder first. (Don't put an extra })

Use this to open a file.
File.open(filename,openingType){|f|
  //Command (f = eachfile)
}

You can choose a basic openingType from below..

  • wb (Write)

  • rb (Read)



f.read(characterCount) and f.write(value) will work for you as a command.
You can use writeFile(pathToFile+filename"."+filetype,value) to write a file right away too..

Hope this helped you. :)
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD


Apidcloud

February 06, 2011, 04:31:26 pm #4 Last Edit: February 06, 2011, 04:35:55 pm by Apidcloud
Well... I didn't understand it that well XD, maybe because im really at the beginning of this part of rgss :P

Thanks a lot ^^

Well, may I ask for an example? xD
So, imagine that i create a new txt file even before opening project xD

I put data that I will need in rgss scripts, such as:

Text file, named 'example'
something => does_that
... (so on)

Now, using rgss to pick up that data and use it...
File.open("example", rb)
#stuff picking up that data <.<
File.close

So, what should I begin with there?
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Storm

Quote from: Apidcloud on February 06, 2011, 04:31:26 pm
Well... I didn't understand it that well XD, maybe because im really at the beginning of this part of rgss :P

Thanks a lot ^^

Well, may I ask for an example? xD
So, imagine that i create a new txt file even before opening project xD

I put data that I will need in rgss scripts, such as:

Text file, named 'example'
something => does_that
... (so on)

Now, using rgss to pick up that data and use it...
File.open("example", rb)
#stuff picking up that data <.<
File.close

So, what should I begin with there?


I don't think so.. I'm finding it out too..
I'm wondering like you too, but still.. you can't convert STRING into FUNCTIONS!
If you find it let me know because none of the computer languages (PHP, RGSS, VB, C+, etc.) can convert string into functions.
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD


Apidcloud

Well, I've done an example with a string but...with numbers it would be possible or not? xD
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Storm

Quote from: Apidcloud on February 06, 2011, 04:49:54 pm
Well, I've done an example with a string but...with numbers it would be possible or not? xD


I'm thought you are talking about converting String into Functions! XD
Oh well, you just read a file you want to, and when you got the number just use [YOUR NUMBER].to_f.

Now you converted it to fixnum. (Number) :)
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD


ForeverZer0

February 06, 2011, 04:59:39 pm #8 Last Edit: February 06, 2011, 05:02:05 pm by ForeverZer0
Look into actual Ruby. That is how it is done with that, simply reading .rb files.
If you simply want to execute a whole file, or load it into memory, save a text document with an .rb extension and use:
require "TEXT_DOC_NAME"


If you want to use an external text file and execute code off of it, something like this should work:


lines = IO.readlines('TEXT_DOC_NAME.txt')
# You can use the ".join" method to combine the lines into a single string, otherwise it will be an array of lines
eval(lines[LINE_INDEX_TO_EXECUTE])


If you want to create an instance of the file:


file = File.open('TEXT_DOC_NAME.txt', 'rb')
# Do something
file.close


To write text to the doc:


file = File.open('TEXT_DOC_NAME.txt', 'wb')
file.write("This is a string")
file.write("\r\nThis is on the second line\r\n")
file.write("\tThis is the third line, and indented")
file.close



EDIT:
And "yes", you can convert strings into functions. I even came up with ways to construct and add new methods to instances of classes at runtime.
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.

Storm

Quote from: ForeverZer0 on February 06, 2011, 04:59:39 pm
Look into actual Ruby. That is how it is done with that, simply reading .rb files.
If you simply want to execute a whole file, or load it into memory, save a text document with an .rb extension and use:
require "TEXT_DOC_NAME"


If you want to use an external text file and execute code off of it, something like this should work:


lines = IO.readlines('TEXT_DOC_NAME.txt')
# You can use the ".join" method to combine the lines into a single string, otherwise it will be an array of lines
eval(lines[LINE_INDEX_TO_EXECUTE])


If you want to create an instance of the file:


file = File.open('TEXT_DOC_NAME.txt', 'rb')
# Do something
file.close


To write text to the doc:


file = File.open('TEXT_DOC_NAME.txt', 'wb')
file.write("This is a string")
file.write("\r\nThis is on the second line\r\n")
file.write("\tThis is the third line, and indented")
file.close



EDIT:
And "yes", you can convert strings into functions. I even came up with ways to construct and add new methods to instances of classes at runtime.



OH!!! Thanks Zer0! I totally forgot of the require method! I focused too much about the script so I forgot about the files.. DARN!
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD


Apidcloud

Well thanks a lot xD
Im going to test some stuff here with all that (:

lvl up everyone xd
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

@F0: You don't need to use \r if you open the files in 'w' or 'r' mode. Windows automatically adds them if you don't use 'w+', 'r+', 'wb' or 'rb'. Actually 'w' and 'r' are Windows hacks, all other OSes use binary file modes and it's kinda more convenient that way, too, because of multiplatform compatibility.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

@blizz
You are right, its just kind of a (bad?) habit I got into. Honestly, I have only wrote code under Windows platforms, so I have never really concerned myself with cross-platform compatibility, which is something I should pay more attention to now that I'm getting into other languages. Never really had the need when writing scripts for RGSS, though.
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.

Blizzard

I used to do it as well. But then I just switched to \n. xD
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Storm

Quote from: Blizzard on February 06, 2011, 05:39:30 pm
I used to do it as well. But then I just switched to \n. xD


I used to say "\n" is the basic of string. XD
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD


Ryex

in case your wondering to convert a string into a function call first convert it to a symbol then send a message to the object with the function

look into the .call method to learn more. in the ruby-docs it should be under object
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Apidcloud

Hmmm, Im testing some stuff here...although...it's difficult xD
document = "Something.txt"
file = File.open(document, rb)
for line in 0...file.lines
  IO.readlines(document)[line]
end
#I don't know exactly what I've to do now?
file.close


Well <.< I don't even know if this works at really...Can some1 help? xd
I accept advices for the logic of this code...

Note:
pick up .txt data and use it for something..?
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

February 06, 2011, 10:09:51 pm #17 Last Edit: February 06, 2011, 10:11:19 pm by ForeverZer0
file = File.open(document, "rb")

Note the quotes used around rb.
This sets the local variable "file" as an instance of the file. You can iterate it with methods like:
file.each_line {|line| print line } 

and...
file.lines.each {|line| print line }

You will also need to make sure to close the file when you are done with it.

OR you can do this, not both.

lines = IO.readlines(document)

This simply creates a normal array, with each element being a string line of the document.
You can iterate the array normally, and have no need to close the file.

What you do with code totally and utterly depends on what it is, and what exactly you are trying to accomplish. I really can't help you with that until I know what you are trying to do.
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.

Blizzard

I personally prefer the File.open way because it's a lot more readable. You can really see that it's a file that's being read.
I also prefer to open a file, use readlines or read, then close the file handle and after all of that work with the data that I read. I like doing it this way so the file is open as short as possible and other programs can open it afterwards. I do the same when writing to files. I first prepare the whole data, open the file, write everything into it and then close it right away.
Also, when using readlines or readline, keep in mind that the string will contain the \n at the end of the line if there is one. So you might want to use line.gsub!("\n") {''} on each line.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Storm

Quote from: Ryex on February 06, 2011, 06:03:08 pm
in case your wondering to convert a string into a function call first convert it to a symbol then send a message to the object with the function

look into the .call method to learn more. in the ruby-docs it should be under object


Can you actually link me the page or an example? There are so many calls.
I was working on the game called The Orion.
It would be nice if you can join our small community by sharing a little by little scripts/ideas at The Orion Forum

I am: RPG Maker XP Scripter
Project Working On: The Orion MMORPG Game, visit The Orion Forum for more information!
Visit my: Deviantart

LEVEL ME UP OR ELSE LEVEL ME DOWN, IT'S EASY! XD