Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 RGSS scripting dissections and explinations
#79
Before I begin discussing more about methods, I think we should tackle one more type of variable.

You've already learned about $global values, variables so powerful that they can be used throughout your project... but also use more memory resources and runs a risk of being overwritten.

You've just learned about @instance values, variables that are unique and only get shared throughout a single class... uses less memory resources and lets you stay a bit more organized.

Now to learn about local variables!!!

Local variables are extremely short-ranged variables and only operate within the method they were created. I could make five methods with a local variable called "julius", and the value of julius won't be shared between them without a lot of effort.

Code:
def add_values
  my_result = 4 + 2
end

def mult_values
  my_result = 4 * 2
end

def divide_values
  my_result = 4 / 2
end

Very basic as examples, these three perform different math functions and the result shows up in the 'my_result' local variable. And when each runs, the my_result value begins as an empty variable holding absolutely NOTHING. So if I was to first run the mult_value method, and then run the add_values method, the value of my_result when I start running the add_values method is absoluely nothing until I add 4+2.

It may seem pretty short-sighted. But actually, local variables are easy to use and you can use the same simple names over and over among multiple methods.

Code:
#--------------------------------------------------------------------------
  # * Determine [Can't Evade] States
  #--------------------------------------------------------------------------
  def cant_evade?
    for i in @states
      if $data_states[i].cant_evade
        return true
      end
    end
    return false
  end
  #--------------------------------------------------------------------------
  # * Determine [Slip Damage] States
  #--------------------------------------------------------------------------
  def slip_damage?
    for i in @states
      if $data_states[i].slip_damage
        return true
      end
    end
    return false
  end
  #--------------------------------------------------------------------------
  # * Remove Battle States (called up during end of battle)
  #--------------------------------------------------------------------------
  def remove_states_battle
    for i in @states.clone
      if $data_states[i].battle_only
        remove_state(i)
      end
    end
  end

These three methods are in the Game_Battler class. And guess what? Each has a local variable that all three use! See that letter 'i'? That 'i' is a local variable. Oh, they could have given it another name, but they went with just one letter. In all three methods, the 'i' is a variable that acts like a counter or number-holder. They could have called it index_value with statements like "for index_value in @states", but that might have made too MUCH sense. And I won't go further on what these methods are even doing. Just note that they all use a local value and none of these methods share the value of this local variable between them.


So the most powerful is the Global variable (works throughout the largest area)
The next powerful is the Instance variable (works throughout a class)
The least powerful is the Local variable (stuck to the method it's in)
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png] [Image: liM4ikn.png] [Image: fdzKgZA.png] [Image: sj0H81z.png]
[Image: QL7oRau.png] [Image: uSqjY09.png] [Image: GAA3qE9.png] [Image: 2Hmnx1G.png] [Image: BwtNdKw.png%5B]
Above are clickable links

Reply }


Messages In This Thread
RE: RGSS scripting dissections and explinations - by DerVVulfman - 11-17-2018, 04:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Help iwth script (RGSS Player crash) Whisper 3 7,674 06-17-2017, 05:03 PM
Last Post: Whisper
  How can I use the cmd of "require" in rgss superegp 2 5,384 11-03-2015, 06:16 AM
Last Post: kyonides
   Scripting in VX vs VX Ace Miharu 5 8,207 02-21-2015, 10:10 AM
Last Post: Taylor
   Combat animations via scripting; How? ZeroSum 2 4,572 09-20-2013, 06:58 PM
Last Post: ZeroSum
Question  RGSS stoped to work Chaos17 5 6,930 02-14-2013, 05:13 PM
Last Post: DerVVulfman
   Ruby, RGSS & General Code Discussion Kain Nobel 6 9,916 12-22-2012, 05:11 AM
Last Post: MechanicalPen
   [Request] Tut. for RGSS Eldur 9 10,612 12-07-2012, 04:27 AM
Last Post: DerVVulfman
   [ASK-RGSS] Behemoth's CBS alike Getsuga_kawaii 0 3,864 04-29-2010, 03:07 PM
Last Post: Getsuga_kawaii
   Scripting I think spazfire 7 8,984 04-12-2010, 03:21 AM
Last Post: DerVVulfman
   Beginner Scripting Tuts? KDawg08 1 3,685 03-31-2010, 11:03 PM
Last Post: Hsia_Nu



Users browsing this thread: