Skip to main content
Inspiring
Apr 28, 2023 9:26 AM UTC
Question

"Include" external .txt file in AS3

I want to include external code without importing a custom class in AS3.
Is there a way to insert a .txt file in a remote directory into AS3.0 code?
Simple example code please.

Thank you

 

    This topic is closed to new replies. Start a new post to keep the conversation going.

    10 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    Apr 28, 2023 1:33 PM UTC

    Hi.

     

    Can you give an example of why do you need to load ActionScript code from a .txt?

     

    As ActionScript is converted to bytecode in compile time, loading code externally doesn't make too much sense for me.

     

    Regards,

    JC

    Translate
    Inspiring
    Apr 29, 2023 6:15 AM UTC

    For example, whenever I edit a music list uploaded to the server, can the user use the automatically modified list when the user runs the program?
    Or can the user just use the list created at compile time?

    Translate
    Vladin M. Mitov
    Inspiring
    Apr 28, 2023 9:38 AM UTC

    You can use the load() method of the flash.net.URLLoader class to load an external .as file at runtime and execute its contents via the eval() method.
    For example:

    // create a URLLoader instance
    var loader:URLLoader = new URLLoader();
    
    // specify the URL of the external .as file
    var url:String = "http://example.com/examplefile.as";
    
    // add an event listener for when the file has loaded
    loader.addEventListener(Event.COMPLETE, onFileLoaded);
    
    // load the file
    loader.load(new URLRequest(url));
    
    // this function will be called when the file has loaded
    function onFileLoaded(event:Event):void {
      // get the loaded file's contents
      var loadedCode:String = event.target.data;
    
      // execute the loaded code using the global 'eval' function
      eval(loadedCode);
    }

     

     

     

    Translate
    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Inspiring
    Apr 28, 2023 11:55 AM UTC

    Scene 1, Layer 'Music_List', Frame 1, Row 51, Column 3 1180: Called undefined method eval.


    I get this error message
    Please let me know if there is an alternative way or what to "import"

    Translate
    kglad
    Community Expert
    Community Expert
    Apr 28, 2023 1:26 PM UTC

    you need to write an eval function.

    Translate