TYPES can self reference - example

Previous topic - Next topic

jestermon

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


MrTAToad

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

Hemlos

Its very interesting, this is how you would setup a class in python, object everything.
Bing ChatGpt is pretty smart :O

mentalthink

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

hardyx

#4
Quote from: mentalthink 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


mentalthink

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

backslider

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.

r0ber7

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.

mentalthink

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"

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

backslider

Quote from: Schranz0r on 2012-Nov-28
mix Inline c++ with GLB :)

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

siatek

Quote from: mentalthink 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"

What book ?
glbasic roxx !!!

fuzzy70

Quote from: siatek 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)

kanonet

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

Schranz0r

#14
@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!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard