SOLVED: Can someone take a look at scripts for me and advise of problem?

Started by Slasher, Tue 06/11/2012 18:31:49

Previous topic - Next topic

Slasher

Hi

I need a hand regarding this shop table.

Adding and cancelling items works fine but there does seem to be a problem.

You start off with 10 Elfers (money).

I'll try and explain the problem.

It's to do with once the Elfers in the label (bottom) get down to 0 (after buying items) and let's say you decide to cancel all the items and Elfers are again at 10, when you check-out you are told you have 0 Elfers left when you should have 10! Therefore when you re open the shop you can't buy.

Could someone take a look at the scripts and pull them apart and hopefully see any problem areas with advice to correct.

Here is draft shop:


Rep Exec Always
Code: AGS
} 
  // SHOP
  
  //ARROWS
  LarrowsQ.Text = String.Format("%d", arrows);
  Larrowsprice.Text = String.Format("%d", arrows *6);
 
  //BASKETS
  LbasketsQ.Text = String.Format("%d", baskets);
  Lbasketsprice.Text = String.Format("%d", baskets *2);
 
  //KNIVES
  LknivesQ.Text = String.Format("%d", knives);
  Lknivesprice.Text = String.Format("%d", knives *4);
  
  //ROPES
  
  LropesQ.Text = String.Format("%d", ropes);
  Lropesprice.Text = String.Format("%d", ropes *5);
  
  //TORCH
   LtorchQ.Text = String.Format("%d", torch);
  Ltorchprice.Text = String.Format("%d", torch *3);
  
   //GRAPPLE
  LgrappleQ.Text = String.Format("%d", grapple);
  Lgrapprice.Text = String.Format("%d", grapple *10);
  
  //TOTAL PRICING 
  LTotalPrice.Text = String.Format("%d", arrows *6 + baskets *2 + knives *4 + ropes *5 + torch *3 + grapple *10);
  totalcost=arrows *6 + baskets *2 + knives *4 + ropes *5 + torch *3 + grapple * 10;

  Lelfers.Text = String.Format("%d", Elfers);

  
 // STOP COUNT IF OVER 10
 
 if (Elfers >= 10)
 {
   
   Elfers= 10;
   Lelfers.Text = String.Format("%d", Elfers);
 
 }  
 
 // STOP COUNT IF OVER 10 
 
  if (totalcost >=  10)
 {
  totalcost= 10;
 
 } 
 
 if (Elfers <=  0)
 {
  Elfers= 0;
 } 
 }


Baskets BUY button:

Code: AGS
function Bbuy2_OnClick(GUIControl *control, MouseButton button)
{
 if (Elfers < 2)
  {
 Display("You do not have enough Elfers!");
 
}
 else
{
  
 baskets=(baskets+1);
 Elfers=(Elfers -2);
}
}


Baskets Cancel button:

Code: AGS
function Bcancel2_OnClick(GUIControl *control, MouseButton button)
{
  basketsQ=(baskets-1);
  Elfers=(Elfers +2);

 if(baskets>0)
 {
 baskets = (baskets-1); // This could also be done with 'arrows--;' or 'arrows-=1;'
 } 
 else if (baskets <=  0)
 {
  totalcost=0;
  Elfers= -1;
 }
 }


Many thanks



Crimson Wizard

First of all, not really relevant to your question, but in my opinion you are putting a lot of excessive tasks to repeatable execute (always!) function.
You basically make game recalculate all those strings over and over again, numerous time per second. Is there a real need for that?
You could simply change label's text in the buttons event handlers: when player clicks on Buy button increase corresponding counter AND change text.
Same goes for Elfers and totalcost counters.

Also instead of writing long calculations for total cost twice, you can make just:
Code: ags

totalcost=arrows *6 + baskets *2 + knives *4 + ropes *5 + torch *3 + grapple * 10;
LTotalPrice.Text = String.Format("%d", totalcost);

You may even put setting totalcost in a separate function and call it from Buy button handlers, something like
Code: ags

function SetTotalCost()
{
   totalcost=arrows *6 + baskets *2 + knives *4 + ropes *5 + torch *3 + grapple * 10;
   LTotalPrice.Text = String.Format("%d", totalcost);
}

Code: ags

function Bbuy2_OnClick(GUIControl *control, MouseButton button)
{
 <...>
{
 baskets=(baskets+1);
 Elfers=(Elfers -2);
 LbasketsQ.Text = String.Format("%d", baskets);
 Lbasketsprice.Text = String.Format("%d", baskets *2);
 SetTotalCost();
}
}

Well, just an advice.



Regarding your problem. You see here:
Code: ags

