Results 1 to 6 of 6

Thread: Global Dictionary

  1. #1

    Global Dictionary

    Hello everyone I was wondering if you guys can help my figure out how to use global dictionary I know that there are actuators and that you put bge.logic.globalDict ()=() but it still isn't working what am I doing worng
    wrong please help



  2. #2
    Member WKnight02's Avatar
    Join Date
    Aug 2017
    Location
    Earth
    Posts
    28
    Code:
    # globalDict is a Python dictionary, so you do not use parenthesis...
    import bge
    
    # I'm placing the reference to the dict into a local variable named "globalDict".
    # Its like a shortcut...
    globalDict = bge.logic.globalDict
    
    globalDict["MyGlobalVariableName"] = 5 # in place of the "5" you can store any valid Python object
    
    myVar = globalDict["MyGlobalVariableName"] # here you go
    Last edited by WKnight02; 03-Sep-17 at 03:04.



  3. #3
    Thank you I understand now



  4. #4
    Originally Posted by WKnight02 View Post
    Code:
    # globalDict is a Python dictionary, so you do not use parenthesis...
    import bge
    
    # I'm placing the reference to the dict into a local variable named "globalDict".
    # Its like a shortcut...
    globalDict = bge.logic.globalDict
    
    globalDict["MyGlobalVariableName"] = 5 # in place of the "5" you can store any valid Python object
    
    myVar = globalDict["MyGlobalVariableName"] # here you go
    there is no good reason to use a variable.

    globalDict = {}

    this will not clear out the logic.globalDict as expected. it will reassign your local variable to a new dictionary object.

    for someone new, this could be wildly confusing.

    if you want to save some typing, then:

    Code:
    from bge import logic, events, constraints #some common ones
    
    logic.globalDict["Key"] = "Something"
    Last edited by Daedalus_MDW; 03-Sep-17 at 07:14.
    System "IVAN" (rev 1.3b) - Win7 64bit - Blender 2.74:
    CPU- Intel i3-3220 3.30 Ghz | GPU- EVGA GTX 970 | RAM- GSkill Ares 16GB 1600 Mhz | MB- ASUS P8Z77-V LK



  5. #5
    Member Rickyx's Avatar
    Join Date
    Oct 2004
    Location
    Torino - Italy
    Posts
    36
    Originally Posted by Daedalus_MDW View Post
    globalDict = {}

    this will not clear out the logic.globalDict as expected. it will reassign your local variable to a new dictionary object.
    Clever gem!
    Dream first.



  6. #6
    Moderator Monster's Avatar
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    13,640
    The attribute bge.logic.globalDict is a build-in container with the sole purpose to hold data in conjunction with the save and the load actuators.

    There is no need to use it for any other purposes (as sometimes advised). When you use this bge.logic.globalDict for other purposes, you can't use the save and load actuators anymore.

    You might think that you need such a container to hold your game business model. Why you need exactly that? You can create your own container (as Daedalus_MDW showed). Keep it in any module you like:

    gameModel.py
    Code:
    model = {}
    demo.py
    Code:
    from gameModel import model
    
    model["last position"] = owner.worldPosition.copy()
    Modules are singletons. That means when you import gameModel you get the same model within the same game session.

    As you see this is not predefined. You need to build your own dictionary (once).



    {} creates a new dictionary. It does not clear an existing one.

    This means "myDictionay = {}" creates a new dictionary and assigns it to the variable (which replaces any previous content). It is no clearing operation even when the effect means the previous content is not available via that variable anymore.

    To clear a dictionary (including bge.logic.globalDict) use the clear() function:

    Code:
    bge.logic.globalDict.clear()



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •