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?
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.
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 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?
or
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.
e.g.:
class Ship(object):
def __init__(self, owner):
self.owner = owner
owner["ship"] = self
self.weapons = []
self._target = None
@property
def target(self):
return self._target
@target.setter
def target(self, value):
self._target = value
for weapons in self.weapons:
weapon.target = value
class Weapon(object):
def __init__(self, owner):
self.owner = owner
owner["weapon"] = self
self.ship = None
self.target = None
def initShip(cont):
Ship(cont.owner)
def initWeapon(cont):
weapon = Weapon(cont.owner)
weapon.ship = ??? # however you connect the ship and the weapon # you can do that later too
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.
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).