Tested some simple things with prototype, but cannot get what i want, help.

Previous topic - Next topic

freshworks

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.

MrTAToad

I don't think you can initialise types with PROTOTYPE functions...

Kitty Hello


Schranz0r

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

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