Learning Object-Oriented Programming in Blitzmax
BlitzMax Forums/BlitzMax Tutorials/Learning Object-Oriented Programming in Blitzmax
| ||
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) |
| ||
Very very very good OOP tutorial!!! Ex-cel-lent!! I encourage other to read it... |
| ||
Good job. I've added it to my resource link |
| ||
Exellent and well written. This really should be added to the official docs. |
| ||
I agree. THis helped me finally get my head around OOP (even after reading loads of other tutorials on it) |
| ||
Thanks for the comments. I'm happy to hear the tutorial is helpful. |
| ||
I found it to be excellent. Thanks for posting. |
| ||
very good work, thank you for spreading :) |
| ||
John J., this is a thorough and excellent coverage of OOP in Blitzmax. thanks! |
| ||
This is great. It needs a sticky :) |
| ||
wow!! excellent tutorial!!! thanks John!!! |
| ||
I wanted to show my support as well for this very nice introduction into OOP with BlitzMax! Thank you very much for John :-) |
| ||
Fantastic learned alot from this tutorial. |
| ||
This tutorial should be made sticky, because it explains the how and why of OOP very well indeed - great stuff! |
| ||
Yes I second that Sticky Request. |
| ||
STICKY Thanks a bunch! |
| ||
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 |
| ||
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? |
| ||
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. |
| ||
thanks, where is this guide? Edit: sorry, i see it now (top of page) |
| ||
make this a sticky, plz. |
| ||
just finished reading, very well written. sticky!! |
| ||
yeah it is well writen should be added to the docs with max and yes sticky! |
| ||
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 |
| ||
... 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 |
| ||
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 |
| ||
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 |
| ||
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. |
| ||
as someone always looking to refine, the link is broken, any links to the original or downloads anyone has it? |
| ||
My brother ended up downloading it a while ago, here's a link. |
| ||
Hi Sonickidnextgen, I receive an error on my Firefox: Firefox can't find the file at http://media1.gamefront.com/guploads/201301/15/20/BlitzMax_OOP_Tutorial.zip?b17f4b620c6cf1393ffa644d11eea151ee12995c6f2461e126e601ef5a3f0f0569ff16138594a2f1b165ec34369665246886bad947e7bbd9031439239cd95d703571b787b52a552887231cbe4d449b6e423da831144d4fba58bab4cc8ee9f8fe9aa243cfb2a4bba8fd98365044ad206a83dd154c86265a. Could you please upload it somewhere else? Thanks, |
| ||
I downloaded it fine from that link. So I've hosted on my site : BlitzMax_OOP_Tutorial.pdf |
| ||
Thank you Brucey |
| ||
Thank you a lot Brucey. How can i delete an object created with New ? The Help Tree said "Delete" is reserved for future expansion. (i need to do this to manage a list of player when a player cut his connexion) |
| ||
The garbage collector will delete the object automatically (at some point in the future) when there are no references to it. |
| ||
Ok thank you Brucey. I am going to manage a boolean field "connected". Regards Golomp |