GLBasic forum

Main forum => GLBasic - en => Topic started by: freshworks on 2009-Nov-16

Title: Tested some simple things with prototype, but cannot get what i want, help.
Post by: freshworks on 2009-Nov-16
Oke, i've tested some simple things with prototype, but cannot get what i need, probally there is a way :) 


First of all:

I Cannot create a new file, added to the project with a prototype definition and functions included, i get errors. Looks like the functions need to be beneath the mainloop. Putting both, PROTOTYPE and the according functions in a second file in the project, causes errors.


Second, most important:
The code be beneath does not work , because i cannot call the player.x and player.y  within the function ShowPlayer or is there a way to do so ???? It would be nice when we could make a call like this.x , this.y etc. (see code for example) the ShowPlayer is the most interesting!


Third:
Can't i return a TYPE from a function like newobject = functionname() <= then the function will create a global new object (prototype) and return this into a var as object, easier to handle :)



Code (glbasic) Select

// PROTOTYPE SOME FUNCTIONS AND PROPERTYS //

PROTOTYPE sInit: x, y, filename$
PROTOTYPE sShow:
TYPE TPlayer
   x
   y
   spriteid
   Init AS sInit = InitPlayer
   Show AS sShow = ShowPlayer
ENDTYPE


// INIT TPLAYER / GAME LOADING ETC //
GLOBAL player AS TPlayer
player.Init(0,0,"Media/grossini.png")


// MAIN LOOP //
WHILE TRUE
   player.x = 0
   player.y = 100
   player.Show()
   SHOWSCREEN
WEND
// END MAINLOOP //






// PROTOTYPE FUNCTIONS / METHODS //
FUNCTION InitPlayer: px, py, filename$
   sprnum% = GENSPRITE()
   LOADSPRITE filename$,sprnum%
   player.spriteid = sprnum%
ENDFUNCTION // INITPLAYER


FUNCTION ShowPlayer:
   DRAWSPRITE [ thisobject.spriteid],[thisobject.x],[thisobject.y]
ENDFUNCTION




Hope it's clear a bit, cheers.
Title: Re: Tested some simple things with prototype, but cannot get what i want, help.
Post by: MrTAToad on 2009-Nov-16
I don't think you can initialise types with PROTOTYPE functions...
Title: Re: Tested some simple things with prototype, but cannot get what i want, help.
Post by: Kitty Hello on 2009-Nov-16
there is no "thisobject", yet. GLBasic is not OO.
Title: Re: Tested some simple things with prototype, but cannot get what i want, help.
Post by: Schranz0r on 2009-Nov-16
You can with a trick:

Code (glbasic) Select

PROTOTYPE sInit: t AS TPlayer, x, y, filename$

...


// INIT TPLAYER / GAME LOADING ETC //
GLOBAL player AS TPlayer
player.Init(player,0,0,"Media/grossini.png")

...


// PROTOTYPE FUNCTIONS / METHODS //
FUNCTION InitPlayer: t AS TPlayer, px, py, filename$
   t.spriteid% = GENSPRITE()
   t.x = px
   t.y = py
   LOADSPRITE filename$, t.sprnum%
ENDFUNCTION // INITPLAYER