Ruby/RGSSx questions that don't deserve their own thread

Discussion in 'RGSSx Script Support' started by Mr. Bubble, Mar 14, 2012.

  1. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,312
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    Yes it would; however, if you done something like this...

    module File_Crap    $doc_name = "Text Document.txt"    # <<  Initial Text  $text = "Random Text \r\nMore Text \r\nSome Extra Text \r\n"   if !File.exists?($doc_name)    File_A = File.new($doc_name, "w+")  end  endthen when u want to add some crap on to the string do this..

    Code:
      $text += "Additional Text Random Text \r\nMore Text \r\nEven More Extra Text \r\n"   File.open($doc_name, "w+") { |doc| doc.write( $text ) }
     
    Last edited by a moderator: Nov 15, 2013
  2. FenixFyreX

    FenixFyreX Fire Deity Veteran

    Messages:
    434
    Likes Received:
    307
    Location:
    A Volcano Somewhere
    First Language:
    English
    Dekita, actually that would add the entirety of $text to the document again, along with your edits. So there'd be duplicate data within your log. 

     Shaz, if you'd like to add to it, just do like Ruby 1.8.X and just open the file and append text. I would not leave a file handle open like that though, it's too risky; it isn't good practice. It would be better to just do something like this:

    module MyLog  FileName = "Data/MyLog.txt"  def self.append(text)    File.open(FileName, 'w+') {|doc| doc << text }  end  def self.overwrite(text, offset = 0)    File.open(FileName, 'w')  {|doc| doc.seek(offset); doc.write(text) }  end  def self.read(offset = 0)    File.read(FileName)  endendIf you must keep a handle to the file open constantly, I would suggest using File#sync and/or wrapping the entire rgss_main in a begin..rescue..end clause to ensure that the file handle is closed upon exit. Or something.
     
    Last edited by a moderator: Nov 16, 2013
  3. IceDragon

    IceDragon Elder Cookie Dragon Veteran

    Messages:
    73
    Likes Received:
    60
    Location:
    Jamaica
    First Language:
    English
    Primarily Uses:
    N/A
    <layman>

    Um, you know ruby allows you to open a file, and leave it open... while still adding stuff to it without worrying about closing it?

    </layman>

    Eh em, right ruby allow you to "sync" a File, basically you can add new data to it without having to manually reopen

    and close the file each time:

    # be warned, using w will overwrite the original file, if you only need to add to it use "a" (Append)my_file = File.open("my_file.log", "w") # write only, we don't need read accessmy_file.sync = true # here is the magicmy_file << "My Text\n"# check the output my_file << "Some other text\n"if you only need to create the file once, and log once:
    Code:
    File.open("my_file.log", "w") do |f|  f.puts("Hello World! I'm a cookie eating Dragon, methinks.")end
    Clean and ruby ensures your file is closed at the end of the block. :)
     
    Acetonide likes this.
  4. Zeriab

    Zeriab Huggins! Veteran

    Messages:
    1,200
    Likes Received:
    1,253
    First Language:
    English
    Primarily Uses:
    RMXP
    Consider using the append mode, 'a', when writing to the file. That way you only need to use the file for relative short amounts of time. I dunno if you can use that option on File::write.

    *hugs*
     
    Acetonide likes this.
  5. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,312
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    @FenixFyreX

    I am unsure as to why my method wouldnt work ? i re-checked the definition of the "w+" arguement and it is defined as follows...

    "r+": Sets the read-write position to the beginning of the file."w+": The same as "r+", but if a file already exists when file is opened, the previous file's contents will be deleted. Therefore, each time you write to the file is should remove the previously created file and just re-write all data now stored in the $text variable?

    Anyone know why this wouldn't be the case? im confused now :p

    Edit:

    as Zeriab also mentioned,

    you could simply use File.open(doc_name, "a") { |d| d << "crap" }

    Note: File.open opens and then reads / writes (depending on arguments) and then closes the file. ( this only works if a block is passed to the method)
     
    Last edited by a moderator: Nov 16, 2013
  6. FenixFyreX

    FenixFyreX Fire Deity Veteran

    Messages:
    434
    Likes Received:
    307
    Location:
    A Volcano Somewhere
    First Language:
    English
    I'm sorry, my fault for my typo. I meant to write 'a' like Zeriab suggested. You aren't required to lug around a large String within a global variable when you do that, you simply append the text to the document. Thanks, Zeriab.

    Also, as I stated before, it is good practice to keep less objects in memory space as possible, even with Ruby. Actually, especially with Ruby. Ruby's garbage collector isn't the best and writing clean, tidy and organized code is much more preferable. While a few loose strings here and there (read: one loose File handle) won't affect performance, keeping all your objects in check can be the difference between 60 FPS and 6, but I digress; I'm just ranting now. :p

    Dekita, I did not intend to target you or your code; I'm simply trying to explain why managing your objects and implementing a bit of encapsulation is more preferable to outright global variables, that's all. :) Your code definitely would work.

    @File.read, if you modularize your code and write a few methods instead of needing to add text dynamically directly within the file handle, it'll help people understand your code as well as yourself further down the road
     
    Last edited by a moderator: Nov 16, 2013
  7. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,312
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    No worries, i did not feel targeted or anything like that.

    I was just a little confused as to why it would keep the existing text within [x] document when opening and using the "w" or "w+" to overwrite it. (it doesnt, i tested :p )

    Edit: also as Ice Dragon stated, its pointless to open the file with read capabilities if the whole file is being overwritten, so "w+" is less efficient than "w" :p
     
    Last edited by a moderator: Nov 16, 2013
  8. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    I only use this for debugging purposes, so I don't mind it a bit messy like that. I'd never release it with something like that in (unless I needed debug logs from beta testers as well).


    Thanks for the tips :)
     
  9. Kurisu

    Kurisu game dev, pilot, cat lover Veteran

    Messages:
    77
    Likes Received:
    18
    First Language:
    non-english
    Previously I wanted a script that calls an event on the map.

    For example:

    $game_map.events[id].set_graphic("character_name", character_index)Now there's a question: how could I temporarily stop this event from moving, without stopping all other event processing? If I tried using script equivalent of wait, it stopped everything including the player.
     
  10. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    What are you trying to accomplish, and why are you doing it via scripting?
     
  11. Kurisu

    Kurisu game dev, pilot, cat lover Veteran

    Messages:
    77
    Likes Received:
    18
    First Language:
    non-english
    Due to author's terms of use I cannot post any part of the script here. It's Khas' ABS system. Enemies there are visible on map as events.

    What I'm trying to do is make enemy "stunned" for a second or two when they get hit by a player. I just hoped it would be something simple but if it's complicated then it's ok :) Maybe I'll think of something else.
     
  12. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,312
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    You could try using...

    $game_map.events[id].lockwhen the event is hit, but you would also have to call...

    $game_map.events[id].unlockto allow the event to start processing again...

    Pretty sure that will work, not tried it though  :/
     
    Kurisu likes this.
  13. kingmakerspider

    kingmakerspider That little devil... Veteran

    Messages:
    309
    Likes Received:
    30
    Location:
    Oregon, USA
    First Language:
    English
    Okay peoples...I am using RMVX (not ACE), and I'm wanting to change when the actors health becomes 'in critical condition' from 25% to 50%... I've been looking all over the scripts to find out where this is defined and I can't seem to locate it. I was hoping someone familiar with RMVX and Ruby SS2 could help me out here and at least point me at the particular script and the lines where the game defines this. Or...I suppose if it's more difficult than I thought...some kind of work-around that would allow this. I have scripts that would change the battler sprite when critical...but I'd like to change what % of HP that is...I hope this makes sense and would appreciate the help. Thanks.
     
  14. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Kurisu, do you have to do this via scripts? If it were an event, I'd just turn on a self switch on the event to swap it to a new page with the graphic but no attack commands, and after X frames, turn the self switch off again. Or I'd start with that and work from there. To know how the events really behave, you'd need to give us a link to the script.


    @kingmakerspider, in Window_Base, the methods hp_color (line 205) and mp_color (line 214) return crisis_color if the actor's hp or mp is less than their max hp or mp / 4. To change it to 50%, just change the 4 to 2 on both of those lines.
     
    Last edited by a moderator: Nov 18, 2013
  15. Kurisu

    Kurisu game dev, pilot, cat lover Veteran

    Messages:
    77
    Likes Received:
    18
    First Language:
    non-english
    @Shaz sadly it has to be done via script, yes :) My request is blowing out of proportions though, I already feel bad for asking so many questions... You do have good idea with self switches, perhaps it would be possible to toggle a self-switch via script command? I will try it this way. There are also two scripts that seem to be expanding self-switch functionality, I might try using them too.
     
  16. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    $game_self_switches[[map_id, event_id, 'A']] = true/false


    is how you change self switches via script.


    If it's turning into a big thing, I'd suggest starting your own thread, because it probably no longer falls under the "... Questions that don't deserve their own thread" category anymore :)
     
    Kurisu likes this.
  17. kingmakerspider

    kingmakerspider That little devil... Veteran

    Messages:
    309
    Likes Received:
    30
    Location:
    Oregon, USA
    First Language:
    English
    Thanks Shaz! That helped me with half of it but using that as a clue I was able to fix it in all my combat scripts...for some reason they use different terminology than the original "Crisis" in some scripts. In the scripts I'm using, it ended up being defined as "pinch", go figure. But I figured it out thanks to you! I appreciate it.
     
  18. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Regular expressions are the bane of my life! Could someone help me out with this one, please?


    I want to take a string that contains any number of \v[x] patterns (where x is a number with 1 or more digits), and pull out an array of all of the numbers. The string may or may not have other text (including spaces) before, between and after these patterns.


    So both of the following


    test \v[1] test \v[2] test \v[3] test


    \v[1]\v[2]\v[3]


    will result in


    [1, 2, 3]


    Edit: nevermind ... got it, after much mucking around.
     
    Last edited by a moderator: Nov 19, 2013
  19. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,062
    Location:
    Toronto
    First Language:
    English
    patt = /\\v\[(\d+)\]/i


    str = %q[test \v[1] test \v[2] test \v[3] test]


    p str.scan(patt).flatten.map {|val| val.to_i}
     
    Zeriab and Shaz like this.
  20. Shaz

    Shaz Veteran Veteran

    Messages:
    37,712
    Likes Received:
    11,421
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    thank you :)
     

Share This Page