GLBasic forum

Main forum => GLBasic - en => Topic started by: gavendortt on 2009-Aug-27

Title: Define function
Post by: gavendortt on 2009-Aug-27
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!
Title: Re: Define function
Post by: doimus on 2009-Aug-27
You could use ordinary variable for that:

mySprite% = 1

LOADSPRITE "sprite.png", mySprite%
Title: Re: Define function
Post by: Kitty Hello on 2009-Aug-27
Or (experimental stuff) try:
CONSTANT foo% = 123
Title: Re: Define function
Post by: gavendortt on 2009-Aug-27
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?
Title: Re: Define function
Post by: MrTAToad on 2009-Aug-27
You only save memory if the #define isn't used, otherwise its a normal variable.
Title: Re: Define function
Post by: gavendortt on 2009-Aug-27
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.
Title: Re: Define function
Post by: doimus on 2009-Aug-27
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
Title: Re: Define function
Post by: Hemlos on 2009-Aug-27
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.

Code (GENCODES()) Select
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.

Code (Written by Moru) Select

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

Title: Re: Define function
Post by: gavendortt on 2009-Aug-28
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:
Title: Re: Define function
Post by: Schranz0r on 2009-Aug-28
Code (glbasic) Select
FUNCTION LoadImage: file$
    LOCAL img_id = GENSPRITE()
    LOADSPRITE file$,img_id
    RETURN img_id
ENDFUNCTION
Title: Re: Define function
Post by: Hemlos on 2009-Aug-28
Quote from: Schranz0r on 2009-Aug-28
Code (glbasic) Select
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
Title: Re: Define function
Post by: Schranz0r on 2009-Aug-29
hehe :D
Title: Re: Define function
Post by: gavendortt on 2009-Aug-29
Wow! Thank you!  =D