TYPE problem

Previous topic - Next topic

bricky91

Hey guys, i've a little problem (well, in fact it is a great problem).

I am learing how to use types, and i wrote this code:

Code (glbasic) Select
TYPE player
x
y
vspeed
ENDTYPE


Well, it gives me an error: syntax error at line 19 (line 19 is where i worte "TYPE player"). What's wrong with my code? I can't understand.

Oh and i can't even update using the editor, i also get an error doing this, so tomorrow i'll download the last version and i'll try it. Maybe it won't give the error any more, but i just wanted to understand if there was something wrong with my code.

Thanks, and sorry for my english, I'm italian...  :whistle:

Moru

You need the latest version from the homepage for updating.

What do you have above the TYPE line?

peterpan

Hi,

here are you.

TYPE Test
a
b
c
ENDTYPE

GLOBAL T AS Test
T.a =100
T.b =200
T.c = T.a + T.b

PRINT T.c,100,100
SHOWSCREEN
MOUSEWAIT

MrTAToad

With the latest update, that works fine (and displays 300)

Schranz0r

Quote from: peterpan on 2009-Feb-04
Hi,

here are you.

TYPE Test
a
b
c
ENDTYPE

GLOBAL T AS Test
T.a =100
T.b =200
T.c = T.a + T.b

PRINT T.c,100,100
SHOWSCREEN
MOUSEWAIT

thats wrong.

You need something like this:

Code (glbasic) Select
TYPE TPlayer
x;y
name$
ENDTYPE

GLOBAL TPlayers[] AS TPlayer

LOCAL PlayerOne AS TPlayer
LOCAL PlayerTwo AS TPlayer


PlayerOne.x = 100
PlayerOne.y = 200
PlayerOne.name$ = "P1"
DIMPUSH TPlayers[], PlayerOne


PlayerTwo.x = 200
PlayerTwo.y = 100
PlayerTwo.name$ = "P2"
DIMPUSH TPlayers[], PlayerTwo


WHILE TRUE


FOREACH player IN TPlayers[]
PRINT player.name$, player.x, player.y
NEXT


SHOWSCREEN
WEND
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

bricky91

Oh, I think I found out the mistake... I wrote
Code (glbasic) Select
TYPE player
x,y
vspeed
ENDTYPE


I used "x,y" instead of "x;y"... I tried wiritng "x;y" and now it works. Thanks to all! Oh, and another question: can I update player 1 and player 2 (refering to schranz0r's example) even individually, without using FOREACH?

Schranz0r

#6
Code (glbasic) Select
WHILE TRUE
// some code
    INC PlayerOne.x, 1 // Move 1 pixel to the right
    PRINT PlayerOne.name$, PlayerOne.x, PlayerOne.y
// more code
WEND
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

Kitty Hello

or:

Players[ 1 ] . x

Beware! The PlayerOne and Players[ 0 ] are not the same, Players[ 0 ] is created by DIMPUSH and thus is a copy of PlayerOne.

bricky91

Ok, thanks a lot! I'll try a bit now   =D

bricky91

Well, another problem, I was trying to make a simple collision system, but it doesn't work... Here is the code:

Code (glbasic) Select
SETSCREEN 640,480,0

level_state=0


TYPE player
x;y
vspeed
ENDTYPE
GLOBAL pl[] AS player


TYPE blocco
x;y
ENDTYPE
GLOBAL bl[] AS blocco



WHILE KEY(1)=FALSE
IF level_state=0
LOCAL b AS blocco
b.x=0
b.y=240
DIMPUSH bl[],b
LOCAL b2 AS blocco
b2.x=32
b2.y=400
DIMPUSH bl[],b2


LOCAL p AS player
p.x=0
p.y=120
p.vspeed=0
DIMPUSH pl[],p

level_state=1
ENDIF

IF level_state=1
update_player()
ENDIF
SHOWSCREEN
WEND
END


FUNCTION update_player:
FOREACH b IN bl[]
FOREACH p IN pl[]
IF collision_block(p.x,p.y,p.vspeed,b.x,b.y)=0
p.vspeed=5
ELSE
p.vspeed=0
ENDIF
p.y=p.y+p.vspeed
IF KEY(203)
p.x=p.x-3
ENDIF
IF KEY(205)
p.x=p.x+3
ENDIF
DRAWRECT p.x,p.y,32,32,RGB(255,0,0)
NEXT

DRAWRECT b.x,b.y,32,32,RGB(255,255,255)
NEXT
ENDFUNCTION


FUNCTION collision_block: xa, ya, vspeeda, xb, yb
IF(xa>=xb AND xa<=xb+32 AND ya+35+vspeeda>=yb AND ya+vspeeda<=yb+33)
RETURN 1
ELSE
RETURN 0
ENDIF
ENDFUNCTION



The player doesn't stop on the blocks... This works fine if there is only one block, but with 2 blocks the player doesn't stop on them. I'm sorry, I know I'm boring, but I want to learn how to use GL basic well, so I need some help  :-[

Schranz0r

#10
use that command:

col% = BOXCOLL(xa%, ya%, wa%, ha%, xb%, yb%, wb%, hb%)
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

bricky91

Ok, thanks a lot, I also needed to write "BREAK" after setting "vspeed=0", to exit the FOR loop. Maybe I'm starting understand something  :whistle: