Results 1 to 15 of 15

Thread: Defining x and y as global variables

  1. #1
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default Defining x and y as global variables

    Is there anything wrong with this? I personally like to define them as global variables as it saves me the effort of defining them nearly every other function.

    However, I've seen some members disapprove of this....

  2. #2
    Join Date
    Feb 2006
    Posts
    3,044
    Mentioned
    4 Post(s)
    Quoted
    21 Post(s)

    Default

    Quote Originally Posted by abu_jwka View Post
    Is there anything wrong with this? I personally like to define them as global variables as it saves me the effort of defining them nearly every other function.

    However, I've seen some members disapprove of this....
    Just try to learn declaring them inside function because Global variables can mess up. If you use same Variable which is global over and over again. You can accidentally mess up something in your script.

    (Bad Example. In some function X is declared as 10 and when next function comes it's still 10 because something went wrong in current function)


    ~Home
    Last edited by Home; 04-09-2012 at 09:53 PM.

  3. #3
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I'm not okay with this

    It's bad practice to declare global variables. Especially x, y and i. Those are used by a lot of functions and procedures. If for some reason one procedure changes it, the script might go wrong. Also it is bad practice for the eye, these are only used within the function and can be discarded afterwards. Not globally used to safe information where it is important that it isn't lost add the end of the function.
    Working on: Tithe Farmer

  4. #4
    Join Date
    Oct 2008
    Location
    C:\Simba\Includes\
    Posts
    7,566
    Mentioned
    19 Post(s)
    Quoted
    180 Post(s)

    Default

    I really hate global variables, I usually try to stay away from them when I can.
    Away for awhile, life is keeping me busy. | Want to get my attention in a thread? @Kyle Undefined; me.
    { MSI Phoenix || SRL Stats Sigs || Paste || Scripts || Quotes || Graphics }

    When posting a bug, please post debug! Help us, help you!

    I would love to change the world, but they won't give me the source code. || To be the best, you've got to beat the rest. || Logic never changes, just the syntax.
    If you PM me with a stupid question or one listed in FAQ, or about a script that is not mine, I will NOT respond.


    SRL is a Library of routines made by the SRL community written for the Program Simba. We produce Scripts for the game Runescape.


  5. #5
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Ok thanks guys - I'll stop doing it then

  6. #6
    Join Date
    Nov 2011
    Location
    United states
    Posts
    516
    Mentioned
    1 Post(s)
    Quoted
    1 Post(s)

    Default

    Hehe I think you are talking about people disapproving of this was in my app I like global variables, but that it cause in my opinion it is easier if I need the values later on in the script. Idc if its frowned upon, if it works it is good...... or least thats how i feel however I'm sure it will catch up with me somewhere along the way in the programming life... so I should probably stop aswell

  7. #7
    Join Date
    Dec 2006
    Location
    Sweden
    Posts
    10,812
    Mentioned
    3 Post(s)
    Quoted
    16 Post(s)

    Default

    Quote Originally Posted by getdropped69 View Post
    however I'm sure it will catch up with me somewhere along the way in the programming life
    It will And then you wonder "why is my script doing this! it's so messed up!", you realize your globals are biting you in the butt. I used to use globals a lot, but you should really avoid them as much as you can, especially for coords and such.


    Send SMS messages using Simba
    Please do not send me a PM asking for help; I will not be able to help you! Post in a relevant thread or make your own! And always remember to search first!

  8. #8
    Join Date
    Oct 2010
    Posts
    1,255
    Mentioned
    0 Post(s)
    Quoted
    15 Post(s)

    Default

    I really don't get this actually (even though i do declare mine locally).

    I don't see how anything can mess up really unless you code like a cheeseball.

    Why would you use X & Y in a function before declaring them anyways, either globally or locally?

    Like who seriously does a FindColor (and respective family) function to define X, Y or a TPA of them and doesn't include the respective actions in it's own begin/end block.

    Like if you do a:
    FindColorSpiral(X, Y, ...)
    Mouse(X, Y, ...)

    Without checking, then it would mess up globally or locally anyways.
    I'm back

  9. #9
    Join Date
    Feb 2012
    Posts
    168
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    IMO globals should be used only for progress variables, (With some rare exceptions).

  10. #10
    Join Date
    Jul 2010
    Posts
    1,115
    Mentioned
    1 Post(s)
    Quoted
    10 Post(s)

    Default

    i would only declare them globally if its value is used in other procedures/functions. you can declare them globally if you want, but locally makes it so it shouldnt go wrong by starting with a bad value.

  11. #11
    Join Date
    Feb 2007
    Location
    Het ademt zwaar en moedeloos vannacht.
    Posts
    7,211
    Mentioned
    26 Post(s)
    Quoted
    72 Post(s)

    Default

    It's a really bad coding practice, there are a few good reasons why every comp sci class actively discourages using global vars. One of the reasons is, it starts with x and y and i, after that you do like x1, x2, y1 and y2 and you end up having only global vars.
    Ideally you can cut and paste a function into a new script and it'll work. Won't work with stuff like this.
    Also I'd like to invite everyone to also vote No at member apps if they spot this.
    I made a new script, check it out!.

  12. #12
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by Markus View Post
    Also I'd like to invite everyone to also vote No at member apps if they spot this.
    I got away with it then

  13. #13
    Join Date
    Oct 2011
    Location
    UK
    Posts
    1,322
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    I would get as far away from global variables as possible look at the following case:
    Simba Code:
    program new;

    function DoubleThenAdd(a, b : integer) : integer;
    Var
      i, x, y : integer;
    begin
      x := a + a;
      y := b + b;
      Result := x + y;
    end;


    procedure DoSomething;
    Var
      i, x, y : integer;
    begin
      for i := 0 to 5 do
        begin
          x := x + 2;
          y := y - 1;
          WriteLn(DoubleThenAdd(x, y));
        end;
    end;

    begin
      DoSomething;
    end.

    Simba Code:
    program new;

    Var
      i, x, y : integer;

    function DoubleThenAdd(a, b : integer) : integer;
    begin
      x := a + a;
      y := b + b;
      Result := x + y;
    end;


    procedure DoSomething;
    begin
      for i := 0 to 5 do
        begin
          x := x + 2;
          y := y - 1;
          WriteLn(DoubleThenAdd(x, y));
        end;
    end;

    begin
      DoSomething;
    end.

    They have different outputs just because of global/local vars, a very contrived case but could happen

  14. #14
    Join Date
    Feb 2007
    Location
    Het ademt zwaar en moedeloos vannacht.
    Posts
    7,211
    Mentioned
    26 Post(s)
    Quoted
    72 Post(s)

    Default

    Also a bad example. Though pascalscript inits variables at 0, you shouldn't rely on that.
    Simba Code:
    program new;

    function DoubleThenAdd(a, b : integer) : integer;
    Var
      i, x, y : integer;
    begin
      x := a + a;
      y := b + b;
      Result := x + y;
    end;


    procedure DoSomething;
    Var
      i, x, y : integer;
    begin
      x := 0;
      y := 0;
      for i := 0 to 5 do
        begin
          x := x + 2;
          y := y - 1;
          WriteLn(DoubleThenAdd(x, y));
        end;
    end;

    begin
      DoSomething;
    end.
    I made a new script, check it out!.

  15. #15
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Only time u use globals in Pascal, is if u need the value initialized from another function.. Or want the value preset before it hits ur second function since Pascal doesn't have pointers and all.. Well it does. Just not Simba.

    In other languages like in the real world, u don't need globals ALL the time since u can use pointers.. but sometimes u do.
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •