Tried my first Ruby script edit. Blew up in my face.

So, I decided to try and see if I could edit a script for some more utility. I already got experience with some other programming languages, so I figured I could probably find out how to do it by reading through the script, and checking some tutorials, but it didn't work.

The script I tried to modify was this one: http://www.theolized.com/2013/11/theo-enemy-swapper.html

And what I did was add this method:

def load_swap_rate_notetag if line =~ /<swapping rate: (.*)>/i if rand(100) <= $1.to_i return true else return false end end endHere (Stuff I added are between <'s and >'s)

Code:
class RPG::Enemy < RPG::BaseItem  attr_accessor :swap  <def load_swap_rate_notetag    if line =~ /<swapping rate: (.*)>/i      if rand(100) <= $1.to_i        return true      else        return false      end    end  end>    def load_swap_notetag    @swap = []    note.split(/[\r\n]+/).each do |line|      if line =~ /<swap:[ ]*(.*)>/i        @swap = $1.to_s.split(/,/).collect do |enemy_id|           <if load_swap_rate_notetag>            id = enemy_id.to_i            next if id == 0            id          end        end.compact      end    end  end  end
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


And? What happened? I assume there was an error message? What did it say?


You added a new if command - did you also add the corresponding end? Did you add it in the right place?


You're pretty much leaving us guessing here.
In your method you are using a variable called line that is never made. (line is made in the method you did not make but it is only in the scope of that method thus your method can't use it). There might be more errors in your code but I currently can't run through it all. Hopefully this helps though. 😉
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.

Thank you for moving it to the Script Support. I hadn't even seen it!

In your method you are using a variable called line that is never made. (line is made in the method you did not make but it is only in the scope of that method thus your method can't use it). There might be more errors in your code but I currently can't run through it all. Hopefully this helps though. 😉
It did help! It now works (almost).

All I need now is to find out how to make it loop through an array...

EDIT: Making it loop through the array doesn't exactly work out for me.

The code in the edited, but still original method now looks like this:

def load_swap_notetag @swap = [] note.split(/[\r\n]+/).each do |line| if line =~ /<swap:[ ]*(.*)>/i @swap = $1.to_s.split(/,/).collect do |enemy_id| for i.each do j if load_swap_rate_notetag id = enemy_id.to_j next if id == 0 id else id end break end.compact end end endIt gives me an error at Line 67 (one line below end.compact), with this message:

[IMG]http://i.imgur.com/Vq3xzn6.png[/IMG]
def load_swap_notetag @swap = [] note.split(/[\r\n]+/).each do |line| if line =~ /<swap:[ ]*(.*)>/i @swap = $1.to_s.split(/,/).collect do |enemy_id| for i.each do j #remove the for loop if load_swap_rate_notetag id = enemy_id.to_j #remove the .to_j (change to .to_i) .to_i converts the enemy_id to an integer next if id == 0 id else #remove the else and the second id id end break end.compact end end endHere's what is going on in the above code:

Spoiler
def load_swap_notetag #this defines a method
Code:
@swap = []   #this creates an instance array
Code:
note.split(/[\r\n]+/).each do |line| #this starts a loop through each line in the note box
Code:
if line =~ /<swap:[ ]*(.*)>/i #if the line matches this regular expression then do the following
Code:
@swap = $1.to_s.split(/,/).collect do |enemy_id| #this takes the match turns it into# a string splits the string into an array collects all the elements of the array storing# the result in the instance array variable called @swap and iterates through each element of that# array
Code:
for i.each do j #your line this is stating for each element of the variable i# (variable i is not created) do j
Code:
if load_swap_rate_notetag #if load_swap_rate_notetag equals true do the following   id = enemy_id.to_j     # id equals enemy_id which you are trying to convert to something                          # .to_j to my knowledge doesn't exist in ruby   next if id == 0        # if the id equals 0 go to the next id   id                     # return the id else                     # this isn't doing anything id                       # this never gets calledend                       # ends your if statement break                    #breaks out of the innermost loop
Code:
end.compact         #removes all nil elements from the array and ends the split loop                    # the other three ends are pretty straight forward
In the method you made:

def load_swap_rate_notetag note.split(/[\r\n]+/).each do |line| #add this line if line =~ /<swapping rate: (.*)>/i if rand(100) <= $1.to_i return true else return false end end endend #add this end*note that unless you called this method somewhere it will never run.

Anyway hope my verbose explanation helps let me know if you encounter anymore issues. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/tried-my-first-ruby-script-edit-blew-up-in-my-face.42941/