help with Python syntax (globalDict)

Hi everyone,

A quick question: I am trying to use globalDict to store a reference to the last object detected with a connected radar sensor, but I am struggling with the syntax. Here is my code:

import bge

own = bge.logic.getCurrentController().owner

controller = bge.logic.getCurrentController().owner

sen = controller.sensors["Radar"]

locked_target = sen.hitObject

bge.logic.globalDict["locked_target"] = sen.hitObject["locked_target"]

print bge.logic.globalDict["locked_target"]

Can anyone shed any light onto what I should change? I am trying to understand how to store the reference to the last detected object - the best I wind up with is just storing the ‘locked target’ string and printing that.

Many thanks


i think the problem is here:

bge.logic.globalDict["locked_target"] = sen.hitObject["locked_target"]

try this:

G = bge.logic
cont = G.getCurrentController()
own = cont.owner
sen = cont.sensors['Radar']

if sen.positive:
    ho = sen.hitObject
    if str(ho) == 'locked_target':
         G.globalDict["locked_target"] = sen.hitObject
    print ("locked_target", G.globalDict["locked_target"])

the hitObject will only be written in the globalDict if its name is ‘locked_target’
Is that what you wanted?

There’s some errors in your code (in the logic more so than anything). Here’s the edited code:

import bge

controller = bge.logic.getCurrentController()

own = controller.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"])

Thanks for your help guys! I was following a small wiki page but I think I got a bit confused SolarLune. Originally the = locked_target part

     bge.logic.globalDict["locked_target"] = locked_target

had quotation marks in the tutorial example- in my inexperience I thought the globalDict held the entry as such and thats when I started getting into trouble.

Sigh…perhaps one day I will get this- I feel a bit of an idiot.

Actually, I have another question. How can I make it so that the last recorded object is held as the target? With this code, unless the radar keeps detecting an object (e.g. cube001) it returns NONE and thats a bit of a problem for a homing missile- I intend to link this to the track to Python actuator.

The code posted above already does this, it only sets the globalDict variable if the hitObject sensor returns true.

I just tested this and unfortunately my hypothesis was correct agoose77- when the target moves out of the radar ‘cone’ it loses ‘lock’- and I get an error in the console. I will post a file soon with my work so far.

Thinking about it though, if this problem is fixable I may keep this anyway as it would emulate ‘painting’ a target with an IR beam for guided weapons…silver lining and all…!

OK, here is the blend- left key moves TARGET into range, up arrow moves the cube backwards. The homing rocket will fire after 3 seconds of lock, and when the HUD cube has scaled down.


countdown.blend (475 KB)

Quick Idea. Make a new Int property on the owner object of this script, call it lockedCnt.

import bge  
own = bge.logic.getCurrentController().owner  
controller = bge.logic.getCurrentController().owner  
sen = controller.sensors["Radar"] 

if sen.positive:
    own['lockedCnt'] += 1
    if own['lockedCnt'] == 1:
        if sen.hitObject != None:
            locked_target = str(sen.hitObject)  
            bge.logic.globalDict["locked_target"] = str(sen.hitObject)
            print(bge.logic.globalDict["locked_target"]+" is now locked target")
    own['lockedCnt'] = 0  
    print("No longer in range of last locked target: "+bge.logic.globalDict["locked_target"]) 

Just use the code SolarLune posted if you aren’t already (you don’t appear to be according to the .blend you posted). It fixes the problem with the locked_target object being recorded as None.

When a logic brick sensor is activated, it sends a Positive pulse, and when it becomes inactive it usually sends a negative pulse. Some sensors don’t send negative pulses afterwards, such as the Always sensor.

But in your case with the Radar sensor, it sends a positive pulse when it finds the specified property, and when that property moves away it sends a negative pulse. Both types of pulses activate the Python script controller and run the script, so you need to specify a filter in the script that only accepts positive pulses.

SolarLune’s code does this with the “if sensor.positive:” line of code. If you don’t specify such a filter, the script will be run with the negative pulse yet no object will be found, which results in the None value instead of an object.

Monster wrote a page that perfectly explains how pulses from sensors work, you may benefit from reading it if you haven’t:

Doh! Thanks guys for the help. I thought I was using that code, but I did have some PC problems so I guess it wasn’t saved properly. Riyuzakisan, I will check out that link! And to SolarLune, I am an idiot x2! I guess I learn things the hard way.

haha, happens to everyone at some point