Use texture from URL

Hello all. Im just wondering if anyone knows how to use download a texture from a url in-game. Like game starts -> texture is downloaded, saved locally, and applied in the game. So you could update textures by uploading them and overwriting the same URL

  1. You don’t want to do this. I don’t know whats the sitiuation but I’m almost 100% that this is not the proper way.
  2. If you still want to do this, you don’t want to upload them. Seriously, is a pain in the ass. You need a server, some kind of API and for security reasons you also need a way to register users. Unless you use some service out there that already gives you an API (I doubt it exists).
  3. If you still want to do this, maybe an URL is not a good idea. Think of a cloud folder, like steam or dropbox, that’s probably a better option.
  4. If even so you are absolutley convinced that you want to do this, python is the answer. Python has standard http libraries, you can download the image with that. Then using the bge.texture module you can replace them inside the game.

Thank you for the input!

I think you’re making things sound to complex elmeunick0

It’s quite simple really, though yes you do have to have a server (though dropbox would work fine).
To download an image from python you can use urllibg:


import urllib
urllib.request.urlretrieve(url, file_name)

To change an image in bge you can use this function:


def switch_image(obj, path):
    '''Replaces the specified objects attached material's image to 
    the image at the specified path.'''
    mesh = obj.meshes[0]
    materialname = mesh.getMaterialName(mesh.materials[0].material_index)
    matID = bge.texture.materialID(obj, materialname)
    if 'tex' not in obj:
        obj["tex"] = bge.texture.Texture(obj, matID, 0)
    obj["tex"].source = bge.texture.ImageFFmpeg(path)
    obj["tex"].refresh(False)

    if not os.path.isfile(path):
        print("Could not find thumnail at: "+path)

As sdfgeoff said, it is fairly simple to do. Here’s a simple example I put together.
download_texture.blend (83.1 KB)

sdgeoff, that can be used to swap out textures in game in real time – is that what that is?

sdfgeoff

If even so you are absolutley convinced that you want to do this, python is the answer. Python has standard http libraries, you can download the image with that. Then using the bge.texture module you can replace them inside the game.

This is the last point, where no uploading is done (wich is the complex part). But it was my intention to make it sound complex since it is probably not what he needs (and it also can become quite complex depending on the situation). A version control software is what he probably needs. If he really needs to update and patch new versions of the game while playing the game (like WOW), it will become complex, and if all he needs is to update to the last version when launching the game, a launcher would be more apropiate.

But to be honest, I didn’t read the post well the first time, I confused “game starts” with “game stats”, and it make me belive that he wanted to update the stats using a texture for some kind of pseudosecurity reasons.

By the way, my nickname is elmeunick9, not 0. It translates to “My new nickname” since in catala the number 9 is pronounced like “nou” wich means “new”… or “nut” depending on the context.

Sorry for mistyping your name elmeunick9. Mind if I ask what language it is?

Yup, it may well not be what he actually needs, but as I have the code for this sitting on my drive, it was very easy to just copy-paste it up there. You had already given the cautionary warning…

It’s Català, we are the ones that do millon-people manifestations once peer year, the reason is always the same, we don’t want to be Spanish any longer.

Thanks for all the replies. the reason I want this function is players can create an account on a website and save a skin file that the game downloads before starting and applies to the character.