Why i can't?

Discussion in 'Server and Client Scripting' started by Blastcore, Feb 4, 2011.

  1. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Why i have to do this:

    Code (Javascript):
    1.  
    2. ({
    3. afterNewMessage : function(msg) {
    4.     if(msg == "Script Check: OK") {
    5.         this.expForNextLevel(345);
    6.     }
    7. }
    8. ,  
    9. expForNextLevel : function(pokeNum) {
    10.         var Erratic = [347, 348, 334, 411, 366, 346, 408, 349, 456, 368, 367, 345, 457, 350, 290, 291, 409, 292, 410, 333, 313, 335];
    11.     var Fast = []
    12.     var MediumFast = []
    13.     var MediumSlow = []
    14.     var Slow = []
    15.     var Fluctuating = []
    16.    
    17.     if(Erratic.indexOf(pokeNum) != -1) {
    18.         var Group = "Erratic";
    19.     }
    20.     if(Fast.indexOf(pokeNum) != -1) {
    21.         var Group = "Fast";    
    22.     }
    23.     if(MediumFast.indexOf(pokeNum) != -1) {
    24.         var Group = "Medium Fast";
    25.     }  
    26.     if(MediumSlow.indexOf(pokeNum) != -1) {
    27.         var Group = "Medium Slow";
    28.     }  
    29.     if(Slow.indexOf(pokeNum) != -1) {
    30.         var Group = "Slow";
    31.     }  
    32.     if(Fluctuating.indexOf(pokeNum) != -1) {
    33.         var Group = "Fluctuating";
    34.     }  
    35.     sys.sendAll(Group);
    36. }
    37. })
    38.  
    And not this?

    Code (Javascript):
    1.  
    2. ({
    3. afterServerStartUp : function() {
    4.        var Erratic = [347, 348, 334, 411, 366, 346, 408, 349, 456, 368, 367, 345, 457, 350, 290, 291, 409, 292, 410, 333, 313, 335];
    5.     var Fast = []
    6.     var MediumFast = []
    7.     var MediumSlow = []
    8.     var Slow = []
    9.     var Fluctuating = []
    10. }
    11. ,
    12. afterNewMessage : function(msg) {
    13.     if(msg == "Script Check: OK") {
    14.         this.expForNextLevel(345);
    15.     }
    16. }
    17. ,  
    18. expForNextLevel : function(pokeNum) {
    19.    
    20.     if(Erratic.indexOf(pokeNum) != -1) {
    21.         var Group = "Erratic";
    22.     }
    23.     if(Fast.indexOf(pokeNum) != -1) {
    24.         var Group = "Fast";    
    25.     }
    26.     if(MediumFast.indexOf(pokeNum) != -1) {
    27.         var Group = "Medium Fast";
    28.     }  
    29.     if(MediumSlow.indexOf(pokeNum) != -1) {
    30.         var Group = "Medium Slow";
    31.     }  
    32.     if(Slow.indexOf(pokeNum) != -1) {
    33.         var Group = "Slow";
    34.     }  
    35.     if(Fluctuating.indexOf(pokeNum) != -1) {
    36.         var Group = "Fluctuating";
    37.     }  
    38.     sys.sendAll(Group);
    39. }
    40. })
    41.  
     
  2. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    (If I'm understanding right what are you talking about) Because those vars are local to a function, and those are different functions :} Make them global if you need to (maybe with a prefix).
     
  3. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    The 'var' keyword says it's a local variable. If you want to use it in another function you need to make it global, so remove the "var" keyword.

    Edit: Ninjad ^^
     
  4. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Ninjad? xD

    Well, oky. :P

    Lmao, both of you commented at same minute :PP
     
  5. Mystra

    Mystra Active Member

    Joined:
    Jul 12, 2010
    Messages:
    1,389
    Likes Received:
    4
    I suggest to leave var as is but move them all outside of ({ }) though :}

    ___
    Ninja achievement unlocked!