Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Calculator script (https://forums.graalonline.com/forums/showthread.php?t=134257576)

Liberated 01-07-2010 07:06 PM

Calculator script
 
I'm working on this calculator script, it's far from done, but i've already come across a problem.
this is my script.
PHP Code:

//#CLIENTSIDE
function onWeaponFired()
{
  new 
GuiWindowCtrl("Calculator")
  {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "165, 200";
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Calculator";
    
300;
    
200;
    
    new 
GuiTextEditCtrl("input")
    {
      
profile GuiBlueTextEditProfile;
      
5;
      
5;
      
width 155;
      
height 30;
      
text "";
    }
    new 
GuiButtonCtrl("Clear")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "Clear";
      
width 35;
      
5;
      
45;
    }
    new 
GuiButtonCtrl("plusmin")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+/-";
      
width 35;
      
45;
      
45;
    }
    new 
GuiButtonCtrl("divide")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "%";
      
width 35;
      
85;
      
45;
    }
    new 
GuiButtonCtrl("multiply")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "X";
      
width 35;
      
125;
      
45;
    }
        new 
GuiButtonCtrl("seven")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "7";
      
width 35;
      
5;
      
75;
    }
    new 
GuiButtonCtrl("eight")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "8";
      
width 35;
      
45;
      
75;
    }
    new 
GuiButtonCtrl("nine")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "9";
      
width 35;
      
85;
      
75;
    }
    new 
GuiButtonCtrl("subtract")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "-";
      
width 35;
      
125;
      
75;
    }
        new 
GuiButtonCtrl("four")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "4";
      
width 35;
      
5;
      
105;
    }
    new 
GuiButtonCtrl("five")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "5";
      
width 35;
      
45;
      
105;
    }
    new 
GuiButtonCtrl("six")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "6";
      
width 35;
      
85;
      
105;
    }
    new 
GuiButtonCtrl("add")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+";
      
width 35;
      
125;
      
105;
    }
        new 
GuiButtonCtrl("one")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "1";
      
width 35;
      
5;
      
135;
    }
    new 
GuiButtonCtrl("two")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "2";
      
width 35;
      
45;
      
135;
    }
    new 
GuiButtonCtrl("three")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "3";
      
width 35;
      
85;
      
135;
    }
    new 
GuiButtonCtrl("equals")
    {
      
profile GuiBlueButtonProfile;
      
height 55;
      
text "=";
      
width 35;
      
125;
      
135;
    }
    new 
GuiButtonCtrl("zero")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "0";
      
width 75;
      
5;
      
165;
    }
    new 
GuiButtonCtrl("dot")
    {
    
profile GuiBlueButtonProfile;
      
height 25;
      
text ",";
      
width 35;
      
85;
      
165;
    }
    
  }
}
function 
one.onAction()
{
  
input.text += 1;
}
function 
two.onAction()
{
  
input.text += 2;
}
function 
three.onAction()
{
  
input.text += 3;
}
function 
four.onAction()
{
  
input.text += 4;
}
function 
five.onAction()
{
  
input.text += 5;
}
function 
six.onAction()
{
  
input.text += 6;
}
function 
seven.onAction()
{
  
input.text += 7;
}
function 
eight.onAction()
{
  
input.text += 8;
}
function 
nine.onAction()
{
  
input.text += 9;


but the problem lies with these things
PHP Code:

function one.onAction()
{
  
input.text += 1;


instead of going 1, 11, 111 it goes, 1, 2, 3, so i need a way to add it as a string, and not as a number. Is there something like a str() function? so it will see it as a string?

cbk1994 01-07-2010 07:21 PM

Try

PHP Code:

function one.onAction() {
  
input.text @= 1;


Also, make sure to read this post.

Immolate 01-07-2010 07:49 PM

I know you're not this far yet but I think Zero's or Pfa's eval() functions will help you out a treat when parsing the input.

Liberated 01-07-2010 07:49 PM

i updated the script, it doesn't do anything with @= tho, no response in the texteditctrl.
also added the clear and "." button.
PHP Code:

//#CLIENTSIDE
function onWeaponFired()
{
  new 
GuiWindowCtrl("Calculator_window")
  {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "165, 200";
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Calculator";
    
300;
    
200;
    
    new 
GuiTextEditCtrl("Calculator_input")
    {
      
profile GuiBlueTextEditProfile;
      
5;
      
5;
      
width 155;
      
height 30;
      
text="0";
    }
    new 
GuiButtonCtrl("Calculator_clear")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "Clear";
      
width 35;
      
5;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_plusmin")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+/-";
      
width 35;
      
45;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_divide")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "%";
      
width 35;
      
85;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_multiply")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "X";
      
width 35;
      
125;
      
45;
    }
        new 
GuiButtonCtrl("Calculator_seven")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "7";
      
width 35;
      
5;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_eight")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "8";
      
width 35;
      
45;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_nine")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "9";
      
width 35;
      
85;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_subtract")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "-";
      
width 35;
      
125;
      
75;
    }
        new 
GuiButtonCtrl("Calculator_four")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "4";
      
width 35;
      
5;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_five")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "5";
      
width 35;
      
45;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_six")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "6";
      
width 35;
      
85;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_add")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+";
      
width 35;
      
125;
      
105;
    }
        new 
GuiButtonCtrl("Calculator_one")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "1";
      
width 35;
      
5;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_two")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "2";
      
width 35;
      
45;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_three")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "3";
      
width 35;
      
85;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_equals")
    {
      
profile GuiBlueButtonProfile;
      
height 55;
      
text "=";
      
width 35;
      
125;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_zero")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "0";
      
width 75;
      
5;
      
165;
    }
    new 
GuiButtonCtrl("Calculator_dot")
    {
    
profile GuiBlueButtonProfile;
      
height 25;
      
text ",";
      
width 35;
      
85;
      
165;
    }
    
  }
}
function 
Calculator_one.onAction()
{
  if(
input.text 0)
  {
    
input.text 1;
  }
  else
  {
    
input.text @= 1;
  }
}
function 
Calculator_two.onAction()
{
  if(
input.text 0)
  {
    
input.text 2;
  }
  else
  {
    
input.text @= 2;
  }
}
function 
Calculator_three.onAction()
{
  if(
input.text 0)
  {
    
input.text 3;
  }
  else
  {
    
input.text @= 3;
  }
}
function 
Calculator_four.onAction()
{
  if(
input.text 0)
  {
    
input.text 4;
  }
  else
  {
    
input.text @= 4;
  }
}
function 
Calculator_five.onAction()
{
  if(
input.text 0)
  {
    
input.text 5;
  }
  else
  {
    
input.text @= 5;
  }
}
function 
Calculator_six.onAction()
{
  if(
input.text 0)
  {
    
input.text 6;
  }
  else
  {
    
input.text @= 6;
  }
}
function 
Calculator_seven.onAction()
{
  if(
input.text 0)
  {
    
input.text 7;
  }
  else
  {
    
input.text @= 7;
  }
}
function 
Calculator_eight.onAction()
{
  if(
input.text 0)
  {
    
input.text 8;
  }
  else
  {
    
input.text @= 8;
  }
}
function 
Calculator_nine.onAction()
{
  if(
input.text 0)
  {
    
input.text 9;
  }
  else
  {
    
input.text @= 9;
  }
}
function 
Calculator_clear.onAction()
{
  
input.text 0;
}
function 
Calculator_dot.onAction()
{
  
input.text @= ".";



Chompy 01-07-2010 07:51 PM

You have to change input.text into Calculator_input.text :)

You should also look into catchevent(), so you have one event to handle all the button clicking

cbk1994 01-07-2010 07:52 PM

You renamed "input" to "Calculator_input", but forgot to rename it in the functions :p

PHP Code:

function Calculator_one.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 1;
  }
  else
  {
    
Calculator_input.text @= 1;
  }


Also, make sure you use "==" when comparing variables instead of "="; a single equality sign is used for assigning values to variables, and two signs are used for comparisons.

edit: damn you Chompy :(

Liberated 01-07-2010 07:57 PM

Quote:

Originally Posted by cbk1994 (Post 1548857)
You renamed "input" to "Calculator_input", but forgot to rename it in the functions :p

PHP Code:

function Calculator_one.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 1;
  }
  else
  {
    
Calculator_input.text @= 1;
  }


Also, make sure you use "==" when comparing variables instead of "="; a single equality sign is used for assigning values to variables, and two signs are used for comparisons.

edit: damn you Chompy :(

ah silly me, completely forgot about that,
and yeah, i know about the == for comparing and = for assigning, i somehow randomly keep forgetting to add the == for comparing.

edit:
awesome, now it's working as planned for this far.

editedit:
i finished it!
PHP Code:

//#CLIENTSIDE
function onWeaponFired()

  
this.number1 0;
  
this.divide 0;
  
this.add 0;
  
this.subtract 0;
  
this.multiply 0;
  new 
GuiWindowCtrl("Calculator_window")
  {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "165, 200";
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Calculator";
    
300;
    
200;
    
    new 
GuiTextEditCtrl("Calculator_input")
    {
      
profile GuiBlueTextEditProfile;
      
5;
      
5;
      
width 155;
      
height 30;
      
text="0";
    }
    new 
GuiButtonCtrl("Calculator_clear")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "Clear";
      
width 35;
      
5;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_plusmin")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+/-";
      
width 35;
      
45;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_divide")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "%";
      
width 35;
      
85;
      
45;
    }
    new 
GuiButtonCtrl("Calculator_multiply")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "X";
      
width 35;
      
125;
      
45;
    }
        new 
