I have a bunch of Values on my main scene which i transfer over to my HUD scene using globalDict, however whenever the game starts up, all the values are set to ‘1’ until i do something to change them, this only started happening as they were transferred through globalDict, any ideas?
bge.logic.globalDict is an empty dictionary. It does not contain a “1” or similar. It is plain empty.
I can only guess it is the way you access the data results either in one or does not work at all.
Remark:
Regardless of it’s generic name it is supposed to be used by the Load/Save actuators for … loading and saving. Using it for other purposes (e.g. transferring values from one scene to another) conflicts with that purpose and breaks it. Therefore I strongly recommend to avoid this data structure. I mean you do not use your mail box to store your food, don’t you you?
You can create your own storage at any module you like:
myStorage.py
storage = {}
myStorageUsage.py
from myStorage import storage
storage["key"] = 1234
This storage will not interfere with the Load/Save aspect of the BGE as it does not touch bge.logic.globalDict.
Thats a good idea, i never thought about that. I will change the storage method, however, i am still curious as to why this happens, the values are integer values, which all get set to 1 when the game starts, however whenever they caused to change, they continue from where they are supposed to be. For example, if I set a value to 33, on startup it will be 1, then when i subtract one, it will become 32.
Monster, I do a bit different, I didn’t know that your method is acceptable too. I do:
Data.py
class dataForScene1():
def __init__(self):
pass
dataList1 = []
dataDict1 = {"Cat": {"Age": 12, "IQ": 0.1}}
Loadup.py
from Data import dataForScene1
a = dataFromScene1.dataDict1["Age"]
a += 1
dataFromScene1.dataDict1["Age"] = a
Sure if you have an integer you can perform according operations on it. Your question asked why you get that integer while you expect nothing.
To analyze that, I need to know how you access the data.
is there a tutorial somewhere? i planed to use the gd for transfering data too, but if this is the method to use i would like to know how. how can i acces data from within the hudscen if i initialized the storage in the game scene?
A module is scene independent. All you need is to load it.
You do that by calling a function from the module or
by calling a function from a module that imports this module or
by executing a script that imports that module.
Simple demo: grab the code from the snippets above.
an according “restore script” would be:
myStorageReading.py:
from myStorage import storage
print( storage["key"] )
Obviously you would get an error when you execute myStorageReading.py before myStorageUsage.py as the key is not added to the storage at that time.
The trick is to use a module (rather than a script). It even allows you to use a single file:
storageDemo.py:
storage = {} # <--- this creates a module variable of module storageDemo
def demonstrateSetValue():
storage["key"] = 1234
print("stored", storage["key"], "in 'key'")
def demonstratePutValue():
if "key" in storage:
print("The value of 'key' is", storage["key"])
else:
print("There is no 'key' in storage")
execute this as module:
—> Python Module: storageDemo.demonstrateSetValue
and later
—> Python Module: storageDemo.demonstratePutValue
and you should see something like this:
Blender Game Engine Started
stored 1234 in 'key'
stored 1234 in 'key'
The value of 'key' is 1234
The value of 'key' is 1234
Blender Game Engine Finished
[The output is double because the sensor status is not checked in the sample]
so if i want to set a properties value using the data out of the storage it would be: own[’blablaValue’] = storage[“importantData”]? i am not working with print, because i dont have a console in ubuntu.
Yes, exactly like that.
How do you see errors?
Start blender from console by typing … ‘blender’
(or if you haven’t installed it you may have to use /path/to/downloads/blender)
Then you can get the debug information.
most of the time i use snippets from tutorials4blender that work perfectly most of the time. i try to keep python as low as i can , because im more into java. therefore i try to solve problems with logic bricks because i use bge mainly because of the lbs. when i have a problem with python where i think a console can show me the errors, i change to windows. i have got a standard windows installation and a ubuntu install on a second hdd(not a live version). i dont like windows because it allways does things in the background and i dont know what. but there are some windowsprograms you dont have in linux so i have to use it from time to time)
@sdfgeoff
yeah i am using the standalone. but i will try the paththing. great tip!
Here is a short video showing what happens: https://drive.google.com/file/d/0B9SMh1EBaTriOGlIcVdYeUIyYk0/view?usp=sharing
You know you are accessing the data via get() with a default/fallback of 1?
0.0 ok, noob mistake :spin: How else can you get the data?
This access is absolutely correct. You questioned why you initial get a 1. That is the reason.
My question is: What do you expect to get when asking for an non-existent item?
Background:
Your “issue” is that there are several situations. Typically you have to think about all possible situations not just the “good case”.
As far as I understood you want to show the status of collected points. You store the collected points in that storage, so you can extract the information from there as you already do.
But you also need to consider what to do when there is no stored collected point (you also need to consider what to do when there is no storage at all). The default behavior (of Python) is to raise an error. While you might think this is bad … in reality it is good. [Typically you want to discover such an error while developing, rather than the user discovers them.]
An error means you did not consider a situation with your code. So you need to think about what else (rather than printing an error) to do. In your situation you already covered that situation. You setup a default/fallback. That means if there is no stored collected point, you treat that situation as one point and work with that one.
From the context you provided, I would say the fallback should be zero rather than one. This matches your surprise on the shown values. Unfortunately on your investigation you assumed a different situation (good case) leaving you confused as hell.
So you need to to correct the handling of the situation when there is no stored value. To be more specific - replace
...get("key", 1) -> ...get("key", 0)
OOOh, ok yes I didn’t even notice! You are very helpful, thanx:yes: