How To

Are Global Variables a thing?

1

AstonBrown 2022-11-06 10:55

In python, I can define a variable as a global variable. That variable would then be able to be accessed in defined functions. Is it possible to do so in Lowers NX in subprograms? If so, how do I do it?


McPepic 2022-11-06 13:18

GLOBAL X, Y
X = 100
Y = 50

CALL DRAW

SUB DRAW
SPRITE 0, X, Y, 1
END SUB


AstonBrown 2022-11-06 13:22

Thank you McPepic


was8bit 2022-11-06 15:14

Alternatively, you can feed either constants or variables into a sub...

So if you feed a variable into a sub, the sub can change the value of that variable...

SUB WRAP(x,y,dx,dy)
Add x,dx,0 to 19
Add y,dy,0 to 15
End sub

Then use

Call wrap(px,py,1,0)

Or

Call wrap(ex,ey,0,-1)

So you can reuse the one SUB to affect many different inputed variables ;)


was8bit 2022-11-06 15:19

GLOBAL is very useful, but also remember that variables created inside a sub are auto destroyed once the sub is finished... it is possible, for example, to use DIM checkscreeen(19,15), and use that array inside that sub, and each time the sub is called, it creates a fresh array automatically set to all zeros for its values ;)


Log in to reply.