Me again! :S
I'm on my 3D editor for my game and I want to know if we can define a variable. I explain.
In C++, we can do ?#DEFINE to define a keyword who work like a rename of anything : function, variable, ...
Poor explaination, but I'm sure Kitty will understand me! :whistle:
I want to know if this feature is in GLBasic, because when we need to declare a certain number of sprite, the number is very hard to understand.
Thank you again!
You could use ordinary variable for that:
mySprite% = 1
LOADSPRITE "sprite.png", mySprite%
Or (experimental stuff) try:
CONSTANT foo% = 123
Yes, but I don't want to use so much memory, because when we use define, the compiler just replace it when hcompile, then there is no memory lost like with variable. I will need more than one editor open and will need all the power of my weak pc, so. There is no define function in GLBasic or I will need to use number?
You only save memory if the #define isn't used, otherwise its a normal variable.
Mmm... I never noticed this, because it's a preprocessor entry. The compiler just replace the define per the number/word we have defined. So, the code don't change, it's the same if you use/don't use the define function. My program was never bigger with this, same thing in Purebasic.
IMHO, it's pointless to discuss memory footprint of constant and regular variable... especially in GLB and on PC. If you have anything more modern than 386, I guess you're safe with using any variables anywhere. =D
There is a way to define sprites, files, objects, and sounds.
Look at the GEN section in the help files.
The names you can define are related to Sprites, Files, Sounds, and Objects:
id% = GENSPRITE()
id% = GENSOUND()
id% = GENX_OBJ()
id% = GENFILE()
This is how you can keep track of names without having to know thier values.
GLOBAL gImgFoo = GENSPRITE(); LOADSPRITE "foo.png", gImgFoo
GLOBAL gImgBar = GENSPRITE(); LOADSPRITE "bar.png", gImgBar
DRAWSPRITE gImgFoo,0,0
DRAWSPRITE gImgBar,0,100
SHOWSCREEN
MOUSEWAIT
Alternative to GEN commands:
It does the same thing as GENSPRITE() and LOADSPRITE, as above.
Has 1 less command per line, but you will need to add the function in your program somewhere.
GLOBAL gImgFoo = LOADIMAGE( "gImgFoo.png" )
DRAWSPRITE gImgFoo , 100 , 100
SHOWSCREEN
MOUSEWAIT
END
FUNCTION LOADIMAGE: File$ //returns ID
STATIC id%
INC id, 1
LOADSPRITE File$, id
RETURN id
ENDFUNCTION
Mmmm...
These two function work like a variable, but after your bug report, I think I will try the second, just to see.
Thanks for your help! :good:
FUNCTION LoadImage: file$
LOCAL img_id = GENSPRITE()
LOADSPRITE file$,img_id
RETURN img_id
ENDFUNCTION
Quote from: Schranz0r on 2009-Aug-28
FUNCTION LoadImage: file$
LOCAL img_id = GENSPRITE()
LOADSPRITE file$,img_id
RETURN img_id
ENDFUNCTION
This is the way to go....short and sweet.
I was actually thinking about this today at work, but you beat me to it ;P
hehe :D
Wow! Thank you! =D