Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Slasher on Mon 05/11/2012 14:08:58

Title: SOLVED: Stop label going beyond 0
Post by: Slasher on Mon 05/11/2012 14:08:58
I'm building a 'Shop'(GUI) and the products 'Cancel' button takes the last order of a particular product away.

This works but I have hit a snag:

How can I stop 'Cancel' button showing less then 0 when repeatedly clicked?

A set of arrows cost 6 Elfers.

In Rep Exec Always:
Code (AGS) Select

LarrowsQ.Text = String.Format("%d", arrows); // update for each time a set of arrows is added / subtracted.
Larrowsprice.Text = String.Format("%d", arrows *6); // for each time a set of arrows is added 6 Elfers is charged.


Buy Button script:
Code (AGS) Select
function Bbuy1_OnClick(GUIControl *control, MouseButton button)
{
arrows=(arrows+1); // When a set of arrows is bought.
arrowsQ=(arrowsQ +6); // then added to arrowprice
}


Cancel Button script:
Code (AGS) Select

function Bcancel1_OnClick(GUIControl *control, MouseButton button)
{
arrows=(arrows-1);    // take away a set of arrows/
Larrowsprice.Text = String.Format("%d", arrows -6);   // take off the price of a set of arrows.
}


Could you please lend a hand?

Thank you


Title: Re: Stop label going beyond 0
Post by: MurrayL on Mon 05/11/2012 14:32:49
I'm not entirely sure what your code is doing here - in particular, why are you setting Larrowsprice.Text in the cancel button click event? It's set in repeatedly_execute_always, so it'll just get overwritten again next frame.

In any case, you can easily stop a number from going below zero like this:

Code (AGS) Select

function Bcancel1_OnClick(GUIControl *control, MouseButton button)
{
    if(arrows>0){
        arrows = (arrows-1); // This could also be done with 'arrows--;' or 'arrows-=1;'
        Larrowsprice.Text = String.Format("%d", arrows -6);
    }
}
Title: Re: Stop label going beyond 0
Post by: Slasher on Mon 05/11/2012 14:46:21
Hi

Quotewhy are you setting Larrowsprice.Text in the cancel button click event?
Because it's a Cancel last order and it is done via a button. Once pressed it takes away 6 elfins from the total and lowers the quantity by 1. The cancel button now stops at 0.

They should be and now are in Rep Exec Always.

A combination of what I have plus your reply seems to work.. I will have to test it as few times before I mark this topic solved.

cheers MurrayL

This is a rough draft of what I am tying to implement:

(http://i1181.photobucket.com/albums/x423/qikfire/SHOP_IMAGE.png)

EDIT: This part of the 'SHOP' has been solved.


Title: Re: Stop label going beyond 0
Post by: MurrayL on Mon 05/11/2012 21:45:22
Glad to hear it's working.

Quote from: slasher on Mon 05/11/2012 14:46:21
Quotewhy are you setting Larrowsprice.Text in the cancel button click event?
Because it's a Cancel last order and it is done via a button. Once pressed it takes away 6 elfins from the total and lowers the quantity by 1. The cancel button now stops at 0.

My query was more about how your script looks like it's doing some unnecessary work.

Your cancel button does this when it's clicked:
Code (AGS) Select
Larrowsprice.Text = String.Format("%d", arrows -6);

But then, in 'repeatedly_execute_always', you're also running this line every frame:
Code (AGS) Select
Larrowsprice.Text = String.Format("%d", arrows *6);

There's no need to set the Text property of the label when you click the cancel button, because it's going to be set to the correct value by the line in repeatedly_execute_always on the very next frame.
Title: Re: Stop label going beyond 0
Post by: Slasher on Mon 05/11/2012 21:48:24
That was a silly oversight on my part.

All labels are in Rep Exec Always.

Only variables are on button clicks.

All working fine now and 'Check-Out' works to:

Code (AGS) Select
cELF.Say("You have bought: Arrow packs %d and Basket packs: %d.",arrows, baskets);
cELF.Say("That will be %d Elfers please",totalcost);
arrows=0;
baskets=0;


(nod)

Mind you, it will lead to other queries.

cheers

slasher