11-17-2018, 04:37 AM
(This post was last modified: 11-17-2018, 04:38 AM by DerVVulfman.)
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.
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.
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)
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)