Page 1 of 1

Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 9:02 am
by Hblade
This idea was actually obtained while using RPG Maker VX / XP

You can run the entire game off of 1 variable. Go to global code, click "Variables". click add. Make a variable called GAME_VARIABLES, or what ever you like :3

Where it says Array, click Yes, then make it the max size, 32000, for now. You can change the array size once your done making your game.

Now in Global Code, Define your variables like so:
Code: Select all
#define SHOOT 0
#define ENEMY_SHOOT 1
#define DIFICULTY 2
#define SPAWN_TIME 3
#define HP 4
#define ENEMY_HP 5
#define SCORE 6

Those are pretty much your non-process eating variables :D! Oh, but one more thing. Go back to variables and click GAME_VARIABLES, then click edit. Make it a "real" that way you can also store decimals :)

To edit one of your defined variables, or read from them, simply do this:
Code: Select all
GAME_VARIABLES[HP] = 34;

This will make the HP = 34 :D

To read from a variable using a text actor, for example score
Code: Select all
sprintf(text, "%g", GAME_VARIABLES[SCORE]);


Hope this helped :D

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 12:32 pm
by lcl
Hey that's good idea! :D

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 2:39 pm
by Hblade
thanks

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 4:22 pm
by jimmynewguy
But doesn't that make all those variables global? All the enemies would be sharing health, I think....I don't really use arrays but by just looking at this it seems this is so. But besides that all your global variables can be one thing, which helps alot! Thanks :)

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 4:33 pm
by Hblade
exactly, thats why I guess just make a few more variables for health. This atleast helps a bit with the globals :D

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 5:46 pm
by Bee-Ant
Free up your processing or free up your computer processing???
If you mean your computer, then I think it's wrong.
Using a single variable doesn't mean speed up your performance...
Let me explain...

- You use Double/Real type variable there, it's size is 8 bytes...
- You use 32000 spaces, so it's 8*32000=256000 bytes
- You use 7 #defines...well I dunno the size, but so far you've already allocated your memory over 256000 bytes

Remember, array ALWAYS allocate memory, whether you fill it or not.
So in this case, it's better to use 7 different real variables non array. They will allocate 56 bytes only...

For more variable size, lookie here http://www.cplusplus.com/doc/tutorial/variables/
and to determine your array size, lookie here http://stackoverflow.com/questions/3753 ... f-my-array
:D

Re: Free up processing, use 1 variable :)

PostPosted: Thu Jul 22, 2010 5:53 pm
by Hblade
Oh O.o


Thanks for the info, bee :D

Re: Free up processing, use 1 variable :)

PostPosted: Fri Jul 23, 2010 2:13 pm
by Fuzzy
I gotta agree with bee.

After all, you cant use things like % with reals. You also end up typing longer variable names, and as bee said, you waste a lot of space.

I would use that technique in very limited situations, and I applaud your thinking outside the box. I think you deserve a point for that.

What you could do is a mixture of that and an array of structures.

struct extra_actor_vars{
char name[];
int health;
float yourboat;
}

It could be quite elegant.

Re: Free up processing, use 1 variable :)

PostPosted: Fri Jul 23, 2010 3:00 pm
by Hblade
Yeah :3