Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: steptoe on Sat 03/12/2011 19:02:21

Title: SOLVED: Label math displays
Post by: steptoe on Sat 03/12/2011 19:02:21
Hi all you mathematical  geniuses

Is it possible and if so how to do the following:

Int A: adds £ per correct answer and displays total on Label A.

IE £1000 per question correct = 5 correct answers= £5000 displayed on Label A.

After which:

Int B: Label B takes the total of Label A and x 3 to it and displays it (£15000) on Label B.

Meanwhile:

Int C: Label C takes the total of Label A and / 5 from it and displays it on Label C.

IE Label A £5000 Label B £15000 Label C 1000

Grateful if anyone can help

cheers




Title: Re: Label math displays
Post by: Khris on Sat 03/12/2011 19:26:15
function UpdateLabels(int money) {
  a += money;
  LabelA.Text = String.Format("£%d", a);
  LabelB.Text = String.Format("£%d", a*3);
  LabelC.Text = String.Format("£%d", a/5);
}

  ...
  if (question was answered correctly) UpdateLabel(1000);
  ...


Good thing I studied mathematics for a few years :=
Title: Re: Label math displays
Post by: steptoe on Sat 03/12/2011 19:43:46
Good job someone did  :=

I'll give it a whirl

cheers Khris

EDIT: Created 3 LABELS: LabelA   LabelB  LabelC
Made 3 Ints A B C..

I put Int A at 5000 start.

Put you code in room yet only A (5000) shows whilst others display 0


function UpdateLabels(int money) {
 A += money;
 LabelA.Text = String.Format("£%d", A);
 LabelB.Text = String.Format("£%d", A*3);
 LabelC.Text = String.Format("£%d", A/5);


and


function room_AfterFadeIn()
{
 
LabelA.Text = String.Format("£: %d", A);

LabelB.Text = String.Format("£: %d", B);

LabelC.Text = String.Format("£: %d", C);

}


:(





Title: Re: Label math displays
Post by: Khris on Sat 03/12/2011 20:06:08
You have to call the function for it to actually do something.
Remove the three lines you put in AfterFadein, you can also delete B and C, you don't need them.

Then put this line in AfterFadein:

  UpdateLabels(0);
Title: Re: Label math displays
Post by: steptoe on Sat 03/12/2011 20:38:18
Hi

wont take updateLabels(0) in after fadein (UNDEFINED error)

I can definately see the advantage of updating all labels in one script.

It makes it more lengthy if i update all labels frequently like:

Example after awarding £ To Int A:


LabelA.Text = String.Format("£: %d", A);

LabelB.Text = String.Format("£: %d", A*3);

LabelC.Text = String.Format("£: %d", A/5);


Have deleted ints B and C and just have A.

Works but is repetitive. Could add (Labels) to globally Rep Exe Always.

cheers


Title: Re: Label math displays
Post by: monkey0506 on Sat 03/12/2011 20:54:23
You need to post the exact code you are using and the exact error message you are getting. Coz the code Khris posted is fine.
Title: Re: Label math displays
Post by: steptoe on Sun 04/12/2011 06:43:03
Hi Monkey/Khris

I have INT 'A' set at 1000 for test run as well as 3 Labels A, B and C.

I then used this as per Khris which works:


function UpdateLabels(int money) {
 A += money;
 LabelA.Text = String.Format("£%d", A);
 LabelB.Text = String.Format("£%d", A*3);
 LabelC.Text = String.Format("£%d", A/5);
}


and this which gives an error:


function room_AfterFadeIn()
{
 UpdateLabels(0); // Undefined token 'UpdateLabels'

}


Using AGS 3.2.1.111



Title: Re: Label math displays
Post by: monkey0506 on Sun 04/12/2011 07:00:00
Functions need to be imported if they're going to be used in other scripts. So if you put the UpdateLabels function in GlobalScript.asc, then you would need to put the following in the GlobalScript.ash header file:

import function UpdateLabels(int money);

Then it will compile.

Oh, and just to make sure you don't get them confused, never put a function into a header, only put imports, and struct and enum definitions in the ASH header files (nothing else belongs there!).
Title: Re: Label math displays
Post by: steptoe on Sun 04/12/2011 07:11:38
Hi Monkey

took out the Room update Label part and added import update Labels as instructed.

Global ash at top:


import function UpdateLabels(int money);


and this in Global asc:


function repeatedly_execute_always() {
 
//LABELS £

 LabelA.Text = String.Format("£: %d", A);
 LabelB.Text = String.Format("£: %d", A*3);
 LabelC.Text = String.Format("£: %d", A/5);

}


In room script:

A=(A+9000);


Works a treat as I only need to adjust Int A.

Solved.

Cheers Guys

:=