Home
Products
Community
Manuals
Contact
Login or Signup

Learning Object-Oriented Programming in Blitzmax

BlitzMax Forums/BlitzMax Tutorials/Learning Object-Oriented Programming in Blitzmax

John J.(Posted 4 years ago) #1
This tutorial is intended for programmers already familiar with BlitzMax's general syntax, but not with the concepts or uses of custom types or object-oriented programming techniques.

When finished reading this tutorial, you should have a good understanding of most of BlitzMax's OOP features:
* Type Definitions with Fields
* Type Definitions with Methods
* Inheritance
* Polymorphism
* Constructors & Destructors
* Static Methods & Fields

Not only does this tutorial teach you how to use these features, but where and why they should be used.

Download PDF (125 KB)


RepeatUntil(Posted 4 years ago) #2
Very very very good OOP tutorial!!! Ex-cel-lent!! I encourage other to read it...


assari(Posted 4 years ago) #3
Good job. I've added it to my resource link


Peter Scheutz(Posted 4 years ago) #4
Exellent and well written.
This really should be added to the official docs.


boomboom(Posted 4 years ago) #5
I agree. THis helped me finally get my head around OOP (even after reading loads of other tutorials on it)


John J.(Posted 4 years ago) #6
Thanks for the comments. I'm happy to hear the tutorial is helpful.


Why0Why(Posted 4 years ago) #7
I found it to be excellent. Thanks for posting.


Kurator(Posted 4 years ago) #8
very good work, thank you for spreading :)


bradford6(Posted 4 years ago) #9
John J.,

this is a thorough and excellent coverage of OOP in Blitzmax.

thanks!


TomToad(Posted 4 years ago) #10
This is great. It needs a sticky :)


GregBUG(Posted 4 years ago) #11
wow!! excellent tutorial!!!
thanks John!!!


computercoder(Posted 4 years ago) #12
I wanted to show my support as well for this very nice introduction into OOP with BlitzMax! Thank you very much for John :-)


David Scifa(Posted 4 years ago) #13
Fantastic learned alot from this tutorial.


Steve Elliott(Posted 3 years ago) #14
This tutorial should be made sticky, because it explains the how and why of OOP very well indeed - great stuff!


Wellmt(Posted 3 years ago) #15
Yes I second that Sticky Request.


North(Posted 3 years ago) #16
STICKY

Thanks a bunch!


Dabz(Posted 3 years ago) #17
this is one of the first tutorials i read for max,and knowing OOP from c++,it really helped me understand how it is incorporated within blitzmax.

thank you john


boomboom(Posted 2 years ago) #18
Right, one thing I don't understand. The end section on Static Functions.

I understand how the code works, I just don't understand the purpose of putting a function inside a type. Surely that is just a renamed 'method'?

What is the difference?

Also, when is it right to use a method, or a function? and when should you put a function insdie a type, as opposed to having it outside?


tonyg(Posted 2 years ago) #19
Functions relate to the type object while methods refer to an instance of it.
The most obvious function is create() as we don't have an instance when we use it. Createlist for example is a function relating to TList while Addlast is a method relating to a specific list.
A function is best put inside a type when it performs an action against that type.
The Beginner's Guide has a section on Functions/Methods which is better than this garbled reply I've hacked together.


boomboom(Posted 2 years ago) #20
thanks, where is this guide?

Edit: sorry, i see it now (top of page)


pappavis(Posted 2 years ago) #21
make this a sticky, plz.


chwaga(Posted 2 years ago) #22
just finished reading, very well written.

sticky!!


Pete Carter(Posted 1 year ago) #23
yeah it is well writen should be added to the docs with max

and yes sticky!


shaun freeman(Posted 1 year ago) #24
As a newbie to oop,and after reading all the tuts on polymorphism and casting, I am puzzled by how this works? code by mark incittis snake from
Gridwars 2(yeah I know everybody wants to do snakes ,including me!)
As there is no ref to arrays(which is how I would do it)

