Prepare blend files for sending over a network.

If you haven’t tried it you should check out the Game jolt API for your games.
Using the “gamejolt” website you can set up your games to save and load data like top scores, saved games and more on to a server. You can also allow things to be downloaded by anyone using your game. A good example would be a saved game to be reloaded by a player or a seed for a random generator so two friends could play a similar game.

With a little extra work, one thing that you could do is make a patch for your game, to be downloaded by the user in order to modify their existing game. You might want to add more monsters, new levels or tilesets, or you might want to change the gameplay to remove exploits.

This is simple enough if you just want to update a text file, but what if you want to add new blender created content like characters or levels?

Well you can save a blend file in to json, store it on the server and then have blender download it and use libLoad to insert it in to your game.

Here’s a basic example:


import bge
import json
from base64 import b64encode, b64decode

def save_blend():
        
    in_path = bge.logic.expandPath("//original.blend")
    with open(in_path, "rb") as infile:
        original_blend = infile.read()
    
    base64_bytes = b64encode(original_blend)
    base64_string = base64_bytes.decode('utf-8')
    raw_data = {"blend": base64_string}
                
    out_path = bge.logic.expandPath("//original_bytes.txt")
    with open(out_path, "w") as out_file:        
        json.dump(raw_data, out_file)   
             
def load_blend(cont):
    
    in_path = bge.logic.expandPath("//original_bytes.txt")
    with open(in_path, "r") as infile:    
        blend_data = b64decode(json.load(infile)["blend"])        
    
    bge.logic.LibLoad("new_data", "Scene", blend_data)
    
    own = cont.owner
    own.scene.addObject("test_cube",  own, 0)
       
def go_blend(cont):
    own = cont.owner
    
    do_save = cont.sensors['1']
    
    if do_save.positive:
        save_blend()
    
    do_load = cont.sensors['2']
    
    if do_load.positive:
        load_blend(cont)
                    

Here’s a blend:
libload_test.zip (663 KB)

Press 1 to save and 2 to load.

What else could you do with this? Well, you could distribute your game as an empty blend, and then have people log in to a website to download and play it. The whole of your game could be hosted online with only the launcher on the client computer.

it could also allow sharing of custom maps or content.
This is just a proof of concept though, you’ll have to dig in to the workings of the gamejolt api if you want to learn how to send the saved data to the server and download it again. Alternatively, using this code and some other ideas you could set up your own website and handle the process yourself.

NOTES:

  • This is easier if you use pickle instead of json, since pickle can save bytes rather than converting to strings. However, the gamejolt api requires the use of json, and overall json is a better choice for sending saved data.
  • Saving and loading the data can take some time. You can use threaded loading to stop your game from freezing.
  • The saved blend.txt takes up quite a lot of space, especially compared to a normal blend file. Bandwidth could be a problem if you’re not careful.
  • You should use “pack external files” to make sure sounds and textures are sent as part of the blend.
  • It would be possible to download the blend and then save it to disk if you want to allow offline usage of your assets. But be careful with permissions, as some computers won’t let you write any old junk to the hard disk.
  • By hacking your game and getting the keys to your gamejolt storage it might be possible for someone to put malicious data in to people computers via this process. You should probably include some kind of verification procedure on the client game to try and prevent this.

You can find my tutorial on using the gamejolt AIP here.