Load external file

Hello its me again haha…so you guys are awesome, real masters of blender and I really apreciate your help…but now I’ll bring a real difficult and advanced challenge:

I can load external blend files using LibLoad()…Is there any way to save the file in the memory buffer, and load in game? I mean, load object from buffer, like a virtual file? Or maybe convert the file to code and load in game?

When you load a library, all requested objects are brought in.

If I have “Actor” object in Def.blend, and I LIbLoad this file, then now I can use Actor.
If Actor is in an inactive layer, then I have to addObject it or look through scene.objectsInactive.
Else, Actor is added automatically.

LibLoad has an argument for scene. It defaults to the active one. This refers to which scene in your main file should the data be added to.

In case you make any kind of modification ingame to an object and want to recall it later on, use shelve, pickle or globalDict to write the modified attributes to a file. You can then look up this file and re-apply the changes.

I may be getting the question wrong though. Clarify if so.

1 Like

“Loading” means the system reads the file from OS filesystem and creates according objects in memory.

The answer is: yes, LibLoad does that (at the time you call it).

Maybe you mean something different.

Got it working using libload. Thanks guys

I’m trying to save a authentication key at game runtime, using globalDict, but its not working. When i close the game i lost the key. Data saved in globalDict aren’t permanent? If no, is there any way to achieve this without saving the key in a external file (to avoid security issues)?

You can try to save the data manually (I explained it here), and ‘encrypt’ the data using zlib. It’s not really encrypted, but only by decompressing the data via code you can reveal it.

import zlib

data = "This is a test string.".encode() # The data must be bytes
data_encrypted = zlib.compress(data)

You need to call bge.logic.saveGlobalDict() in order to generate a file contaning the global dict, then on starting a new game you use bge.logic.loadGlobalDict() to recall it.

Note that these two methods use the current working directory, i.e. wherever in your computer the blend file you’re working with is located.

If you want to do something slightly more complex and have total control on where the files you write to and read from are, check joel’s method.

I’m calling bge.logic.saveGlobalDict() but still not working. Heres my testing code:

import bge

global_dict = bge.logic.globalDict
load_dict = bge.logic.loadGlobalDict()
save_dict = bge.logic.saveGlobalDict()

def userLogin():
    input_check = True
    if input_check:
        user_key = 'USERKEY'
        if user_key:
            load_dict
            global_dict['userKey'] = user_key
            save_dict
            print("Login successfull. Key "+global_dict['userKey']+" has been saved.")
        else:
            return False

load_dict 

if "userKey" in global_dict:
    key = global_dict['userKey']
    print(global_dict)
else:
    userLogin()
    print("Validate error")

It does save the key, but when i restart the game globalDict is empty and do the userLogin() again.

func() -> function call.
name = func() -> name is equal to func return value (None if no return)
name = func -> name is equal to func. now name() calls the function.

In your code, the functions are only called initially. save_dict and load_dict are equal to None, because these functions have no return value. So remove parentheses; now save_dict and load_dict are equal to the functions you equal their names to.

Like so,

1 Like

Thank you man!