function Bcancel2_OnClick(GUIControl *control, MouseButton button)
{
  basketsQ=(baskets-1);
  Elfers=(Elfers +2);
 
 if(baskets>0)
 {
 baskets = (baskets-1); // This could also be done with 'arrows--;' or 'arrows-=1;'
 } 
 else if (baskets <=  0)
 {
  totalcost=0;
  Elfers= -1;
 }
 }

The logic in this function seem broken to me.
First, you are increasing Elfers without checking if they were spent already. Is this right? What if player clicks on this button before anything else?
Also you change this basketsQ variable - it does not seem to be used anywhere else. Is this a typo and you meant just "baskets"?
Second, if baskets quantity(?) is 0, then you set your money to -1 for some reason. This means that if player bought no baskets yet, his money will be zeroed.

Slasher

Thanks Crimson that helped a bunch.

It appears to act as it should and with less code.

I have used to add items

Code: AGS
function Bbuy1_OnClick(GUIControl *control, MouseButton button)
{
  if (Elfers < 6)
  {
 Display("You do not have enough Elfers!");
 
}
 else
{
 arrows=(arrows+1);
 Elfers=(Elfers -6);
 LarrowsQ.Text = String.Format("%d", arrows);
 Larrowsprice.Text = String.Format("%d", arrows *6);
 Lelfers.Text = String.Format("%d", Elfers);
 SetTotalCost();
 
}
}


And this to cancel last item:
Code: AGS
function Bcancel1_OnClick(GUIControl *control, MouseButton button)
{
  
  if(arrows ==0) // checks if any in basket
 {
   Display("You have no arrows to cancel.");

 }
 else
  {
  arrows=(arrows-1);
  Elfers=(Elfers +6);
  LarrowsQ.Text = String.Format("%d", arrows);
  Larrowsprice.Text = String.Format("%d", arrows *6);
 
  
}
}



Just one more bit to complete.

After clicking the check-out button and returning to room I need for all the items to be reset to 0.

Code: AGS
 cELF.Say("That will be %d Elfers please",totalcost);
 cELF.Say("If there is anything you want to sell just let me know. I give good prices!");

 arrows=0;
 baskets=0;
 knives=0;
 ropes=0;
 torch=0;
 grapple=0;


This does not seem to occur..

Can a light be shed?

cheers

EDIT: Forgot to update labels...

Code: AGS
cELF.Say("That will be %d Elfers please",totalcost);
 cELF.Say("If there is anything you want to sell just let me know. I give good prices!");

 arrows=0;
 LarrowsQ.Text = String.Format("%d", arrows);
 Larrowsprice.Text = String.Format("%d", arrows);
 baskets=0;
 LbasketsQ.Text = String.Format("%d", baskets);
 Lbasketsprice.Text = String.Format("%d", baskets);
 knivesQ=0;
 LknivesQ.Text = String.Format("%d", knives);
 Lknivesprice.Text = String.Format("%d", knives);
 ropes=0;
 LropesQ.Text = String.Format("%d", ropes);
 Lropesprice.Text = String.Format("%d", ropes);
 torch=0;
 LtorchQ.Text = String.Format("%d", torch);
 Ltorchprice.Text = String.Format("%d", torch);
 grapple=0;
 LgrappleQ.Text = String.Format("%d", grapple);
 Lgrapprice.Text = String.Format("%d", grapple);



Crimson Wizard

You forgot to update your labels text.
Before you used to update them in "repeatedly execute" and they got set to correct values as soon as counters changed. Now you update them by event.

There's a common pattern for such cases, an "initialize" function, which could be useful to contain all logic that prepares the "shop" (or any other similar screen). Here's my suggestion:
Code: ags

function SetupShop()
{
 // Setting counters
 arrows=0;
 baskets=0;
 knives=0;
 ropes=0;
 torch=0;
 grapple=0;
 // Setting texts
 //ARROWS
  LarrowsQ.Text = String.Format("%d", 0);
  Larrowsprice.Text = String.Format("%d", 0);
  //BASKETS
  LbasketsQ.Text = String.Format("%d", 0);
  Lbasketsprice.Text = String.Format("%d", 0);
  //KNIVES
  LknivesQ.Text = String.Format("%d", 0);
  Lknivesprice.Text = String.Format("%d", 0);
  //ROPES
  LropesQ.Text = String.Format("%d", 0);
  Lropesprice.Text = String.Format("%d", 0);
  //TORCH
  LtorchQ.Text = String.Format("%d", 0);
  Ltorchprice.Text = String.Format("%d", 0);
   //GRAPPLE
  LgrappleQ.Text = String.Format("%d", 0);
  Lgrapprice.Text = String.Format("%d", 0);
  // Also totals
  SetTotalCost();
}


EDIT: oh, well, you got it right on your own anyway ;)

Slasher


SMF spam blocked by CleanTalk