Object oriented concepts

Started by Blizzard, August 06, 2008, 08:07:00 pm

Previous topic - Next topic

Blizzard

August 06, 2008, 08:07:00 pm Last Edit: August 06, 2008, 08:09:41 pm by Blizzard
I hope that I'm not getting lazy in my old days. Anyway... mumerus asked me something about scripting since he wants to learn how to script. I decided to explain him a bit more how it works so I explained him the object oriented concept. I explained objects, classes, properties (instance variables) and behavior (methods). I think that it would be a shame not to share it. I know, it's not as neat as you're used to from me, but I lack the time to make this into a real tute. And if I did that, I would explain it more thoroughly which would complicate things, make the tutorial awesome, but eat away even more of my time. Alright, enough talking, here it goes.

Spoiler: ShowHide
Quote[01:39] mumerus: Clarify this for me
[01:39] mumerus: Weapon Name Syntax:

$data_weapons[weapon_id].name
Example:
$data_weapons[1].name
[01:40] mumerus: It can be like
[01:40] mumerus: $data_weapons[1].Idiot's_Sword?
[01:40] Blizzard: not really
[01:41] Blizzard: ok, this is how it works
[01:41] Blizzard: imagine we have a "variable" named icon
[01:41] Blizzard: now let's see what we have
[01:41] mumerus: k
[01:41] Blizzard: we have an object
[01:41] Blizzard: we wonder what's the icons's size
[01:41] Blizzard: so we say
[01:41] Blizzard: icon.size
[01:41] Blizzard: see
[01:42] Blizzard: objects can be considered as real-life objects
[01:42] Blizzard: and they have properties
[01:42] Blizzard: llike size
[01:42] Blizzard: color
[01:42] Blizzard: smell
[01:42] Blizzard: taset
[01:42] Blizzard: imagine food
[01:42] Blizzard: now, food is not an object
[01:42] Blizzard: it's a class
[01:42] Blizzard: it's a type, a shape
[01:42] Blizzard: like
[01:42] Blizzard: Pizza is food
[01:42] Blizzard: Hamburger is food
[01:42] Blizzard:
class Food
[01:43] Blizzard:
class Pizza < Food
[01:43] Blizzard:
class Hamburger < Food
[01:43] Blizzard: that's how it works
[01:43] mumerus: I see
[01:43] Blizzard: now, a Pizza can have some properties
[01:43] Blizzard: let's take the radius
[01:43] Blizzard: se we say
[01:43] Blizzard:
class Pizza < Food
   attr_reader :radius
end
[01:44] Blizzard: let's make something like a default pizza
[01:44] Blizzard: like
[01:44] Blizzard: a Pizza that always has a radius of 30 cm
[01:44] Blizzard:
class Pizza < Food
   attr_reader :radius
   def initialize
     @radius = 30
   end
end
[01:45] Blizzard: now we can create a pizza and store it into a variable
[01:45] Blizzard:
xyz = Pizza.new
[01:45] Blizzard: when we use this now
[01:45] Blizzard:
p xyz.radius
[01:45] Blizzard: a window will appear telling us the radius of the pizza (which is 30)
[01:46] Blizzard: let's now create a pizza where we can say what radius we want at the very beginning
[01:46] Blizzard:
class Pizza < Food
   attr_reader :radius
   def initialize(rad)
     @radius = rad
   end
end
[01:46] Blizzard: using PIzza.new now will give us an error (argument error, 0 for 1)
[01:46] Blizzard: which means
[01:46] Blizzard: PIzza requires some data and information to be created
[01:47] Blizzard: so we need to use
[01:47] Blizzard:
ttt = Pizza.new(25)
[01:47] Blizzard: if we use now
[01:47] Blizzard:
p ttt.radius
[01:47] Blizzard: we will get 25 as result
[01:47] Blizzard: and there is a third alternative
[01:47] Blizzard: let's say we want pizzas with variable sizes, but a default of 35
[01:48] Blizzard:
class Pizza < Food
   attr_reader :radius
   def initialize(rad = 35)
     @radius = rad
   end
