.txt files in rgss

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

Previous topic - Next topic

Apidcloud

Thanks a lot everyone :P
Im at school right now, but as soon as i arrive home I will practise it a bit...

Another question...u said that it will make my document an array right?
How does it works in this case?
line[0] = #information on first line
line = #information on i line


like that?
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.

Storm

Quote from: Apidcloud on February 07, 2011, 06:22:59 am
Thanks a lot everyone :P
Im at school right now, but as soon as i arrive home I will practise it a bit...

Another question...u said that it will make my document an array right?
How does it works in this case?
line[0] = #information on first line
line = #information on i line


like that?


It splits into array.
Example, this is your text document:
+-------------------------------+
| Dear Blah,                              |
| How are you doin?                   |
| If you received my msg just call |
| me back, ok?                          |
|                                             |
| Love,                                    |
| Awesome Face                        |
+-------------------------------+

line[0] = Dear Blah,
line[1] = How are you doin?
line[2] = If you received my msg just call
line[3] = me back, ok?
line[4] =
line[5] = Love,
line[6] = Awesome Face
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

Hmmm ok, and what if I want to split my lines? xD
Something like this:

#Document
1 => 2
3 => 4
#

How would i use 'values' and 'keys' of arrays?
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

Ryex

for this you need regular expressions, there is a good tut here on this forum that explains how they work

take this code example with you as it dose what you want.

string = "1 => 2"
string.gsub(/([0-9]+)\s=>\s([0-9]+)/)
key = $1
value = $2


of course there is like 10 ways to do the second line. Regexp are heavily intragrated in to ruby

you could do it like this

case string
when /([0-9]+)\s=>\s([0-9]+)/
  key = $1
  value = $2
end


this is most useful when there is several patterns the string could follow

you could also do this

if string =~ /([0-9]+)\s=>\s([0-9]+)/
  key = $1
  value = $2
end



good luck
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

February 07, 2011, 12:06:32 pm #25 Last Edit: February 07, 2011, 12:19:52 pm by Apidcloud
Thanks a lot xD
Although, shouldnt we use: key = $1.to_i #? xD

Well, I was doing something like that as well
Thanks again for the help

Bye~
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

Ryex

yes true, the way I did it you'll just have strings, if you want to format those strings use to_i for an integer and to_f for floating point values.
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

February 07, 2011, 03:06:32 pm #27 Last Edit: February 07, 2011, 03:14:37 pm by Apidcloud
Outch xD I could do something oO but it is returning an error.
Permission denied to acess the document lol

whats happening? <.< I used this as 'file' opening:
file = File.open("Data", "rb").each_line {|line|}

When I test the prject and this part comes up, it appears
---------------------------
Teste
---------------------------
Erro no Script Text -part 2, em 266 na linha 'Errno::EACCES'

Permission denied - Data
---------------------------
OK   
---------------------------

Someone help here T_T
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

Your not using the call right. Nor are you including the file extension. I'm at work at the moment, I'll explain more when I get home here in a about an hour.
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.

Apidcloud

