FOR loop issues

Previous topic - Next topic

DHeadshot

Hi, just a slight issue.  Whenever I try to do anything useful with for loops, I get something like this:
Code (glbasic) Select

C:\DOCUME~1\(Username removed)\LOCALS~1\Temp\glbasic\gpc_temp0.cpp: In function `int __GLBASIC__::__MainGameSub_()':
C:\DOCUME~1\(Username removed)\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:89: error: `n' was not declared in this scope
*** FATAL ERROR - Please post this output in the forum

the related code will be simply a FOR loop with something inside it.  This exact errormessage was generated from this:
Code (glbasic) Select
LET n=1
FOR n=0 TO 1 STEP 0.1
ALPHAMODE n
LOADSPRITE "(Filename Removed)",0
DRAWSPRITE 0, 300, 150
SHOWSCREEN
KEYWAIT
NEXT

Any ideas?

MrTAToad

That error message means that you have enabled explicit variable declaration and haven't defined the variable "n"

DHeadshot

#2
There is nothing in the help about option explicit and I haven't touched anything.

Edit:  I found the Option Explicit box.  It's not ticked.  Any other ideas?

This is why I hate anything that has anything to do with C - things always go wrong when the C bit compiles.  C is the worst language ever made (yes, that includes COBOL and Malbolge) as it will often randomly decide it hates you and won't work.

Ian Price

Remove the "LET n=1" and it works.
I came. I saw. I played.

DHeadshot

I tried that before and it didn't work.  However, I've just tried again and it has.  Any idea why it randomly changed it's mind (other than the fact that C does that in my experience)?

Schranz0r

LOCAL n#


;) btw: LET is out!
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

MrTAToad

#6
LET probably checks to see if the variable is defined when you assign a value - and thus you get the message...

Quote(other than the fact that C does that in my experience)
The only time it will seem to do this is overwriting random parts of memory - which is due to programmer error...

Have you recently updated your copy of GLBasic ?

DHeadshot

Did it earlier today, when I first started it up...

As for LET, I like to stick it in if something isn't working, just to see if that's the problem...

com_1

LOADSPRITE "sprite.png",1;

n=0.0015;


WHILE TRUE

IF n<1; n=n+0.0015;ENDIF;

ALPHAMODE -n; DRAWSPRITE 1, 300, 150; ALPHAMODE 0;

SHOWSCREEN; WEND