• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

HTML5 [HTML5/JS] Loading image from a URL

matharoo

manualman
GameMaker Dev.
I want to load an image from the Discord CDN, like this one.

Currently the only way I know is sprite_add(). If I use it, I get this error in the web console:
Access to Image at 'https://cdn.discordapp.com/attachments/305914243977576459/483206320707862528/Untitled.png' from origin 'https://objective-kilby-081a78.netlify.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://objective-kilby-081a78.netlify.com' is therefore not allowed access.
So, is there any way to fix this, or another way to load the image? Maybe it could be done through JS and then passed to GM? I don't know.

Thanks!
 
M

MarceloP

Guest
Hello again @matharoo

This error usually occurs when you're trying to access another file from a server and your domain is not allowed to do so. It seems that discord has some policy related to not allowing images from any domains to access their uploaded images.
You can get more related info here. This is kindda some secure measures from browsers to protect content...

If you DO need to use discord images, this seems to be something that can be "overcome" by changing the request itself, then you'd have to use something like http_request of even http_get_file, instead of sprite_add. You can also get more info about that on those 2 pages: Asynchronous HTTP Events and More about HTTP Events

But you'd probably have to download to the disk the file first, and then load it from the disk with sprite_add, but controlling the request headers or downloading it, you may be able to do so without many problems, unless you have thousands of them xD

Sorry for using only GM1.4 doc, those are the ones I had in my bookmarks. You can easily find the same functions in the GMS2 docs and I strongly recommend you to do so :)
Hope I've helped.
 

matharoo

manualman
GameMaker Dev.
Hello again @matharoo

This error usually occurs when you're trying to access another file from a server and your domain is not allowed to do so. It seems that discord has some policy related to not allowing images from any domains to access their uploaded images.
You can get more related info here. This is kindda some secure measures from browsers to protect content...

If you DO need to use discord images, this seems to be something that can be "overcome" by changing the request itself, then you'd have to use something like http_request of even http_get_file, instead of sprite_add. You can also get more info about that on those 2 pages: Asynchronous HTTP Events and More about HTTP Events

But you'd probably have to download to the disk the file first, and then load it from the disk with sprite_add, but controlling the request headers or downloading it, you may be able to do so without many problems, unless you have thousands of them xD

Sorry for using only GM1.4 doc, those are the ones I had in my bookmarks. You can easily find the same functions in the GMS2 docs and I strongly recommend you to do so :)
Hope I've helped.
Thanks a lot, I'll check those out :)
 
Top