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.
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?
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.
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.
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")
else:
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.