Skip to main content
Inspiring
April 28, 2023
Question

"Include" external .txt file in AS3

  • April 28, 2023
  • 2 replies
  • 1236 views

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 has been closed for replies.

    2 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 28, 2023

    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

    Inspiring
    April 29, 2023

    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?

    Vladin M. Mitov
    Inspiring
    April 28, 2023

    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);
    }

     

     

     

    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Inspiring
    April 28, 2023

    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"

    kglad
    Community Expert
    Community Expert
    April 28, 2023

    you need to write an eval function.