Stencyl 3.4.0 is now out. Get it now!

How to include arbitrary data in projects (text, images, SWF)

Jon

  • *
  • Posts: 17497
I've hacked in a temporary permanent but rather efficient way to include data in projects, like text files, images and SWF movies. The original intent was just the text file portion.

Instructions

1) Create an "extras" folder under your game's folder.

2) Stick the desired files directly into that folder. Subdirectories are supported.

3) In-game, access your data like this:

Quote
Update: There is now an extension that does all of the below using blocks. Check it out!
http://community.stencyl.com/index.php/topic,35620.0.html

Text Files

Code: [Select]
var s:String = nme.Assets.getText("assets/data/dict.txt");

Images (PNG, JPG, GIF)

Code: [Select]
var img:nme.display.BitmapData = nme.Assets.getBitmapData("assets/data/sample.png");
com.stencyl.Engine.engine.root.addChild(new nme.display.Bitmap(img));

(Alter the second line as you see fit to attach to different targets.)

Movies (SWF)

Code: [Select]
var bytes = nme.Assets.getBytes("assets/data/sample.swf");
var loader:nme.display.Loader = new nme.display.Loader();
loader.loadBytes(bytes);
loader.contentLoaderInfo.addEventListener(nme.events.Event.COMPLETE, function(_) {
    com.stencyl.Engine.engine.root.addChild(loader.content);
});

(Variation - This auto-removes the clip after completion)

Code: [Select]
var bytes = nme.Assets.getBytes("assets/data/sample.swf");
var loader:nme.display.Loader = new nme.display.Loader();
loader.loadBytes(bytes);
loader.contentLoaderInfo.addEventListener(nme.events.Event.COMPLETE, function(_) {
    var clip:nme.display.MovieClip = cast(loader.content, nme.display.MovieClip);
    clip.addFrameScript(clip.totalFrames - 1,
        function():Void {
            clip.stop();
            com.stencyl.Engine.engine.root.removeChild(clip);
        }
    );
    com.stencyl.Engine.engine.root.addChild(clip);
});

(In local testing, you may get a security error that you have to dismiss each time. Only works on the Flash target. Some movies may not be supported, but this is made pretty clear by the Flash player. NME has a cross-platform SWF loader, but when I tried it, it did not render things correctly.)

« Last Edit: February 15, 2015, 11:29:49 am by Jon »

Future Freak

  • Posts: 413
Cool - will test this out tonight - it will really help with my xml data - as I'm currently copying and pasting xml files into var string declarations in behaviours.

Future Freak

  • Posts: 413
I finally got this working, after a few mis-assumptions.

Maybe "2) Stick the files in to that folder." should be
2) Stick the files in to that folder. (Do not create any subdirectories - place all files in the root of the extras folder)
and maybe say that the assets/data/ HAS to be as specified.

as I was presuming the assets/data/ was a reference to the path I created in the extras folder 
e.g. /extras/data/xml/level1.xml
would be referenced by nme.Assets.getText("data/xml/level1.xml")

Jon

  • *
  • Posts: 17497
Correct, everything has to be at the top level of that folder.

Jon

  • *
  • Posts: 17497
I did a quick test on images and found them to work flawlessly, and you can even slip in a JPG like this. Bear in mind that none of this obeys scaling and all that, so the utility is pretty limited outside the web game realm.

I also checked out SWF movies, and those work fine too, as long as you understand the limitations and quirks of working with raw SWF's. Instructions updated to reflect that.

Once Mike stabilizes his work, I could consider wrapping these up as utility functions, but no further than that at this point because there's a lot more to this than meets the eye and can't be done justice at this time.

Dom818

  • *
  • Posts: 1296
I was doing some tests with this earlier and you seems to be able to include anything in the game. Your not limited to just text, image, and swf files. I am close to getting a mp4 video playing on the iOS devices even. This feature has so much potential.

Warpgate Studio

  • Posts: 42
Good work u are resolved my problems :) Ty i have another question, if i have into my swf some as3 script the code doesnt read it, in my swf i put the skip botton but it doesnt work

Jon

  • *
  • Posts: 17497
I was doing some tests with this earlier and you seems to be able to include anything in the game. Your not limited to just text, image, and swf files. I am close to getting a mp4 video playing on the iOS devices even. This feature has so much potential.

With an extension or using pure Haxe? I was unaware that NME could play video on its C++ targets out of the box.

Dom818

  • *
  • Posts: 1296
I was doing some tests with this earlier and you seems to be able to include anything in the game. Your not limited to just text, image, and swf files. I am close to getting a mp4 video playing on the iOS devices even. This feature has so much potential.

With an extension or using pure Haxe? I was unaware that NME could play video on its C++ targets out of the box.

I was using an extension.

« Last Edit: August 31, 2013, 01:28:38 pm by Dom818 »

Dom818

  • *
  • Posts: 1296
Just an update for those who are interested in the iOS video player. I have successfully got it to stream video from a website. It is still crashing on the local videos though.

Dom818

  • *
  • Posts: 1296
Ok, so I have been trying to get this to work for some time now and I think I found out why it is failing on me with local files. I have thought that it is the way the files are saved into the app when it is compiled. I put the video into the extras folder with it name as "video.mp4". After the app is compiled, the name is "assets_data_video_mp4". Is there any way I can get that file into the app without the weird naming mechanic?

EDIT: Got the movie player to show up with the local file now but it instantly closes because it has no idea what kind of file it is trying to play.

« Last Edit: September 01, 2013, 08:44:32 pm by Dom818 »

Jon

  • *
  • Posts: 17497
That naming scheme you've pointed out is controlled by NME and isn't alterable. But at the same time, that's not how you pick up the file when you call Assets.getBlah - it's a virtual file name, not a real file you're looking at.

DarkTimmy

  • *
  • Posts: 462
This is a really dope feature. I don't have a direct use in my latest game but man, it could be REALLY useful down the line.

-Tim
Main Site: Donley Time Foundation Facebook Page: Donley Time Foundation
Newgrounds Links!

ajgutierrex

  • Posts: 71
This is truly awesome!, thanks to everyone who worked on this project, definitely would love to use it. Would there be any ideas on how to get this working with the different 1x, 1.5x, 2x and 4x scales?... I think it would make it a perfect asset for stencyl!

QUICK EDIT: Does loading .swf's work all across the board? meaning Android, iOS, HTML etc.. ?

« Last Edit: September 10, 2013, 10:33:02 am by ajgutierrex »

rob1221

  • *
  • Posts: 8912
Quote
Would there be any ideas on how to get this working with the different 1x, 1.5x, 2x and 4x scales?

Make an image for each of the 4 scales and then choose the correct one based on the value of Engine.SCALE.