Author Topic: TYPES can self reference - example  (Read 5622 times)

jestermon

  • Guest
TYPES can self reference - example
« on: 2012-Sep-19 »
As I go through GLBasic, trying to see to what limits BASIC can represent OO principles, I am discovering amazing things about the TYPE datatype.
Since I have not seen any documentation or references to the self referencing of TYPE, I have decided to share this little code snippit.
Take special note of the line
FUNCTION add: vec AS VECTOR3
which is a function within the declaration of the VECTOR3 TYPE definition. This is so much like a class or a structure within c++ or c referencing itself.
I certainly hope that this type of feature gets documented soon, since a newcomer to GLBasic has no way of knowing that these sort of things are possible.

Code: GLBasic [Select]
// --------------------------------- //
// Project: vector3
// Author : jestermon
// --------------------------------- //


TYPE VECTOR3
        x#
        y#
        z#
        length#
       
        FUNCTION set: x#, y#, z#
                self.x# = x#
                self.y# = y#
                self.z# = z#
                self.getLength()
        ENDFUNCTION
       
        FUNCTION getLength:
                self.length# = SQR((self.x# * self.x#) + (self.y# * self.y#) + (self.z# * self.z#))
        ENDFUNCTION

        FUNCTION normalize:
                self.getLength()               
                IF self.length# < 0.0000001 THEN self.length# = 1
                self.x# = self.x#/self.length#
                self.y# = self.y#/self.length#
                self.z# = self.z#/self.length#
                self.getLength()
        ENDFUNCTION
       
        FUNCTION setLength: newLength#
                self.normalize()
                self.x# = self.x# * newLength#
                self.y# = self.y# * newLength#
                self.z# = self.z# * newLength#
                self.getLength()
        ENDFUNCTION
       
        FUNCTION add: vec AS VECTOR3
                self.x# = self.x# + vec.x#
                self.y# = self.y# + vec.y#
                self.z# = self.z# + vec.z#
                self.getLength()
        ENDFUNCTION
       

ENDTYPE


LOCAL v AS VECTOR3
LOCAL v2 AS VECTOR3

v.set(2,4,3)
PRINT v.length, 0,0

v2.set(3,2,5)
PRINT v2.length, 0,20

v.add(v2)
PRINT v.length, 0,40


SHOWSCREEN
MOUSEWAIT
 


EDIT:
I have completed a TYPE encapsulation of the VECTOR3 methods (functions), which make like a heck of a lot easier (for me at least), by everything as a member function of the TYPE (class), and having it all in one place. I've shared the VECTOR3 TYPE here, for anyone else who might find it helpful

Code: GLBasic [Select]
// --------------------------------- //
// Project: vector3
// Author : jestermon
// --------------------------------- //


