Dynanic loading of models

Software: Away3D 4.x

Tempy111, Sr. Member
Posted: 16 November 2016 01:01 PM   Total Posts: 133

okay. What I’m trying to do is where there is a ‘Models’ folder with the swf, which a new model can just be placed. This model can then be loaded by the program/game. This allows easy update for new model add-ons.

right now, the only way I know how to insert new models is to take the folder (using As3 models), add the import line in the game code, then to use create a variable for it and do the whole ‘Var model:TestModel = new TestModel(); etc.’

which is all done at design time. If an add-on with a new model/object comes around, the main game needs to be updated too. which.. well, it’s all well and good, but I would be nice to be a bit more open.. I’m not sure, however, how to go about this. This might not be a Away3d question though.. i’m not sure.. might be able to just compile the model into a swf, and then call it via urlloader for the Swf..

   

Avatar
Fabrice Closier, Administrator
Posted: 16 November 2016 01:27 PM   Total Posts: 1265   [ # 1 ]

yes, you need to allow code exec, and if in air, you will also need define domain…

   

Tempy111, Sr. Member
Posted: 16 November 2016 01:31 PM   Total Posts: 133   [ # 2 ]

okay, thanks ^_^

   

Tempy111, Sr. Member
Posted: 26 November 2016 11:28 PM   Total Posts: 133   [ # 3 ]

this seams pretty tricky.. I got the model’s as files all contained in a SWF.. I’m not quite sure about Code execution being allowed but I use flash’s LoaderContext to allow the domain, I use the Loader to load the swf, with a listener to fire when it loads..

which it does but first, complains that the model’s package doesn’t extend Sprite (it extends ASBase).. so I make another file to use as the Document Class, to handle that, thinking that when I import the models’ as file, I can just get it to work, but nope.. I either get told it’s expecting a value passed (but the function has no value to pass to it) or moaning about the instantiation attempted on a non-constructor.. this is gonna take a bit of time I think.. and most of what I find online doesn’t seam too helpful..

if only I could get rid of this ‘Error #2023: Class <> must inherit from Sprite to link to the root.’ but I have the main class extending Sprite.. and without doing that, well, a lot of stuff moans.. mmm

   

Tempy111, Sr. Member
Posted: 27 November 2016 10:45 PM   Total Posts: 133   [ # 4 ]

Almost got it working (that is, it’s working but I need to double check a couple of minor-ish points).

I’ll put my solution up later, maybe a couple of days, for archival reasons. nothing worse then coming across a forum post with the same problem you have but all they say is ‘solved’ and not say how ^_^

   

Tempy111, Sr. Member
Posted: 28 November 2016 08:07 AM   Total Posts: 133   [ # 5 ]

okay:

First, the Archive swf. Put all the model code into a subfolder. in the main Folder, add a new as file as the document class. this is made to extend Sprite, and import the subfolder’s main file (using the name of the subfolder). Give it a public variable. In the constructor, make the public variable into a new copy of the model. For example:

package 
{
 import flash
.display.Sprite;
 
import Sandwich.Sandwich;

 public class 
Main extends Sprite
 {
  
public var Item:*;
  
  function 
Main():void
  { 
   Item 
= new Sandwich();
  
}
  
 }

To import, place the SWF created with the main swf (can be a subfolder). Use the Loader class. Allow Code import. Mostly set up as you normally set up a loader. on the complete event handler, set up a temporary class to hold the constructor from the archive swf.

Example:

public var _loader:Loader();
private function 
AssetLoad(e:MouseEvent):void
  {
   
//Asset Loader
   
_loader = new Loader();
   
   
_loader.contentLoaderInfo.addEventListener(Event.COMPLETEloaderCompleteHandler);
   var 
loaderContext:LoaderContext = new LoaderContext(falseApplicationDomain.currentDomain,null);
   
loaderContext.allowCodeImport true;
   
loaderContext.checkPolicyFile false;
   
_loader.load(new URLRequest("Assets/Sandwich.swf"), loaderContext);
  
}
  
private function loaderCompleteHandler(e:Event):void
  {
   _loader
.contentLoaderInfo.removeEventListener(Event.COMPLETEloaderCompleteHandler);
   var 
item:Class = _loader.contentLoaderInfo.applicationDomain.getDefinition("Main") as Class;
   var 
itemStore:* = new item();
   
//Do what you want with the archive now
   
  
   
   
‹‹ make jiglib cube lookat camera

X

Away3D Forum

Member Login

Username

Password

Remember_me



X