How do I load text and image from an external txt file?

Get help using Construct 2

Post » Fri Jul 24, 2015 1:23 pm

I know that is possible to load text from a internal txt file. But I don't know how to import text from an external txt file using json. Is it possible to load images from links through a txt file too? I'll really appreciate if anyone explain to me how to do this. Thanks.
B
4
S
2
Posts: 125
Reputation: 850

Post » Fri Jul 24, 2015 1:43 pm

Hi Braus,

you can request external file contents with ajax from an URL (i think that is what you mean with external?)
Take a look at the ajax object https://www.scirra.com/manual/107/ajax request from URL.

Request URL
Send a GET request to retrieve the contents of a URL. A tag is provided to match it up with the On completed, On progress and On error triggers.


The contents could also be a link to an image.
You can then set the AJax.lastData to the sprite you want to load the image to.

Load image from URL
Load an image from a given URL. The current animation frame will be replaced with the image. It is not shown until the image has finished downloading, and On image URL loaded triggers. Images loaded from different domains are subject to the same cross-domain restrictions as AJAX requests - for more information see the section on cross-domain in the AJAX object. Data URIs can also be passed as an image, e.g. from a canvas snapshot or webcam image. The Size parameter sets whether the Sprite object will be set to the image size when it loads, or whether to keep its current size and stretch the image.


If you want to test locally dont forget to enable ajax requests from all sources in your hosting
If you want AJAX requests to your server to work from any domain, or in preview, you can configure it to send the following HTTP header:

Access-Control-Allow-Origin: *


I hope that helps :)
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Fri Jul 24, 2015 3:11 pm

Hi j0schi

Thanks for these confirmations. I still not understood how can I do to request the URL for images links and text files? Using a specific action or the same that I use to request a internal txt file? I need to get the image link on the text file (the same that I use to store the text informations, is possible?) How cai I use AJax.lastData to load the image?

I have another big question (at least for me): can I use both (internal and external) files depending if mobile has or not internet connection? If yes, can you explain to me how to do this?
B
4
S
2
Posts: 125
Reputation: 850

Post » Fri Jul 24, 2015 4:54 pm

Hi Braus,

here is one way to do it:
Image

please note that the imageurl.txt has to contain the url to the actual image. you can also request php files this way whcih gives out another image depending on the url request. there are several other ways too...

you can also check with the Browser Object "is online" but i think that would not be necessary as you want to handle any connection error...

i hope that this point you in the right direction :)
regards
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Fri Jul 24, 2015 5:21 pm

Great, j0schi

It's very clear for me now. I'll try to apply these instructions. Thanks a lot.
B
4
S
2
Posts: 125
Reputation: 850


Return to How do I....?

Who is online

Users browsing this forum: odiusfly and 25 guests