TYPE VECTOR3
        x#
        y#
        z#
        length#

        FUNCTION set: x#, y#, z#
                self.x# = x#
                self.y# = y#
                self.z# = z#
                self.getLength()
        ENDFUNCTION

        FUNCTION getLength#:
                self.length# = SQR((self.x# * self.x#) + (self.y# * self.y#) + (self.z# * self.z#))
                RETURN self.length#
        ENDFUNCTION

        FUNCTION normalize:
                self.getLength()
                IF self.length# < 0.0000001 THEN self.length# = 1
                self.x# = self.x#/self.length#
                self.y# = self.y#/self.length#
                self.z# = self.z#/self.length#
                self.getLength()
        ENDFUNCTION

        FUNCTION setLength: newLength#
                self.normalize()
                self.x# = self.x# * newLength#
                self.y# = self.y# * newLength#
                self.z# = self.z# * newLength#
                self.getLength()
        ENDFUNCTION

        FUNCTION add: vec AS VECTOR3
                self.x# = self.x# + vec.x#
                self.y# = self.y# + vec.y#
                self.z# = self.z# + vec.z#
                self.getLength()
        ENDFUNCTION

        FUNCTION subtract: vec AS VECTOR3
                self.x# = self.x# - vec.x#
                self.y# = self.y# - vec.y#
                self.z# = self.z# - vec.z#
                self.getLength()       
        ENDFUNCTION

        FUNCTION invert:
                self.x# = - self.x#
                self.y# = - self.y#
                self.z# = - self.z#
                self.getLength()
        ENDFUNCTION
       
       
        FUNCTION multiply: vec AS VECTOR3
                self.x# = self.x# * vec.x#
                self.y# = self.y# * vec.y#
                self.z# = self.z# * vec.z#
                self.getLength()               
        ENDFUNCTION
       
        FUNCTION divide: vec AS VECTOR3
                self.x# = self.x# / vec.x#
                self.y# = self.y# / vec.y#
                self.z# = self.z# / vec.z#
                self.getLength()               
        ENDFUNCTION
       
        FUNCTION dot#: vec AS VECTOR3
                LOCAL res# = self.x#*vec.x#+self.y#*vec.y#+self.z#*vec.z#
                RETURN res#
        ENDFUNCTION
       
        FUNCTION cross: vec AS VECTOR3
                LOCAL v AS VECTOR3
                v.x# = self.y#*vec.z#
                v.y# = self.z#*vec.x#
                v.z# = self.x#*vec.y#
                self.x# = v.x#
                self.y# = v.y#
                self.z# = v.z#
                self.getLength()               
        ENDFUNCTION
       
        FUNCTION distance#: vec AS VECTOR3
                self.subtract(vec)
                RETURN self.length#
        ENDFUNCTION
       
        FUNCTION direction: vec AS VECTOR3
                LOCAL v AS VECTOR3
                v.x# = self.x#
                v.y# = self.y#
                v.z# = self.z#
                vec.subtract(v)
                self.x# = vec.x#
                self.y# = vec.y#
                self.z# = vec.z#
                self.getLength()                               
        ENDFUNCTION
       
        FUNCTION scalar#: scale#
                self.x# = self.x# * scale#
                self.y# = self.y# * scale#
                self.z# = self.z# * scale#
                self.getLength()
                RETURN self.length#                                            
        ENDFUNCTION
               
        FUNCTION angle#: vec AS VECTOR3
                LOCAL div, dotv ,l1,l2
            l1 = self.getLength()
            l2 = vec.getLength()
                div = l1 * l2
                IF div = 0 THEN RETURN 0
                dotv = self.dot(vec) / div
                IF ABS(dotv) > 1 THEN RETURN 0
                RETURN ACOS(dotv)
        ENDFUNCTION
       
        FUNCTION reflect: normal AS VECTOR3
           LOCAL dotv#
           self.normalize()
           normal.normalize()
           dotv# = self.dot(normal)
           normal.scalar(dotv#)
           normal.scalar(2.0)
           self.subtract(normal)
        ENDFUNCTION

ENDTYPE

 
« Last Edit: 2012-Sep-19 by jestermon »

MrTAToad

  • Guest
Re: TYPES can self reference - example
« Reply #1 on: 2012-Sep-20 »
Oh yes - its useful if you need recursion.

However, it can only be done with functions - you get a compiler error (type member is recursively defined) if you try it with variable allocation

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1634
  • Particle Hawk
    • View Profile
Re: TYPES can self reference - example
« Reply #2 on: 2012-Nov-27 »
Its very interesting, this is how you would setup a class in python, object everything.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3365
  • Integrated Brain
    • View Profile
Re: TYPES can self reference - example
« Reply #3 on: 2012-Nov-27 »
Yes really this it's simply awesome  ... before too meet this method... I read some thing about Classes in C++, but if someone don't shows how works, it's a bit confused...

When I start to use this method, I think it's explained in the MrTatoad Book, basically I do all my Code at this mode... modify something it's question of seconds...

I don't know if in C++ the Classes are more powerfully, but whit only this you can do very complex things very easy and quickly

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 491
    • View Profile
Re: TYPES can self reference - example
« Reply #4 on: 2012-Nov-27 »
I don't know if in C++ the Classes are more powerfully, but whit only this you can do very complex things very easy and quickly

In C++ you can make many things with classes than with GLB TYPEs. For example, inheritance, private variables and methods, function override, etc. But in GLB is more easy to use. I want inheritance for GLB too, using EXTENDS in Types like in Java or INHERITS like in VBasic. It's easy to understand that you include the content of one type in another type.

For example, making a chess game:

Code: GLBasic [Select]
TYPE Piece
   row%; column%
   colour%
   
   FUNCTION Move: newrow%, newcol%
       row = newrow
       column = newcol
   ENDFUNCTION
ENDTYPE

TYPE Knight
    EXTENDS Piece

    // True (1) if knight can move to new position
    FUNCTION CanMove: newrow%, newcol%
         // ...
    ENDFUNCTION
ENDTYPE

TYPE Pawn
    EXTENDS Piece

    // True (1) if pawn can move to new position
    FUNCTION CanMove: newrow%, newcol%
       // .....
    ENDFUNCTION
ENDTYPE
 
« Last Edit: 2012-Nov-27 by hardyx »

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3365
  • Integrated Brain
    • View Profile
Re: TYPES can self reference - example
« Reply #5 on: 2012-Nov-27 »
ok Hardyx I think I understand a bit... it's like a make Types into another Types, but whit functions... I never use more than a "Sub-Level" but I suppose if you say this can be very usefully, I´m a only newbie programming...  :P

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: TYPES can self reference - example
« Reply #6 on: 2012-Nov-28 »
I would like to see a "extends" function in GLBasic, but I don't think that Gernot will implement it.
Many people before (including me) asked for this feature but Gernot don't want "real" OOP in GLBasic because of it's "complexity" for beginners.

Offline r0ber7

  • Prof. Inline
  • *****
  • Posts: 526
    • View Profile
Re: TYPES can self reference - example
« Reply #7 on: 2012-Nov-28 »
Inheritance would be great. The way I go about it now is somewhat like this:

Code: GLBasic [Select]
TYPE plant_type
       x%
ENDTYPE

TYPE flower_type
   plant AS plant_type
ENDTYPE
 

I know that's not real inheritance, but it suits my purposes.

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3365
  • Integrated Brain
    • View Profile
Re: TYPES can self reference - example
« Reply #8 on: 2012-Nov-28 »
I would like to see a "extends" function in GLBasic, but I don't think that Gernot will implement it.
Many people before (including me) asked for this feature but Gernot don't want "real" OOP in GLBasic because of it's "complexity" for beginners.

I think this it's a good point for the new people but bad for people we are using GLBasic for some years... I think solve this problem not it's too much complex, I think the forum will have an specific post, like a mini course for begin whit Glbasic and the getting skills.... I say this because when I begin whit GLBasic I don't know about the book of MrTatoad after a year, and I stay in the forum each day from I started...

PS: I think if any one of us write a little chapter explaning, very softly how make things can be great for people new in the forum... I don't wanna say a book, only a few pages explaining concepts from easy to "hard"

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: TYPES can self reference - example
« Reply #9 on: 2012-Nov-28 »
mix Inline c++ with GLB :)
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: TYPES can self reference - example
« Reply #10 on: 2012-Nov-29 »
mix Inline c++ with GLB :)

