GLBasic forum

Main forum => GLBasic - en => Topic started by: sf-in-sf on 2013-Aug-27

Title: member functions of TYPE
Post by: sf-in-sf on 2013-Aug-27
Here is an example of encapsulted functions inside a TYPE. Keep in mind they are supposed to be the external "public buttons" of your objects.
It works like that.
I didn't get the green "this" to work -both in GLb and C- but appearently "self" would be the right word. I will try it.
Code (glbasic) Select

// --------------------------------- //
// Project: encaps_function
// Start: Thursday, August 22, 2013
// IDE Version: 10.283 // just updated

// The green "this" remains a mystery to me,
// but the program works like that:
TYPE sq
size%=100;
dir%=1;
//dummy%=0
FUNCTION changesize: // a member function, "encapsulated".
INLINE //Now use the different C syntax:

if (dir>0)
{INC(size); //should be this.size?
if (size>222)
{size=200; dir=0;}
}
else
{DEC( size);
if (size<77)
{size=88 ; dir=2;}
}
// ENDIF // not in C.
ENDINLINE

ENDFUNCTION
FUNCTION draw:
INLINE
DRAWRECT( 33,33,size,size, 0xffbb99);
ENDINLINE
ENDFUNCTION
ENDTYPE

LOCAL s AS sq  //instanciation
WHILE TRUE
// call member functions of the object:
s.changesize()
s.draw()
// i.e. push the external buttons of
// the closed object.
SHOWSCREEN
WEND

// P.S. a typical example of such member functions are
// "accessors", used to get or set the encapsulated values
// of the object from the outside.
Title: Re: member functions of TYPE
Post by: kanonet on 2013-Aug-27
I dont really unterstad the purpose of this tutorial, since it does not really show anything. Yes the command to access the type inside his own functions is "self.". If you would show this, it may be useful for someone. Sorry, its not meaned as an offence.
Title: Re: member functions of TYPE
Post by: mentalthink on 2013-Aug-27
Kanonet, I think like aproach to C++ goes fine, for me it's good see codes like this... My C++ skills are very low and I think I want to do something like this the another day whitout succes... I'm sure for you, like a good porgrammer this it's very easy , but for people like me , we don't see the code very clear sometimes... like teaching works good at least for me.
Title: Re: member functions of TYPE
Post by: kanonet on 2013-Aug-27
But for this example INLINE was just not neccessary, "self." would completly do the job. BTW self also works in INLINE, or you could use "this->". His post was more a question then a tutorial, thats why i answered here, IMHO it was a bit to few information get called "tutorial". But thats my opinion. ;)
Title: Re: member functions of TYPE
Post by: mentalthink on 2013-Aug-27
Sorry then I read bad...   :rant: :-[ :P
Title: Re: member functions of TYPE
Post by: hardyx on 2013-Aug-27
In C++, you can use "size" (the variable) or this->size, but "this->" isn't mandatory. "this" is a pointer to the type in C++.
In GLBASIC the sentence is self.size, and it's mandatory to access type vars. "self" is a reference to the type in GLB.

P.D. You can write your type functions in GLBasic code too.
Title: Re: member functions of TYPE
Post by: sf-in-sf on 2013-Sep-11
Thanks for the comments everyone. Someone more experienced is welcome to re-write the code in a better way, and post it to the community. Thanks in advance.
I find this tuto is an important key to get started with the  object functionality of GLb, and I feel it should appear in a form or another in the help files / manual, Gernot. It's a pitty that "self" is not documented.
Title: Re: member functions of TYPE
Post by: kanonet on 2013-Sep-11
I think good examples how to use Types + Functions are:
www.glbasic.com/forum/index.php?topic=4486
www.glbasic.com/forum/index.php?topic=7280
Title: Re: member functions of TYPE
Post by: sf-in-sf on 2013-Sep-30
Nice one Hatonastick!
I'm back with a final update that only uses GLb syntax. Getting started becomes easy now.
Code (glbasic) Select
// --------------------------------- //
// Project: encaps_function

// The green "this" remains a mystery to me,
// but this is the way to stick to the GLb syntax:
TYPE sq
loc%=33; size%=100;
dir%=1;
//wish$="go_sailing"
FUNCTION changesize: // a member function, "encapsulated".

IF self.dir>0
INC self.size
IF self.size>222
self.size=200
self.dir=0
ENDIF
ELSE
DEC self.size
IF self.size<77
self.size=88 ; self.dir=2
ENDIF
ENDIF

ENDFUNCTION
FUNCTION draw:
DRAWRECT self.loc,self.loc,self.size,self.size, 0xffbb99
ENDFUNCTION
ENDTYPE

LOCAL s AS sq  //instanciation, builds object s from type/template sq.
// (s.loc=33 automatically)
LOCAL s2 AS sq
s2.loc=222 //adjustment, kind of polymorphism.
WHILE TRUE
// call member functions of the object:
s.changesize() // 'self' will refer to s.
s.draw()
s2.changesize() // 'self' will refer to s2.
s2.draw()
// i.e. push the external buttons of
// the closed object.
SHOWSCREEN
WEND

// P.S. a typical example of such member functions are
// "accessors", used to get or set the encapsulatd values
// of the object from the outside.
Title: Re: member functions of TYPE
Post by: hardyx on 2013-Oct-01
Better code and more readable now.
Title: Re: member functions of TYPE
Post by: Hemlos on 2014-Jan-15
Yes this would be self in glbasic
ps. not a bump, i think you were in the wrong area when you started this thread, its ok ill move it to our GLBasic - en



Title: Re: member functions of TYPE
Post by: MrPlow on 2014-Jan-15
What i would like to know is .... is it faster to use "in type functions" and how they could benefit array types?

So far I have never used these as I am unsure of the performance considerations...
Title: Re: member functions of TYPE
Post by: MrTAToad on 2014-Jan-15
Continual use of functions in types does work out quicker than using standard functions.

In addition, keeping the program running from inside an extended type does also have a slight speed advantage.
Title: Re: member functions of TYPE
Post by: MrPlow on 2014-Jan-16
Thanks MrT,

Might look into it more...for future projects...
Title: Re: member functions of TYPE
Post by: MrTAToad on 2014-Jan-17
They are useful!