globalDict unique entries

Hi all

I need some advice regarding messages and globalDict:

I have a situation where a weapon will be duplicated any number of times. It uses globalDict to store a reference from a radar sensor- the problem is new instances of the weapon share the same globalDict reference.

Is there a way to make each scripts use of globalDict unique, or am I missing something?



you need to create a unique id for every instance.
Then store the object into the global dict wich the id in its key_name
could then look like:

obj = gameobject
global[str(obj)+"_"+str(obj[‘id’])] = the reference you want

You should explain the problem in more detail.

What exactly are you storing in globalDict, and why?

A unique id of any Python object can be retrieved via id().

But I agree with Goran, it sounds more like a design question rather than a technical problem. Please describe the purpose. I’m sure there are some simple solutions to it.

OK, here is the problem in more detail:

In the game the player can select one of eight ships, with each ship being able to have one weapon selected for it.

This is achieved by having the ships and weapons sat in a hidden layer and added /combined before each race via Python.

So for example, a player may choose ship A with weapon B, and select an AI with ship B and weapon B.

Certain weapons use globalDict to store targets detected using the radar sensor (this is an old code that I have yet to change to a module):

import bge
own = bge.logic.getCurrentController().owner
controller = bge.logic.getCurrentController().owner
sen = controller.sensors["Radar"]
if sen.positive:
    locked_target = sen.hitObject
    bge.logic.globalDict["locked_target"] = locked_target
#print (bge.logic.globalDict["locked_target"])

I assumed that copies of objects automatically generate a separate globalDict reference, but I have found that this is not the case.

Here is a file that illustrates this. Press LEFT cursor to move each block into the radar cone. After two seconds the rockets will fire, but they lock onto the right target within the right cone only and not each individually.


globalDict.blend (495 KB)

globalDict is an attribute of GameLogic (bge.logic). It is created by this module and will die with that module.

You can give a game object own dictionaries (which is indeed recommended in your situation).

Obviously a weapon is shooting at one target. So it makes sense if the weapon knows about the target

weapon["target"] = target

Where does the target coming from?
Does the single weapon lock a target?
Does the single ship lock a target?

ship["target"] = target
for weapon in ship["weapons"]:
  weapon["target"] = target

Just some quick and dirty examples.
It might make sense to create a class ship and class weapon. Then apply the instances of the classes as property to the game objects.


class Ship(object):
  def __init__(self, owner):
    self.owner = owner
    owner["ship"] = self
    self.weapons = []
    self._target = None

    def target(self):
        return self._target

    def target(self, value):
        self._target = value
        for weapons in self.weapons:
 = value

class Weapon(object):
  def __init__(self, owner):
    self.owner = owner
    owner["weapon"] = self
    self.ship = None = None

def initShip(cont):

def initWeapon(cont):
  weapon = Weapon(cont.owner)
  weapon.ship  = ??? # however you connect the ship and the weapon # you can do that later too

Maybe I missed what you are looking for. Here is another thought:

A bullet/missile can get the target from the weapon when firing (except you want to be able to change the target while flying).

weapon = ...
rocket = ...
rocket["target"] = weapon["target"]

The weapons act separately from the ship that carries them, for example, the radar sensor is built into the weapon and not on the ship and is set to pick up a property in each ship object.

So all I really need is a per object dictionary.

So the weapon decides what the target is.

In this case the weapon should give the rocket/missile the target on firing (after object creation). As the weapon should know the rocket it should be easy to do that (see post #7).

Makes sense! Will re-engineer and report back.

And thanks!

Here is a working demo:


globalDict.blend (353 KB)

Cool, I like this way of doing things, very clever!

Thanks for the example!