9 Replies Latest reply on Apr 19, 2006 7:30 AM by Peter Lorent

    Loading Variables in a Class Function

    DuhChamp Level 1
      I am having problems loading variables from an external text file inside of a class function. The text file has been created properly, and the code that I use in the class is as follows:

      _root.createEmptyMovieClip("texRules", -100);
      loadVariables("texRules.txt", _root.texRules);
      m_Rules = _root.texRules.rules;

      I create an empty movie clip on the root frame named texRules, load the variables into that movie clip with the next line and then set my member string value equal to the text file variable named rules. When I run the program, I get an undefined for my m_Rules.

      Here is my texRules.txt:

      rules=THESE ARE THE TEXAS HOLD'EM RULES.


      Any ideas as to what I am doing incorrectly?
        • 1. Re: Loading Variables in a Class Function
          DuhChamp Level 1
          Ok. I discovered a problem. My variable is loading from the text file to the movie clip, but for some reason it is not loading to my member property (m_Rules). When I view the variables from the debug section, I see that _level0.texRules.rules = "THESE ARE THE TEXAS HOLD'EM RULES.", which is exactly what I want. However, it is not being assigned to my property, m_Rules.

          The only thing I can think of doing is creating an onEnterFrame function, so it will update past the first frame, but that seems excessive and inefficient. Any ideas?
          • 2. Re: Loading Variables in a Class Function
            DuhChamp Level 1
            That didn't seem to help. I did a test that while m_Rules is still undefined, keep trying to assign a value to it, but it just gave me an infinite loop.
            • 3. Re: Loading Variables in a Class Function
              injpix Level 3
              You need to target your _root. Show us your class. Also is there a particular reason why you are using a negative integer for a depth value? I would recommend anything 1 and above. For anything lower then zero is typically reserved for author time depths. To target your _root I usually use the following code;

              class ABC
              {
              public var target:MovieClip;

              public function ABC(target)
              {
              this.target = target;
              }
              }

              //code in _root
              myABC = new ABC(this);


              …while “this” is referring to the _root. Remember the timeline is in the MovieClip class.
              • 4. Re: Loading Variables in a Class Function
                DuhChamp Level 1
                I've opted to use the LoadVars instead of creating an empty movie clip and loading the variables there. Here is my new code.


                public function setRules():Void
                {


                var rulesLoader:LoadVars = new LoadVars();
                rulesLoader.onLoad = function (success:Boolean):Void
                {
                if (success)
                {
                trace(rulesLoader.rules);
                this.m_Rules = rulesLoader.rules;
                }

                else
                {
                trace( "Unable to load external file.");
                }

                }


                rulesLoader.load("texRules.txt");


                }


                Here is where I define my properties of my class:

                class TexHoldem extends Game
                {
                //Class member properties
                //Private Properties
                private var m_Rules:String;
                private var m_Target:MovieClip;


                Here is my constructor:
                public function TexHoldem(target)
                {
                this.m_Target = target;
                super("Texas No Limit Hold'Em", 0, 1);
                setRules();
                }


                The setRules function works fine, the only problem now is that I can't save the variables once the onLoad function goes out of scope. I've thought of modifying it so that it returns a type, but that seems difficult. The two easiest solutions I see is to create an empty text field that holds the string or find a way to get the rulesLoader.rules outside of the onLoad scope. Any suggestions now? Sorry to change the problem on you.
                • 5. Re: Loading Variables in a Class Function
                  injpix Level 3
                  Read this article, this class is a little more powerful versus Flash’s delegate class.
                  http://www.person13.com/articles/proxy/Proxy.htm

                  You can download the class in the very last section of the page. I believe this will solve your problem, your losing scope in the event handler as he mentions.
                  • 6. Re: Loading Variables in a Class Function
                    DuhChamp Level 1
                    Thanks for the site.. I'll let you know if it works.
                    • 7. Re: Loading Variables in a Class Function
                      Peter Lorent Level 2
                      Try the attached class.

                      • 8. Re: Loading Variables in a Class Function
                        injpix Level 3
                        // local reference to the current class
                        var thisObj:TexHoldem=this;

                        Neat-O LuigiL! Do you know the technical name for doing this? Is this a form of casting?
                        • 9. Re: Loading Variables in a Class Function
                          Peter Lorent Level 2
                          Come to think of it, no. I don't think there is a technical name. You just store a local reference to the current object which - in this case - adds it to the scope chain of setRules(). This allows the current object to be accessed by nested functions.
                          Nope, it's not a form of casting. You just type the var to be of datatype TexHoldem because it holds a reference to that class...