Loading external CSV and images

Hiya!

I'm trying to load a list of enemies from a webserver (around 100 at a time). I need to be able to do two things:
1. Load in names and 1-3 stats.
2. Load in images and add them to already existing sprites.

I've tried:
- Database connection with scripts and php/MySQL stuff from tutorials of online highscore lists. It's a bit heacy with my level of experience, and it seems a bit like overkill.
- Loading a CSV, but cannot figure out how to load a CSV file from an online storage (I've uploaded one to a webhotel).

Thanks in advance :)
 
H

Homunculus

Guest
You need to run an http request to get the data. Since load_csv requires a file, you should probably go for http_get_file . Check the manual on that and try to get your csv to begin with, the manual does provide an example. Remember that you need a direct link, not a page that lets you download the file, but the file directly, otherwise this will not work
 
You need to run an http request to get the data. Since load_csv requires a file, you should probably go for http_get_file . Check the manual on that and try to get your csv to begin with, the manual does provide an example. Remember that you need a direct link, not a page that lets you download the file, but the file directly, otherwise this will not work
Thanks.

I've used the following code in a Create event in an object in the start room (it's a test CSV with very limited content):
GML:
http_get_file("http://ddu.nu/Monsters/monsters.csv", "\Downloads\monsters.csv");
It just does... nothing. It doesn't put out an error or anything. I've tried messing up the link on purpose, but it still does nothing. Do I need to use a script as well or put the file into a variable? I would prefer to add the CSV file to an array.
 

FrostyCat

Redemption Seeker
Once again, have you read the Manual's example like you were instructed to?
The Manual entry on http_get_file() said:
Extended Example:

The http_get_file function can be called from any event, and since it is asynchronous the callback can be almost instantaneous or could take several seconds. Calling the function is simple and would look something like this:
GML:
file = http_get_file("http://www.macsweeneygames.com/downloads/upgrade.zip", "\Downloads\Upgrade.zip");
The above code will request a file from the given URL and set it to be downloaded to the local storage area (as specified in the HTML5 Game Options) , in a directory "Downloads" with the given file name (this does not have to be the same as the source file name, but should use the same file extension). The async_load map index will be stored in the variable "file" so you can check for the correct callback in the Asynchronous Event, and if the save directory does not exist, it will be created. The Asynchronous Event triggered would be the HTTP sub-event, and in that event you would have something like the following:
GML:
if ds_map_find_value(async_load, "id") == file
    {
    var status = ds_map_find_value(async_load, "status");
    if status == 0
        {
        var path = ds_map_find_value(async_load, "result");
        var files = zip_unzip(path, "/NewContent/");
        if files > 0
            {
            global.ExtraContent = true;
            }
        }
    }
The above code will first check the "id" of the ds_map that has been created, then check the status of the callback. If the value is equal to 0 (signalling success) the result from the callback will then be used along with the zip_unzip() function to unzip the downloaded file to the given directory. If the unzip succeeds a global variable is set to true.
Your code should work the same way as the example, with the return value of http_get_file() saved in a variable and the file-handling code in the asynchronous HTTP event. You just have a different URL for http_get_file(), and load_csv() instead of zip_unzip() in the HTTP event.
 
Once again, have you read the Manual's example like you were instructed to?

Your code should work the same way as the example, with the return value of http_get_file() saved in a variable and the file-handling code in the asynchronous HTTP event. You just have a different URL for http_get_file(), and load_csv() instead of zip_unzip() in the HTTP event.
Thanks for the reply.

Changing the unzip to load_csv helps, and putting it in the correct event helps. It doesn't actually work yet, but at least I get some error messages now, that I can use to solve it.

Let's say I get this working. Will it mean that if I export my game as an APK, the game can load an external CSV and use it? Or does it load the external CSV and build it inot the APK? The reason I'm asking is that it's an important feature for me to be able to update the enemies in a game without having to distribute different versions all the time. It's a learning game, and I need to be able to update the enemies/challenges without having a school reinstall the game.
 
H

Homunculus

Guest
It gets the updated file and saves it locally every time you run the http call successfully. In short, yes, you can definitely change the file and let the apk download it without having to update the game itself.
 
Last edited by a moderator:
I got it working. Thanks, Homunculus and FrostyCat.

For reference, I used the following code in a Create event, which is just like the manual (will move it to a mouse event of some sort later):
GML:
file = http_get_file("http://domain.com/Folder/plswrk.csv", "\plswrk.csv");
And the following in an Async - HTTP event (slightly edited):
Code:
if ds_map_find_value(async_load, "id") == file{
    var status = ds_map_find_value(async_load, "status");

    if status == 0{
        var path = ds_map_find_value(async_load, "result");
       
        var gridname = load_csv(path);
        show_debug_message(path);
   
        if monsters_grid > 0{
            //global.ExtraContent = true;
            //show_debug_message(ds_grid_get(gridname,0,1));
            //show_debug_message(ds_grid_get(gridname,1,1));
        }
    }
}
 
Top