Posts: 4,607
Threads: 543
Joined: Dec 2009
Once upon a time, in a very far away sea, there was a hungry shark named $hark who kept lurking around searching for little @fish to bite about 50 times each. Little @fish knew it was better to wait for the best moment to get out of their hiding places so they could find some delicious plancton to eat. If there was any sign of $hark surrounding the area, little @fish would go home immediately. The same happens whenever the plancton is scarce in any particular region of the sea.
By the way, I am a shark or that is what some people made me think.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Posts: 325
Threads: 9
Joined: Feb 2016
11-13-2018, 08:57 PM
(This post was last modified: 11-13-2018, 08:58 PM by Siletrea.)
(11-11-2018, 09:28 PM)DerVVulfman Wrote: Full understanding of what wings are, type of wings... joint coordinate with each other????
OMG! I think Siletrea read the Frank Herbert novel 'Children of Dune' as she basically creating her own version of the Bene Gesserit summation of the 'The Union of Opposites'.
And yes, when you make a Child class, you can essentially 'whiteout' a method copied over from the Parent class. But you do that by making your own version of that method. If the parent class has a 'get_silly' method, the child must too have a 'get_silly' method.
Okay, the Superclass thing...
Like I just said, your Child class must have the same methods as the Parent class, even if the Child rewrites the Parent's methods. But that doesn't give you much flexibility. That would only ensure that your child is a literal XEROX of the parent without any new features.
The whole thing about the Superclass is that you can create a new method in the Child class and quickly copy the stuff from the old method into it. Basically, the use of the super() statement is a shortcut so you don't have to manually enter every single item from the parent's version. It does that work for you.
If the parent class has a method called 'punch_freddy' and it looks like this:
Code: def punch_freddy
@target = @freddy
@target.hp -= 20
@target.damage = 20
@target.flash = white
@target.falls = true
end
You can use the super() statement in your child class to make a new 'punch_freddy' method like this:
Code: def punch_freddy
super()
@target.woozy = true
@target.screams = true
end
This saves some space in your code, so your child class doesn't have to rewrite everything like this:
Code: def punch_freddy
@target = @freddy
@target.hp -= 20
@target.damage = 20
@target.flash = white
@target.falls = true
@target.woozy = true
@target.screams = true
end
Looks like the new code with the super() statement only has three lines including the super() statement. Certainly seems better than remaking the method with seven lines, right?
And let's face it, this is a small example. Some methods you might be inheriting are HUMONGOUS! WHO WANTS TO REWRITE A 30-LINE METHOD WHEN THROWING A super() IN ITS PLACE CAN BE DONE?
YES, def means define. It is a statement that defines the start of a method. You use the 'class' statement to define the start of a class, and a 'module' statement to define the start of a module. You use the def statement so frequently because you make a lot of methods when writing code.
Again, it's heiarchy: A module can hold multiple classes, and classes can hold multiple methods... all a tree of sorts. Or a shrub if the guy writing the code is a total mess. ^_^
Now why use the ampersand (@) to define a variable rather than the dollar sign ($)... First, let's talk about what we call them.
The variables with the $ symbol are called 'global' values. They work throughout the whole system. Not just one code, but could be referenced throughout your entire SCRIPT LIBRARY!!!! WOOO! POWER. But it has drawbacks. It's a memory hog, using more resources than instance variables (next on the agenda). Also, it's riskier. If one script uses a $global variable of a particular name and another script uses a $global variable of the same name, they could wipe each other out!
The variables with the @ symbol are called 'instance' variables. They are defined to work specifically within their class. And because they are class specific, you can have two classes with instance variables of the same name. So if two different classes have a @my_name variable, they each can have different values set to each of them. One won't overwrite the other. Well, not without effort on the scripter's part. And they use less memory resources.
So while the $global variable has more reach, the @instance variable allows better coding.
ok firstly I have no idea what a XEROX is...and secondly...the superclass thing...its a way of...adding new things into a prexisting system without having to go back to the old script right?
the @ symbol still confuses me but I understand the $ alot more now! thanks!
new logo for Yesteryear created by Lunarberry!
Posts: 11,239
Threads: 649
Joined: May 2009
The @ symbol is confusing? Well, it basically means that the variable belongs to THAT particular class.
If I made a class called Shelley and a class called Miriam, each might have a variable called @hair_color. I could set the @hair_color variable in the Shelley class to ' sandy_blonde', and set the @hair_color variable in the Miriam class to ' auburn_brown '. Each has its own variable, even though they have the same name. If I tried using the $hair_color value in either class, one would overwrite the other.
STUDY TIME: Look at any of the initialize methods in the default classes. They tend to have @instance variables belonging just to them.... mostly.
Oh, wow. Um... Yeah. Xerox is the manufacturer of a a popular office copy machine. In fact, it was the essential STANDARD for copiers. So back in the day, we just called copiers 'Xerox', just like how we call adhesive bandages 'Bandaids'
Hey, I use phrases like 'oft' for often and other old phrases from before I was borne. BUT I don't think I have ever used 'nary'.
So when I said "that your child is a literal XEROX of the parent", I meant it was a near-perfect copy or duplicate. And well, yeah. The super() statement is a way of letting you add new content to an existing method. It's just that the super() statement "IS" the old content and you're writing the new content around it.
Code: def goofy_method
blah_blah_blah_new_statement1
blah_blah_blah_new_statement2
super()
blah_blah_blah_new_statement3
blah_blah_blah_new_statement4
end
We could just say, we're taking the original 'goofy_method' method and adding two more statements to the start, and two more statements when the original method would have been done.
TEST TIME: So, show me an example of a method using the super() statement. Just an interpretation of what you think it means. This is a no pressure thingie. Just a gauge for discussion to see how I'm doing as a teacher and stuff. More like a test on ME.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
Above are clickable links
Posts: 4,607
Threads: 543
Joined: Dec 2009
11-14-2018, 10:02 AM
(This post was last modified: 11-14-2018, 10:10 AM by kyonides.)
Let's make the board's mockup class now!
Code: class Forum
def initialize
@name = ""
@url = ""
@status = "OK"
@admins = [ ]
@members = [ ]
@sections = { "Announcements and Updates" => [ ] }
end
def name() @name end
def status() @status end
def url() @url end
alias address url
def admins() @admins end
def members() @members end
def sections() @sections end
def name=(new_name) @name = new_name end
def status=(new_status) @status = new_status end
def url=(new_url) @url = new_url end
alias address= url=
end
Now we're gonna create our forum...
$save_point = Forum.new
$save_point.name = "Save-Point"
$save_point.address = "https://www.save-point.org"
Mmm it looks fine so far but we do need people populating it, don't we?
Code: class ForumMember
def initialize
@name = ""
@legend = ""
@title = ""
@number_of_posts = 0
@posts = [ ]
@thanks_given = 0
@last_visit = Time.at(0)
@hobbies = [ ]
@status = "Average User"
end
attr_accessor :name, :legend, :title, :number_of_posts, :thanks_given
attr_accessor :last_visit, :status
attr_reader :posts, :hobbies
end
So let's go make new forum members!
@Siletrea = ForumMember.new
@Siletrea.name = "Siletrea"
@Siletrea.legend = "Making a game about a forgotten robot"
@Siletrea.title = "YesterYear developer"
@Siletrea.hobbies = ["Play Ace Attorney games too many hours a day", "Reading Dune series of books?", "Dream on making robots", "Love Spyro just because", "3D animation", "Collect stray cats"]
@Kyonides = ForumMember.new
@Kyonides.name = "Kyonides"
@Kyonides.legend = "A lazy scripter that needs to get some sleep"
@Kyonides.title = "Dark Scripter"
@Kyonides.hobbies = ["Making scripts", "Compiling stuff", "Use Linux distros", "Pick on Siletrea because it's fun!", "Laughing at Spyro", "Calling PodPod Evil PodPöd", "Ignore evil bounter hunters", "Sadly talking to an Argentinian that prefers to use floats and the to_i method instead of multiplying by m and later dividing by 100"]
Adding new members to the forum
$save_point.members << @Siletrea << @Kyonides
But there is no point in registering members only so let's add some admins as well!
Code: class ForumAdmin < ForumMember
def initialize
super
@status = "Administrator"
end
end
@DerVVulfman = AdminMember.new
@DerVVulfman.name = "DerVVulfman"
@DerVVulfman.legend = "Looking as furry and ugly as Dilgear"
@DerVVulfman.title = "Administrator"
@DerVVulfman.hobbies = ["Eating ramen just because", "Skipping lunch almost every single day", "Talk about Doctor Who like a parrot", "Collect stray cats"]
@Kirito = AdminMember.new
@Kirito.name = "Kirito"
@Kirito.legend = "I randomly post stuff on the board unless I broke something!"
@Kirito.title = "Technical Administrator"
@Kirito.hobbies = ["Making chat bots that only know how to bother innocent sharks but lick anybody else's face instead"]
Let's add those new members to the forum's list of admins.
$save_point.admins << @DerVVulfman << @Kirito
Updating the forum status...
$save_point.status = "Avatars are broken, thank Kirito for it :P"
Think that super or super() or super(argument1, argument2) and etc. are like a Siletrea that runs to her mother crying a lot because some guy named Kyonides laughed at Spyro the dragon. It's like asking your mother to do you a favor (no arguments needed here) or updating her about what has happened if super gets some arguments like. i.e.
Code: class SiletreasMom < Person
def being_picked_on(report)
@report = report
cry_out_loud_at_the_person_who_is_bothering_her_daughter
end
end
class Siletrea < Person
def being_picked_on(report)
super(report)
end
end
@siletrea = Siletrea.new
report = "Kyonides is a meanie! He dared to laugh at Spyro the dragon!"
@siletrea.being_picked_on(report)
At the same time it's also what wulfo said, a way that prevents you from typing the same stuff all over again because you got stuff from your mother like, err, hair and eye color, height, a couple of her old books, dunno what else...
@instance variable... Why don't we define the term instance first? Let's check what the search box tells us about it.
Quote:in·stance
/ˈinstəns/
noun
noun: instance; plural noun: instances
Case 1
an example or single occurrence of something.
"a serious instance of corruption"
synonyms:
example, exemplar, occasion, occurrence, case;
illustration
"an instance of racism"
Case 2
a particular case.
"in this instance it mattered little"
It's not extremely simple so let's go rephrase it a little bit...
Case 1: Something I can use to teach something to Siletrea or anybody else or anything I can use as a warning in case they are about to make a terrible mistake or somebody that is practically flawless and other people should follow that very same person or a case of a corrupt politican that steals money or pollutes a river while other local politicians haven't done such terrible things before, etc. As a summary it is linked to number one 1 alias picking a single thing.
Case 2: Being unique or specific to a person or some stuff, where specific stands for something that describes that person or thing but not another. A good example would be me picking on you while nobody else like wulfo or lani or kirito does the same here.
So what's an instance variable?
A unique or specific trait or feature that is included in a class and will be sent to a specific copy of that class but not to another unless you pass the same value on purpose.
Code: class Female
def initialize
@name = "Some girly girl's name"
end
def name() @name end
def name=(new_name) @name = new_name end
end
@siletrea = Female.new
@siletrea.name = "Siletrea"
@another_girl = Female.new
@another_girl.name = "Siletrea"
In that case both girls would call themselves Siletrea but only because I did it on purpose.
@another_girl.name = "Puma"
Now the other girl is your cat Puma!
Well, that's possible only because I didn't define Female as a human exclusive class so female cats are also included by default. So if we compare both names we can quickly notice that their names are unique, different, specific, not a simple photocopy.
Warning!
If I go pay a visit to another script, both @siletrea and @another_girl would mean nothing to it so it will tell you they can't be found anywhere. Instance @variables might disappear if they aren't stuck to anything that won't be forgotten by the game any time soon. One "solution" would be using the dollar sign $, which in Siletrea's own dialect it might be called the bunny ear. Making a global variable $girls = [@siletrea, @another_girl] will store them till the game is closed by the player. A better method could be using $game_temp or $game_system or $game_party or even $game_actors to store them via a method (the def guys we keep typing here), just pick a good name for that new method. One could also create a new module like:
Code: module Girls
def Girls.add(*girls) @add += girls end
@add = [ ]
end
Girls.add(@siletrea, @another_girl)
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Posts: 11,239
Threads: 649
Joined: May 2009
11-14-2018, 05:08 PM
(This post was last modified: 11-14-2018, 05:12 PM by DerVVulfman.)
Mr. Potato Head from the movie WARGAMES ©1983
A movie about a gaming hacker who didn't realize he broke into NORAD to play the game 'Globalthermonuclear War'
Yeah... um.. Siletrea? Let's ignore him for now. I don't want you to go through sensory or information overload when you're just starting out. And I do want to know how much you comprehend on the current discussion.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
Above are clickable links
Posts: 325
Threads: 9
Joined: Feb 2016
11-14-2018, 09:15 PM
(This post was last modified: 11-14-2018, 09:16 PM by Siletrea.)
(11-14-2018, 04:05 AM)DerVVulfman Wrote: The @ symbol is confusing? Well, it basically means that the variable belongs to THAT particular class.
If I made a class called Shelley and a class called Miriam, each might have a variable called @hair_color. I could set the @hair_color variable in the Shelley class to 'sandy_blonde', and set the @hair_color variable in the Miriam class to 'auburn_brown '. Each has its own variable, even though they have the same name. If I tried using the $hair_color value in either class, one would overwrite the other.
STUDY TIME: Look at any of the initialize methods in the default classes. They tend to have @instance variables belonging just to them.... mostly.
Oh, wow. Um... Yeah. Xerox is the manufacturer of a a popular office copy machine. In fact, it was the essential STANDARD for copiers. So back in the day, we just called copiers 'Xerox', just like how we call adhesive bandages 'Bandaids'
Hey, I use phrases like 'oft' for often and other old phrases from before I was borne. BUT I don't think I have ever used 'nary'.
So when I said "that your child is a literal XEROX of the parent", I meant it was a near-perfect copy or duplicate. And well, yeah. The super() statement is a way of letting you add new content to an existing method. It's just that the super() statement "IS" the old content and you're writing the new content around it.
Code: def goofy_method
blah_blah_blah_new_statement1
blah_blah_blah_new_statement2
super()
blah_blah_blah_new_statement3
blah_blah_blah_new_statement4
end
We could just say, we're taking the original 'goofy_method' method and adding two more statements to the start, and two more statements when the original method would have been done.
TEST TIME: So, show me an example of a method using the super() statement. Just an interpretation of what you think it means. This is a no pressure thingie. Just a gauge for discussion to see how I'm doing as a teacher and stuff. More like a test on ME.
ok so the @ symbol helps to organize things! even if they have the same name! as long as its in a different line of code it can help differentiate things?
hmm the "super" statements... its like small bakesale!
the original list of items for sale would be muffins and cupcakes and brownies! BUT somewhere down the line a new seller in the bakesale comes in with cookies and tarts! instead of going to the computer and building up a whole new list from scratch and then having to print everything out and replace the old sign they just take a pen and add the new items on at the bottom of the list!
right?
new logo for Yesteryear created by Lunarberry!
Posts: 11,239
Threads: 649
Joined: May 2009
Bottom or TOP actually. I'll be back tonight with the NEXT lesson about the super() statement.
UP! UP! AND AWAAAAYYYY!!!!
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
Above are clickable links
Posts: 4,607
Threads: 543
Joined: Dec 2009
11-15-2018, 04:51 AM
(This post was last modified: 11-15-2018, 04:58 AM by kyonides.)
Siletrea Wrote:hmm the "super" statements... its like small bakesale!
the original list of items for sale would be muffins and cupcakes and brownies! BUT somewhere down the line a new seller in the bakesale comes in with cookies and tarts! instead of going to the computer and building up a whole new list from scratch and then having to print everything out and replace the old sign they just take a pen and add the new items on at the bottom of the list!
right?
$save_point.members << @Siletrea << @Kyonides
Girls.add(@siletrea, @another_girl)
Those lines would be quite similar to what you described above, even if they handle people or cats.
Continuing with your bakery example...
Code: class Business
def initialize
@open_store = true
end
def prepare_products
@list_of_available_products = [ ]
end
end
Code: class FlourDistribuitor < Business
def prepare_products
super
@list_of_available_products << "Flour"
end
def deliver_product
@received_phone_call = true
@loaded_truck = true
@drive_off_to_bakery = true
@leave_flour_there = true
end
end
Code: class Bakery < Business
def prepare_products
super
@list_of_available_products << "Last Bread" << "Last Muffin"
@new_list_of_ingredients = ["Flour", "Sugar"]
@clean_tools = true
end
def call_flour_distributor
@call_flour_distributor = true
@send_them_list_of_ingredients = true
end
end
Code: @flour_distributor = FlourDistributor.new
@puma_bakery = Bakery.new
@puma_bakery.prepare_products #=> Siletrea finds out she needs more flour!
@puma_bakery.call_flour_distributor #=> She calls the flour distributor then
@flour_distributor.prepare_products
@flour_distributor.deliver_product
As you can see both the flour distributor and Puma's Bakery are businesses so they both need to do one thing in common, to prepare their products for distribution or sale at the store. There we called super to let both of them create a new list of products available at the time. It's like a distributor and a bakery sharing the same purchase / stock software like some basic system based on Excel? to make lists. Take into account that both have their own procedures meaning they don't need to do the same steps another business did. So super allowed us to complete a series of required steps to keep both businesses running smoothly.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Posts: 11,239
Threads: 649
Joined: May 2009
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.
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...)
Above are clickable links
Posts: 325
Threads: 9
Joined: Feb 2016
Hmmm so global$ accesses everything but is clumsy and takes to much resources to run
Instance @ accesses everything within its script andor scripts connected with Pac-Man<
Local can only work within one closed off section of its script and is used mostly for clarification or “if” statements
Ok... the shark has also made a reasonably understandable example with superclass as well so thanks for that
But what does double Pac-Man mean? << does it accesses the entire tree instead of just the prior script?
new logo for Yesteryear created by Lunarberry!
|