The Dink Network

Reply to Re: Storage capacity

If you don't have an account, just leave the password field blank.
Username:
Password:
Subject:
Antispam: Enter Dink Smallwood's last name (surname) below.
Formatting: :) :( ;( :P ;) :D >( : :s :O evil cat blood
Bold font Italic font hyperlink Code tags
Message:
 
 
November 9th 2018, 07:28 AM
slimeg.gif
metatarasal
Bard He/Him Netherlands
I object 
I think you're stuck because you keep thinking about it in terms of bits. Just because computers think in bits doesn't mean you have to! If you want to use a variable of base 4 (values 0-3), you can just use base 4 directly. If you want base 3 or base 5 (which you can't if you think only in bits) you can do that too!

Let me take an example of storing three variables of base 4 into the same global variable:

//Assigning three values to a global variable, the three variables are passed as &arg1 &arg2 and &arg3
int &temp = &arg3;
&temp = &arg3;
&temp * 4;
&temp += &arg2;
&temp * 4;
&temp += &arg1;
&globalvar = &temp;


And for reading a variable from this:

//Reading an arbitrary variable from the global variable.
//&arg1 contains which variable we want to get (the first, second or third)
int &result = &globalvar;
int &temp = &arg1;

&temp -= 1;
divloop:
if (&temp > 0)
{
&result / 4;
&temp -= 1;
goto divloop;
}

&result = math_mod(&result, 4);
return(&result);
kill_this_task();


I couldn't be bothered to make a full method out of this, but it should be relatively easy. Note that using this method you can easily store numbers of a different base if you want to, just change the number 4 in the scripts above to whatever base you want to use. The only thing to consider is the total size of an int which limits how many variables you can store in the same global. If you want to know how far you can go: The product of all the bases cannot exceed 2147483648. You could even extend the methods above to hold multiple variables of multiple bases at once, but it gets more complicated and the situations in which it is actually useful are probably rather exotic.

I didn't actually test the scripts above (blame laziness), but I did throw together a little demonstration DMOD demonstrating the principle. This DMOD has one screen with three objects on it having three different scripts which count how often they've been hit (they reset to 0 after 3). Here are the scripts if you like to try this yourself:

First object:
void hit(void)
{

//Get value:
int &var1 = math_mod(&globvar, 4);

//Add one as a test:
&var1 += 1;
if (&var1 > 3)
&var1 = 0;

//Store value:
&globvar / 4;
&globvar *= 4;
&globvar += &var1;

say("var1: &var1 globvar: &globvar",1);

}


Second object:
void hit(void)
{

//Get value:
int &temp1 = &globvar;
&temp1 / 4;
int &var2 = math_mod(&temp1, 4);

//Add one as a test:
&var2 += 1;
if (&var2 > 3)
&var2 = 0;

//Store value:
int &temp2 = math_mod(&globvar,4);

&temp1 / 4;
&temp1 * 4;
&temp1 += &var2;
&temp1 * 4;
&temp1 += &temp2;
&globvar = &temp1;

say("var2: &var2 globvar: &globvar",1);

}


Third object:
void hit(void)
{

//Get value:
int &var3 = &globvar;
&var3 / 16;

//Add one as a test:
&var3 += 1;
if (&var3 > 3)
&var3 = 0;

//Store value:
int &temp1 = &var3;
int &temp2 = math_mod(&globvar,16);

&temp1 * 16;
&temp1 += &temp2;
&globvar = &temp1;

say("var3: &var3 globvar: &globvar",1);

}


I used &globvar as the global variable in this case.

If you want to read more on these kind of tricks I believe the file FIFO and variables in DinkC has more about this. Also storing multiple variables in one variable is specifically discussed in SuperVar.

Also note that the panic about running out of variables is a total non-issue for most people, and an issue that can easily be mitigated by not going overboard with unnecessary globals for most others. I personally have never even been close to running out. If the global you're trying to save is only used on a single screen I recommend using the fake editor_seq / editor_frame trick instead of a global, this is significantly easier than using supervariables (and it doesn't use a global at all!).