end
[01:48] Blizzard: now we can try something like
[01:48] Blizzard:
a = Pizza.new(40)
b = Pizza.new
[01:48] Blizzard: we will get no error
[01:48] Blizzard:
p a.radius
p b.radius
[01:48] Blizzard: this will give us 40 and 25 respectively
[01:49] Blizzard: since we said that we wanted Pizza a to be created with a radius of 40
[01:49] Blizzard: and Pizza b didn't get any information so we have a pizza of 35
[01:49] Blizzard: well
[01:50] Blizzard: I just explained you the concept of creating classes, objects and what properties are
[01:50] Blizzard: I'll put something else on top
[01:50] Blizzard: behavior
[01:51] Blizzard:
class Pizza < Food
   attr_reader :baked
   def initialize(rad = 35)
     @radius = rad
     @baked = false
   end
   def bake
     @baked = true
   end
end
[01:51] Blizzard: here's the code and what we will get
[01:51] Blizzard:
pizza = Pizza.new
p pizza.baked
pizza.bake
p pizza.baked
[01:51] Blizzard: we created a PIzza
[01:52] Blizzard: it was initialized with @baked as false
[01:52] Blizzard: that means the first time "false" will be printed on the window
[01:52] Blizzard: then we called the "method" named "bake"
[01:52] Blizzard: (that's what "def" is for: define method with name "bake")
[01:53] Blizzard: when we called the second printing
[01:53] Blizzard: we got "true"
[01:53] Blizzard: because the method "bake" was executed first
[01:53] Blizzard: and it changed the variable
[01:53] Blizzard: that's basically it
[01:53] Blizzard: that's some knowledge to start with
[01:54] Blizzard: you define instance variables (the ones with the prefix @)
[01:54] Blizzard: and you define methods
[01:55] Blizzard: which pretty much simulates the behavior of the object
[01:55] Blizzard: it's like real life
[01:55] mumerus: Hm
[01:55] Blizzard: the difference is that you can but don't have to go into details
[01:55] Blizzard: we have a very simple pizza here
[01:55] Blizzard: we could have complicated it endlessly
[01:55] Blizzard: creating other classes
[01:55] mumerus: After a big talk with Ulta.. thats gonna hurt me
[01:55] Blizzard: for stuff you put on the pizza
[01:55] Blizzard: lol
[01:56] Blizzard: I think I'll post this as a messy tute
[01:56] mumerus: Yeah
[01:56] mumerus: Im starting to get it little by little
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

dark-archangel

Hmm Bliz the tutorial si nice,but I think it could be awesome if you could make a video tutorial,an maybe...put it on youtube.
I think a lot of people will appreciate your explanations ;)

Blizzard

How am I supposed to make a video tutorial about theory?!
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

Not a real video, kind of like a PPT. Anyway, I explained all this to a classmate of mine just a couple of days ago. Strange thing is, he needed to know what goes on in the RAM to understand it properly...
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

Actually the heap-stack concept is important, too. Especially when comparing objects.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

I'll remember that, we're gonna have more sessions, he still didn't get all of it.
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Quintinius

I'm pretty sure that there is a lot of people that are gonna  appreciate what you are doing.
I'm just way too lazy to ever attempt scripting.  :^_^':

Berans

I like the explanations there, very down-to-earth and understandable...
Scripting is an evil thing though...it sucks you in and makes you spend more time than you have fixing up the tiniest little bugs :P

Blizzard

August 08, 2008, 01:28:34 pm #8 Last Edit: August 08, 2008, 01:30:20 pm by Blizzard
Quote from: Berans on August 08, 2008, 01:20:00 pm
Scripting is an evil thing though...it sucks you in and makes you spend more time than you have fixing up the tiniest little bugs :P

I can write a book about that. -_-
... OH SHIT, I ALREADY HAVE! :negative:
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.