Define function

Previous topic - Next topic

gavendortt

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!

doimus

You could use ordinary variable for that:

mySprite% = 1

LOADSPRITE "sprite.png", mySprite%

Kitty Hello

Or (experimental stuff) try:
CONSTANT foo% = 123

gavendortt

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?

MrTAToad

You only save memory if the #define isn't used, otherwise its a normal variable.

gavendortt

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.

doimus

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

Hemlos

#7
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

Bing ChatGpt is pretty smart :O

gavendortt

#8
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:

Schranz0r

Code (glbasic) Select
FUNCTION LoadImage: file$
    LOCAL img_id = GENSPRITE()
    LOADSPRITE file$,img_id
    RETURN img_id
ENDFUNCTION
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

Hemlos

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
Bing ChatGpt is pretty smart :O

Schranz0r

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

gavendortt

Wow! Thank you!  =D