The Dink Network

Variable Isolation

May 3rd 2010, 11:57 AM
knightgl.gif
castman
Peasant He/Him Brazil
Some day I'll finish my mod... Some day... 

I've been trying to understand how does that crazy 'Dink's Trunk' script works and I've got this question:

How can I isolate a variable number?

As some know, that script saves in a single variable every Dink's items with a monster number Ex.: 126532485

So, the third number from left to right is the number of bombs (maybe not, just an example) as we can see it's 4.

How can I take that number 4 out from that others and use it?

&trunk = 126532485;

&bomb = ???;
May 3rd 2010, 01:37 PM
slimeg.gif
metatarasal
Bard He/Him Netherlands
I object 
You can use the fact that variables are integers. The largest value for an integer is 2,147,483,647 so you have 10 digits to work with. So you'll do something like this: (Note: I haven't looked this up, so this isn't guaranteed to work...)

&trunk = 126532485;
int &temp1 = &trunk;
int &temp2 = &trunk;
int &loopvar1 = 1000000000;
int &loopvar2 = 11;
loop:
&loopvar2 -= 1;
&temp1 / &loopvar;
if (&loopvar2 = <number you want> )
{
&bomb = &temp1;
}
&temp2 -= &temp1;
&temp1 = &temp2;
&loopvar1 / 10;
if (&loopvar2 > 0)
goto loop;


<number you want> is the number you want counted from the right. So in this example this would be 3.

EDIT: This script does look a bit ugly... I wonder if there's an easier way...
May 3rd 2010, 01:46 PM
bonca.gif
Erwin
Peasant He/Him Netherlands
Friendship is magic 
Attach this script to a sprite and talk to it to test it.

void talk (void)
{
int &n4 = 1;

//&n3 is the number you want counted from right to left.
//In this example we want the 4 in 126532485 which is the 3rd number.
int &n3 = 3;

if (&n3 > 1)
{
loop:

&n4 *= 10
&n3 -= 1

if (&n3 > 1)
{
goto loop;
}

}

//&n1 is the number you're taking a number from being 126532485 in this example
int &n1 = 126532485;
int &n2 = &n1;

&n2 /= &n4;

&n4 *= 10;
&n1 /= &n4;

&n1 *= 10;

&n2 -= &n1;

//Showing the outcome.
say_stop("&n2", &current_sprite);
}


Just as I finished this I saw that metatarasal beat me to it so I haven't really checked his script yet.
May 4th 2010, 07:14 AM
wizardb.gif
Kyle
Peasant He/Him Belgium
 
Interesting... I wrote a script that does the exact same thing, but as a helper function I can call from anywhere and pass through the variable and digit to extract, but it's much longer and has much more redundant code. Gonna take a look at your scripts and see if I can simplify it
May 4th 2010, 11:59 AM
slayer.gif
rabidwolf9
Peasant He/Him United States
twitch.tv/rabidwolf9 
There was also a script in someone's FIFO and Variables in DinkC tutorial under the Multiple values in one variable section. Really useful.
May 5th 2010, 08:09 AM
fairy.gif
Someone
Peasant He/Him Australia
 
Thanks Rabid