Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Slasher on Wed 17/01/2018 15:41:16

Title: Maths expert required...
Post by: Slasher on Wed 17/01/2018 15:41:16
Hi

I have a maths problem that needs solving...

Code (ags) Select

  LWeight.Text=String.Format("%d",Weight);
  LWeightOZ.Text=String.Format("%d",Weight_Ozs);


It's lbs and ozs.

16ozs equals 1lb

I have it so that when ozs reaches 16 then lbs go up by 1.

What is NOT happen is any remainder ozs should stay in the ozs label.

Ex

1lbs 9ozs then you add 8ozs which gives 2lbs 1oz.

I am loosing the 1oz so labels show 2lbs 0ozs

Maths expert required...

EDIT
Trying this

Code (ags) Select

if(Weight_Ozs>=16){
Weight_Ozs-=16;
Weight+=1;


EDIT
Can end up with - ozs!

aghhhhhhhhhh



Title: Re: Maths expert required...
Post by: Crimson Wizard on Wed 17/01/2018 16:13:17
My advice - keep everything in the "lowest" units (ozs ?) only, - Weight_Ozs.

There is a very useful math operation, called "remainder of division", in AGS it's done using "%" operator. E.g. 20 % 16 = 4.
Also, what is very useful, when you divide integer numbers, any fractions are discarded. E.g. with integers 20 / 16 = 1.

Use these two "features". When you need to show weight components on screen, or use them in some other calculations, do this:

int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining

LWeight.Text = String.Format("%d", lbs);
LWeightOZ.Text = String.Format("%d", osz);
Title: Re: Maths expert required...
Post by: Slasher on Wed 17/01/2018 16:23:11
error


Code (ags) Select
int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining


GlobalScript.asc(7): Error (line 7): Expected integer value after '='
Title: Re: Maths expert required...
Post by: Crimson Wizard on Wed 17/01/2018 16:26:03
Quote from: Slasher on Wed 17/01/2018 16:23:11
error


Code (ags) Select
int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining


GlobalScript.asc(7): Error (line 7): Expected integer value after '='


Do you have variables with same names used already anywhere? Is Weight_Ozs defined before this code?

E: Where did you put this code anyway?
Don't say you just put it in the beginning of GlobalScript, outside of all functions... :(
Title: Re: Maths expert required...
Post by: Jack on Wed 17/01/2018 16:35:15
That error means you're trying to define it in the script rather than one of the script's functions. You can only make assignments outside functions referencing engine constants.

In other words, the same code will work if you do it in a function.
Title: Re: Maths expert required...
Post by: Slasher on Wed 17/01/2018 16:43:01
Quote from: Crimson Wizard on Wed 17/01/2018 16:26:03
Quote from: Slasher on Wed 17/01/2018 16:23:11
error


Code (ags) Select
int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining


GlobalScript.asc(7): Error (line 7): Expected integer value after '='


And the line 7 is?...
Do you have variables with same names used already anywhere? Is Weight_Ozs defined before this code?

global pane
in all the button that add subtract ozs..:(


Title: Re: Maths expert required...
Post by: Crimson Wizard on Wed 17/01/2018 16:52:21
Quote from: Slasher on Wed 17/01/2018 16:43:01
global pane
in all the button that add subtract ozs..:(

I am sorry, but I do not understand what you are saying.
Could you show an example of actual script that uses these calculations?
Title: Re: Maths expert required...
Post by: Slasher on Wed 17/01/2018 16:56:23
To all buttons (variant values)

Add / Subtract values

Code (ags) Select


function Button6_OnClick(GUIControl *control, MouseButton button)
{
if(Button6.NormalGraphic==7){
  Button6.NormalGraphic=8; 
  Weight +=3;
  Weight_Ozs +=2;
  Axe=true;
  }
  else if(Button6.NormalGraphic==8){
  Button6.NormalGraphic=7; 
  Weight -=3;
  Weight_Ozs -=2;
  Axe=false;

}
}
Title: Re: Maths expert required...
Post by: Crimson Wizard on Wed 17/01/2018 17:06:13
But where did you put the code that I suggested? Where is it in the script?
Title: Re: Maths expert required...
Post by: Slasher on Wed 17/01/2018 17:17:57

Top of global

int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining


Rep exe always

  LWeight.Text = String.Format("%d", lbs);
  LWeightOZ.Text = String.Format("%d", osz %16);



Originally in Global button function

function Button6_OnClick(GUIControl *control, MouseButton button)
{
if(Button6.NormalGraphic==7){
  Button6.NormalGraphic=8; 
  Weight +=3;
  Weight_Ozs +=2;
  Axe=true;
  }
  else if(Button6.NormalGraphic==8){
  Button6.NormalGraphic=7; 
  Weight -=3;
  Weight_Ozs -=2;
  Axe=false;
}
}




Title: Re: Maths expert required...
Post by: Crimson Wizard on Wed 17/01/2018 17:33:27
Okay I see now, this is what Jack thought and posted in his reply above.

Quote from: Slasher on Wed 17/01/2018 17:17:57
Top of global

int lbs = Weight_Ozs / 16; // how many full lbs are there
int osz = Weight_Ozs % 16; // how many osz remaining


This is wrong, and not what I meant. I did not suggest to add these as global variables.
You cannot use math expression when setting global variables value anyway; and that does not make sense too, because variables won't have their values automatically updated when Weight_Ozs changes.


So, this is not about math anymore, now it is only about scripting.


First, start with the connection between different values.
These are two functions that return LBS and OSZ remainer from Weight_Ozs:
Code (ags) Select

// Returns full LBS from the Weight_Ozs
int GetWeightLBS()
{
    return Weight_Ozs / 16;
}
// Returns number of OSZ remaining after subtracting full LBS from the Weight_Ozs
int GetWeightOSZ()
{
    return Weight_Ozs % 16;
}


Now you can use these functions like:
Code (ags) Select

  LWeight.Text = String.Format("%d", GetWeightLBS());
  LWeightOZ.Text = String.Format("%d", GetRemainingOSZ());



What you have to do after, is remove Weight variable, that stores full LBS, and keep the weight only in Weight_OSZ. That is, for example, when you increase by 1 osz you do "Weight_OSZ += 1", and when you increase by 1 lbs you do "Weight_OSZ += 16". (I hope I got it right, because I do not know osz/lbs system well)

Example:
Code (ags) Select

function Button6_OnClick(GUIControl *control, MouseButton button)
{
if(Button6.NormalGraphic==7){
  Button6.NormalGraphic=8; 
  Weight_Ozs += 3 * 16 + 2; // 3 lbs and 2 osz
  Axe=true;
  }
  else if(Button6.NormalGraphic==8){
  Button6.NormalGraphic=7; 
  Weight_Ozs -= 3 * 16 + 2;
  Axe=false;
}
}


Which could be simplified even further to make weight values easier to read and fix, for example:
Code (ags) Select

#define WEIGHT_OF_AXE (3 * 16 + 2)

function Button6_OnClick(GUIControl *control, MouseButton button)
{
   if ...
      Weight_Ozs += WEIGHT_OF_AXE;
   else if ...
      Weight_Ozs -= WEIGHT_OF_AXE;
}
Title: Re: Maths expert required...
Post by: Slasher on Wed 17/01/2018 17:47:12
Cheers Crimson

I'll give it a whirl...

thans