How can i make the game download .blend files?

With the new version of UPBGE, 0.2.5, I thought about making my game with the heavy scenes that will be used in the game itself, being separated from the lighter ones like menus and tals,

but I wanted to save everything in a folder with the name of the game in the appdata, as it is easier for my game to know where the game scenes are. And I wanted the game to actually download the scenes, because the players doing all that would not be playing.

I would also like to know if it is possible for me to make a non-executable file for the player not to disturb the game scenes so easily.

encode your blend files in some way so clients can’t open them. But then have a decoder that can read them and libload them into the game

Create game data folder in AppData

To get the AppData path through Python and create your game data folder in it, simply use:

import os, pathlib

# Concatenate the APPDATA environment variable to the game folder name,
# then create a pathlib.Path object, allowing easier path manipulation
pathGameData = pathlib.Path(os.environ["APPDATA"] + "/My Game Folder")

# Creates game directory if not already exists
if not pathGameData.exists():
    pathGameData.mkdir()

Downloading a file from URL

To download a file from a url:

from urllib.request import urlopen

# Download the file from URL
fileData = urlopen("https://github.com/bgempire/TedTheFrog/raw/master/Ted%20The%20Frog.blend")

# Read the downloaded data into memory as bytes
fileData = fileData.read()

The data will be downloaded into memory as bytes. You can use the downloaded blend file in two ways.

Saving to a file

To save downloaded data to a file, simply write it as bytes:

# The target file path to write downloaded data
resultingBlendFile = pathGameData / "DownloadedBlendFile.blend"

# Open the given file path in 'write bytes' mode
with open(resultingBlendFile.as_posix(), "wb") as openedFile:
    openedFile.write(fileData)

After the file is saved, you can load it using bge.logic.startGame or bge.logic.LibLoad functions as usual.

LibLoading directly

As bge.logic.LibLoad accepts a bytes data parameter, you can LibLoad the data directly into the game:

import bge

# The parameters are: libName, whatToLoad, bytesData
bge.logic.LibLoad("DownloadedLib", "Scene", fileData)

I don’t have idea what you mean. :laughing:

3 Likes