February 07, 2011, 04:24:07 pm #29 Last Edit: February 07, 2011, 04:29:22 pm by Apidcloud
Forgot about the extension .txt <.< epic fail xD Thanks (:

@Edit <.< I though all my logical was ok...but i was wrong XD screen gets froze everytime i call this...
So, my code consists in picking up the strings and numbers from the .txt file and use them as teletransportation data...
In the document, there are lines as this one:
"string" = "string", number, number, number.
Here is the code:

@map_name   = []
    @respawn_map =[]
    @respawn_x   = []
    @respawn_y   = []
    @respawn_dir = []
$old_map_id = $game_map.id
data = load_data("Data/MapInfos.rxdata")
    return if !File.exists?("Data")
    file = File.open("Data.txt", "rb")
    file.each_line do |line|
      case line
      when /(\".*\")\s\=\>\s(\".*\")\,\s(\d+)\,\s(\d+)\,\s(\d+)\,\s(\d+)\./i
        @map_name.push     = $1
        @respawn_map.push  = $2
        @respawn_x.push    = $3.to_i
        @respawn_y .push   = $4.to_i
        @respawn_dir.push  = $5.to_i
      end
    end
    file.close
    for i in 1...data.size
      if data[i].name != nil
        actual_name = data[i].name
        respawn_name = data[i].name
      end
      if actual_name == @map_name
        actual_id = i
        break
      end
      if respawn_name == @respawn_map
        respawn_id = i
        break
      end
    end
    for j in 0...@map_name.size
      return if @map_name.nil?
      if $old_map_id == actual_id
        $game_temp.player_new_map_id = respawn_id
        $game_temp.player_new_x = @respawn_x[j]
        $game_temp.player_new_y = @respawn_y[j]
        $game_temp.player_new_direction = @respawn_dir[j]
        $game_player.refresh
        $game_map.update
        $scene = Scene_Map.new
      end
    end
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

I fail to see any reason to make a simple task so complicated as all that. You can simply write an array to the text doc, then in the game set a variable to it using the "eval" function.

ex. Text Document (Array is used like this: [MAP_ID, RESPAWN_X, RESPAWN_Y, DIRECTION])

[1, 20, 20, 2]
[2, 15, 30, 4]
[3, 18, 9, 6]
[4, 2, 45, 8]


Then within the code, set it to a variable:

@respawns = IO.readlines("Data.txt") # No need to include "rb" or close file with this method
@respawns.collect! {|array| eval(array) } # Execute block on each element, then change array to returned value


If you want the map names in the array, run a method to add them. You could do this different ways, or even include it in the text document if you would like. I wouldn't even worry about, but just use the ID to find it if it was ever needed.

Either way, you now have an array labeled "@respawns", whose elements are arrays of integers.
To set the current respawn, just reference the desired one by its index, or line number of the text document if it helps you to think of it that way (remember the index is 0 based).


temp = @respawns[WHATEVER_INDEX_YOU_LIKE]
$game_temp.player_new_map_id = temp[0]
$game_temp.player_new_x = temp[1]
$game_temp.player_new_y = temp[2]
$game_temp.player_new_direction = temp[3]
$game_player.refresh
$game_map.update


You actually could do it with less code than that, but this should demonstrate how it works a little easier.

I am curious though, why would you ever do it this way. If you are merely trying to learn, that it fine, I have done much stranger things in the process of learning Ruby, but I would highly suggest against something so inefficient for an actual game. You could simply store the values in array within the game, and not use a file at all.
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.

Apidcloud

February 09, 2011, 11:05:01 am #31 Last Edit: February 09, 2011, 12:12:18 pm by Apidcloud
First of all XD
Im doing this...most likely to learn something about File, IO and Dir classes...and I already did this(without txt file) inside the script :P
Now I wanted to 'change' that information into .txt folder and then use rgss to take all that, string and integers...
Im going to try to use what u said.

One more time, thanks a lot for the help ^^

@Edit:
I failed using what u said there XD

Im going to explain my previous code..
Without .txt file, I could just do a class with this type of conditions:

if $old_map_id == 1
$game_temp.player_new_map_id = 2
       $game_temp.player_new_x = 10
       $game_temp.player_new_y = 20
       $game_temp.player_new_direction = 6
       $game_player.refresh
       $game_map.update
       $scene = Scene_Map.new
end


With txtfile ->
First of all, I though about creating some arrays, what would store the values of .txt document.

@map_name   = []
@respawn_map =[]
@respawn_x   = []
@respawn_y   = []
@respawn_dir = []


Then, I thought about using regexp to take the lines that follow it:

data = load_data("Data/MapInfos.rxdata")
   return if !File.exists?("Data")
   file = File.open("Data.txt", "rb")
   file.each_line do |line|
     case line
     when /(\".*\")\s\=\>\s(\".*\")\,\s(\d+)\,\s(\d+)\,\s(\d+)\,\s(\d+)\./i
       @map_name.push     = $1
       @respawn_map.push  = $2
       @respawn_x.push    = $3.to_i
       @respawn_y .push   = $4.to_i
       @respawn_dir.push  = $5.to_i
     end
   end
   file.close

Each of these variables, will store the correspondent $'s.

After this, I needed to check the name of the maps(map where actor dies, and where it respawns) and then convert it to id:

for i in 1...data.size
     if data[i].name != nil
       actual_name = data[i].name
       respawn_name = data[i].name
     end
     if actual_name == @map_name
       actual_id = i
       break
     end
     if respawn_name == @respawn_map
       respawn_id = i
       break
     end
   end


Finally, as each array have the same size, I only needed to use the size of one to make all necessary conditions...

for j in 0...@map_name.size
     return if @map_name.nil?
     if $old_map_id == actual_id
       $game_temp.player_new_map_id = respawn_id
       $game_temp.player_new_x = @respawn_x[j]
       $game_temp.player_new_y = @respawn_y[j]
       $game_temp.player_new_direction = @respawn_dir[j]
       $game_player.refresh
       $game_map.update
       $scene = Scene_Map.new
     end
   end


This latter code, with .txt logic it is freezing the screen all the time I call it xD

Someone help please? :P
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

Just make a hash with the map IDs as the keys. Then you can simply use a single array to store all the needed data like the x, y, and direction.

All of the loops, iterations, and regular expressions are unneeded and are very inefficient. They have no real practical use, and are the likely use of your problem. It would be a good idea to rethink your initial approach than to try to force a bad one to work. I've had to rewrite entire scripts for this reason. About 3/4 of the way through, I see an easier way to do it, so I erase everything I've done and start over. The final result is usually 1/2 the code, easier to use, easier to overview, and is far more efficient. Sometimes you just need to go back to the drawing board. Everything you are trying to do could be done in 20 lines of code or less.

For the last example of code you have, I do see one obvious error right off the bat. The "return if @mapname.nil?" is quite worthless. The iteration would never get far enough to check that line, and would through an error if it was nil. I imagine you meant "@mapname[j] == nil" (I prefer using comparison, not ".nil?"). This will check the element for this iteration. I don't believe you need to call "$scene = Scene_Map.new" IF this is being called from Scene_Map. 

What my previous post said should work. The only thing I see that I forgot to mention is what Blizzard mentioned earlier about the "/n" not being removed. That can be fixed by substituting this line:
@respawns.collect! {|array| eval(array) }

...with this one:
@respawns.collect! {|array| eval((array.gsub(/\n/) { '' })) }
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.

Apidcloud

I just arrived home xD that's the reason I couldn't asnwer here...I'm going to try to use that again :P

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

Zeriab

Lemme warn you that you cannot load text files normally form within an encrypted archive.
If you want to encrypt your game you can convert the text file to a format load_data understands. (You can load the file as a string and save it using save_data)
One problem with that approach is that you cannot search for all file and you have to try and load a file to check if it exist. Since the files are encrypted they are not immediately available for the players to edit. Even if they decrypt the game it's still bothersome to edit marshalled files.

You can have them outside of the encrypted archive which is most easily achieved by not putting them in the data folder or some subfolder, but rather create another folder for the text files. This way you can use the file system utilities all you want. The player will be able to see and depending on your format edit it. Which for some cases is positive.

Of course you can have combination where you use one way for some files and the other for other files.

*hugs*

Apidcloud

Thanks XD

At the moment im studying something else (something that i really need to do, and fast...), so when im over with it, I'll try it again XD

See ya
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