Jump to content

LSL & Math


Casey Conundrum
 Share

You are about to reply to a thread that has been inactive for 654 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello all!

I needing some help on a script, I know it has to be pretty simple but I'm just not seeing it my self. I'm trying to return a positive when subtracting from a given number even when its returns a negative number. I just can't rap my head around lsl and math.

I was able to get it to this point

integer subtract(integer x, integer y)
{
    if( x < y ) return 0;
    else return x - y;
}

default
{
    state_entry()
    {
        llOwnerSay((string)subtract(10, 11));
    }
}

but the system I working on works on positive returns and returning 0 throws off my system when I just want to remove what the system has. IE Object A is asking to remove 11 from Object B, but Object B only has 10 Units, So Object B would send back it was only able to remove 10 Units so it would tell Object A 10 was removed.

Link to comment
Share on other sites

Thanks Wulfie seems to be working so far with this code. But if I run into trouble I will ask again on it :P

integer subtract(integer x, integer y)
{
    integer a;
    if(x < y)
    {
        integer z = ShowAbsolute(x - y);
        a = y - z;
        return a;
    }
    else
    {
        return x - y;
    }
}

integer ShowAbsolute(integer inputInteger)
{
    integer A = llAbs(inputInteger);
    return A;
}

default
{
    state_entry()
    {
        llOwnerSay((string)subtract(10, 11));
    }
}

 

Link to comment
Share on other sites

7 hours ago, Casey Conundrum said:

Object A is asking to remove 11 from Object B, but Object B only has 10 Units, So Object B would send back it was only able to remove 10 Units so it would tell Object A 10 was removed.

So within your function, does this mean x is what object B actually has and y is what it's being told to remove? If so, then just return x instead of zero when x is less than y.

  • Like 1
Link to comment
Share on other sites

Oh, you want it to work like a bank account, where you can't withdraw more if your balance is zero. I think.

//  Minimum value of two integers
integer min(integer x, integer y)
{   if (x < y)  { return x; } else { return y; }
}

//  Withdraw from balance
integer gBalance = 100;

//  Withdraw from balance
integer withdraw(integer n) {
    integer withdrawal = min(n, gBalance);  // limit of what you can withdraw
    gBalance -= withdrawal; // will not go negative
    llOwnerSay("Tried to withdraw " + (string)n + " received " + (string) withdrawal);
    return withdrawal;
}

default
{
    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
        withdraw(25);
        withdraw(50);
        withdraw(50);   // overdrawn
    }
}

This should work.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 654 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...