GuiButtonCtrl("Calculator_seven")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "7";
      
width 35;
      
5;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_eight")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "8";
      
width 35;
      
45;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_nine")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "9";
      
width 35;
      
85;
      
75;
    }
    new 
GuiButtonCtrl("Calculator_subtract")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "-";
      
width 35;
      
125;
      
75;
    }
        new 
GuiButtonCtrl("Calculator_four")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "4";
      
width 35;
      
5;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_five")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "5";
      
width 35;
      
45;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_six")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "6";
      
width 35;
      
85;
      
105;
    }
    new 
GuiButtonCtrl("Calculator_add")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+";
      
width 35;
      
125;
      
105;
    }
        new 
GuiButtonCtrl("Calculator_one")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "1";
      
width 35;
      
5;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_two")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "2";
      
width 35;
      
45;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_three")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "3";
      
width 35;
      
85;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_equals")
    {
      
profile GuiBlueButtonProfile;
      
height 55;
      
text "=";
      
width 35;
      
125;
      
135;
    }
    new 
GuiButtonCtrl("Calculator_zero")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "0";
      
width 75;
      
5;
      
165;
    }
    new 
GuiButtonCtrl("Calculator_dot")
    {
    
profile GuiBlueButtonProfile;
      
height 25;
      
text ",";
      
width 35;
      
85;
      
165;
    }
    
  }
}
function 
Calculator_one.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 1;
  }
  else
  {
    
Calculator_input.text @= 1;
  }
}
function 
Calculator_two.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 2;
  }
  else
  {
    
Calculator_input.text @= 2;
  }
}
function 
Calculator_three.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 3;
  }
  else
  {
    
Calculator_input.text @= 3;
  }
}
function 
Calculator_four.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 4;
  }
  else
  {
    
Calculator_input.text @= 4;
  }
}
function 
Calculator_five.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 5;
  }
  else
  {
    
Calculator_input.text @= 5;
  }
}
function 
Calculator_six.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 6;
  }
  else
  {
    
Calculator_input.text @= 6;
  }
}
function 
Calculator_seven.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 7;
  }
  else
  {
    
Calculator_input.text @= 7;
  }
}
function 
Calculator_eight.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 8;
  }
  else
  {
    
Calculator_input.text @= 8;
  }
}
function 
Calculator_nine.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 9;
  }
  else
  {
    
Calculator_input.text @= 9;
  }
}
function 
Calculator_zero.onAction()
{
  if(
Calculator_input.text == 0)
  {
    
Calculator_input.text 0;
  }
  else
  {
    
Calculator_input.text @= 0;
  }
}
function 
Calculator_clear.onAction()
{
  
Calculator_input.text 0;
}
function 
Calculator_dot.onAction()
{
  
Calculator_input.text @= ".";
}
function 
Calculator_plusmin.onAction()
{
  
Calculator_input.text -= Calculator_input.text;
}
function 
Calculator_divide.onAction()
{
  
this.number1 Calculator_input.text;
  
this.divide 1;
  
Calculator_input.text "0";
}
function 
Calculator_add.onAction()
{
  
this.number1 Calculator_input.text;
  
this.add 1;
  
Calculator_input.text "0";
}
function 
Calculator_subtract.onAction()
{
  
this.number1 Calculator_input.text;
  
this.subtract 1;
  
Calculator_input.text "0";
}
function 
Calculator_multiply.onAction()

  
this.number1 Calculator_input.text;
  
this.multiply 1;
  
Calculator_input.text "0";
}
function 
Calculator_equals.onAction()
{
  if(
this.divide == 1)
  {
    
this.divide 0;
    
Calculator_input.text = (this.number1 Calculator_input.text);
    
this.number1 0;
  }
  else if(
this.add == 1)
  {
    
this.add 0;
    
Calculator_input.text += this.number1;
  }
  else if(
this.subtract == 1)
  {
    
Calculator_input.text this.number1 Calculator_input.text;
    
this.subtract 0;
    
this.number1 0;
  }
  else if(
this.multiply == 1)
  {
    
Calculator_input.text this.number1 Calculator_input.text;
    
this.number1 0;
    
this.multiply 0;
  }


1 thing i plan on fixing in the future,
right now you cannot do for example: 8*8*8*8=
but you have to do 8*8=64*8=somehugenumber
so you have to press the = after each equation, im still quite happy with it,


All times are GMT +2. The time now is 08:24 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.