type nme6
field x,y,blah etc
field tail:nme6 ' object of type : Type var refering to same type??
field head:nme6

' this field var has a type declaration? type within type?

..then this...

function Create:nm6(x,y,length etc. )

'this function has a type???

..later there is this..

n.tail=nme6.create(.....)

(a field variable (within a type) = a value generated by a function(also within the same type)???

and finally this...

n.tail.head=n ????????? WTF!!is this advanced form of casting/polymorphism??
This is not covered in any tut Ive read
Can anyone explain this ?


end function


tonyg(Posted 1 year ago) #25
... sorry but I have read your post about 8 times and still have no idea what you're asking.
How about opening your own topic and posting the exact code you're after.
I *think* you're confused that an nme6 instance contains references to two other nme6 instances : Head and Tail.
I am guessing an nme6 instance is saved in variable 'n' and then a new nme6 instance saved into the 'tail' attribute on n...
n.tail=nme6.create(...) as nme6.create returns and nme6 instance.
It also seems that the nme6 instance pointed to by n.tail is updated so that it;s head attribute points back to 'n'.
Maybe if it was written like this it would be better :
firstnme6:nme6=nme6.create()
secondnme6:nme6=nme6.create()
firstnme6.tail=secondnme6
secondnme.head=firstnme6


shaun freeman(Posted 1 year ago) #26
Thanks tonyg
Im sorry I didnt explain clearly enough what Im trying to do..

I have looked at Gridwars 2 Code by Mark Incitti,and while it is not my
intention to copy other peoples code, I DO wish to learn from it and the
one enemy (Nme) type that features in his game that puzzles me is the snake.

The old way to create these was to create an array ( a list!) of coords for each part, which I used to do as long ago as 1982!
(add 1 to head,delete the tail),but as Im not 16 anymore Im finding it hard to get my head around the new oop constructs in blitzmax. ;)

I do understand that an object can be a number,string,etc but I had no idea that a type could contain fields REFERING TO THE PARENT TYPE FIELDS as it seems to do in this instance.

The line of code ,

n.tail.head= n

was the most confusing part as I have not seen this before , but like you suggest EACH INSTANCE (n) has a HEAD and a TAIL type instance contained therein.
It was just the way in which the following tail could *point* to the *head*
in front of it (or tail section!).

After reading through Waves excellent tut on OOP I worked out that

n.tail.head=n

means

n step into tail step into head = n (where n was the previous n.tail instance?

Thanks for your help I shall look into this further


TomToad(Posted 1 year ago) #27
You are basically creating a linked list. Each segment points to a parent link (head) and a child link (tail). When you create the snake, you create the first segment with n = nme6.Create(). Then the next segment is created and n.tail is set to point to it.
n.tail = nme6.Create()
now n.tail points to the second segment, but you need to point the second segment's head back to the first segment.
n.tail.head = n
Let me break that down.
n is the instance of the first segment
n.tail is the instance of the second segment
n.tail.head is the head field of the second segment which now needs to point to the first segment.
So now the two segments look like this

Segment1
Tail: Points to Segment1
Head:Points to Null (not yet defined)
Segment2
Tail:Points to Null(not yet defined)
Head:Points to Segment 1

After adding the third segment, it will look like this

Segment1
Tail: Points to Segment2
Head: Points to Null
Segment2
Tail Points to Segment3
Head Points to Segment1
Segment3
Tail: Points to Null
Head: Points to segment2


shaun freeman(Posted 1 year ago) #28
That has to be the most conscise explanation I have ever read.
Thanks Tomtoad, As a fairly basic programmer , I guess this oop stuff throws up some strange concepts,but then thats what makes things interesting!
I think the null object references(empty objects?) were causing me some confusion also,but then they had to be created first so the following instances could refer to them? no?

Thanks for taking the time guys.Much appreciated.