This is much more complex than a "extends" function.  :whistle: :good:

Offline siatek

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
Re: TYPES can self reference - example
« Reply #11 on: 2012-Nov-29 »
I would like to see a "extends" function in GLBasic, but I don't think that Gernot will implement it.
Many people before (including me) asked for this feature but Gernot don't want "real" OOP in GLBasic because of it's "complexity" for beginners.

I think this it's a good point for the new people but bad for people we are using GLBasic for some years... I think solve this problem not it's too much complex, I think the forum will have an specific post, like a mini course for begin whit Glbasic and the getting skills.... I say this because when I begin whit GLBasic I don't know about the book of MrTatoad after a year, and I stay in the forum each day from I started...

PS: I think if any one of us write a little chapter explaning, very softly how make things can be great for people new in the forum... I don't wanna say a book, only a few pages explaining concepts from easy to "hard"

What book ?
glbasic roxx !!!

Offline fuzzy70

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 828
  • Look left, Look right, LOOK OUT!!
    • View Profile
Re: TYPES can self reference - example
« Reply #12 on: 2012-Nov-29 »
What book ?
Have a look at MrTAToads sig at the bottom of his posts & you will see a link to GLBasic books he has made  =D

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: TYPES can self reference - example
« Reply #13 on: 2012-Nov-29 »
I want extends too.

But if someone give me a 3-line sample how to inheritance one GLB type in an other one it would do the job for me.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: TYPES can self reference - example
« Reply #14 on: 2012-Nov-29 »
@kanonet:

stupid example, but hey, simple as f**k :D

Code: GLBasic [Select]
TYPE tVec
        x%;y%
        FUNCTION SET: x%, y%
                self.x = x
                self.y = y
        ENDFUNCTION
ENDTYPE


TYPE tText
       
        vec AS tVec
       
        FUNCTION SHOW: text$
                        PRINT text$, self.vec.x, self.vec.y
        ENDFUNCTION
ENDTYPE


LOCAL text AS tText
text.vec.SET(10,20)

WHILE TRUE


        text.SHOW("TEST!!!")

SHOWSCREEN
WEND
END
 




hmm you can code your game in Inline :D :


Code: GLBasic [Select]
INLINE

class cVec{
public:
        int x, y;
       
        cVec(int x, int y){
                this->x = x;
                this->y = y;
        }
};


class cText{
public:
        void show( const char* text, cVec* vec){
                PRINT(text, vec->x, vec->y);
        }
        void show( const char* text, int x, int y){
                PRINT(text, x, y);
        }
};

cVec vec(10,20);
cText* text;



        while(true){
       
                text->show("TEST PRINT",&vec);
                text->show("TEST PRINT2",10,40);
       
        SHOWSCREEN();
        }

ENDINLINE

 


And you can do all the stuff like in C++ ... function overloading, inheritance, friendship in classes and so on :)

NOTE:

in c++ all GLBasic-Commands have a () at the end!
« Last Edit: 2012-Nov-29 by Schranz0r »
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard