Help with python and game logic

I am attempting to write python,
And im not that good at it…

I want to make an object(missile) use a near sensor to feed targets with the property enemy to a track to actuator
from what I see it’s pretty simple, For some reason I just can’t handle another language at the moment

Any coders want to collaberate?

Here is a pure python version:

  • Create a property called ‘property’ on the object running the track to.
  • Make the value of the property the property you want to track.
  • Delete your current track to stuff
  • Add an ALWAYS True Pulse enabled sensor.
  • Connect it to a PYTHON controller.
  • Make a new text document in the text window, and paste the code below.
  • Select the name of the text document in the PYTHON controller.

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()
objects = scene.objects
distance=None
object=None
for ob in objects:
    if ob==own:
        continue
    if not own['property'] in ob:
        continue
    dist = own.getDistanceTo(ob)
    
    if distance==None:
        distance = dist
        object = ob
    if dist<distance:
        object = ob
        distance=dist
if not object==None:
    own.alignAxisToVect(own.getVectTo(object)[1],1,1)

the script of agoose is right
only I change those variable… :wink:


import bge
c= bge.logic.getCurrentController()
ob= c.owner

target=0
minDist=9000#(9km)

for i in bge.getCurrentScene().objects:
    if "enemy" in i:
    	dist=ob.getDistanceTo(i)
      	if dist<minDist:
            minDist=dist
            target=i

if target!=0:
    ob.alignAxisToVect(ob.getVectTo(target)[1],1,1)
    ob.linearVelocity[1]=10#ahhhhh ... that chicken!! this line is wrong!

little explanation for the loop…much important for write




XXX=[6,2,4]
XXX is a LIST of number

you can schearc in XXX the number too big with loop in this mode:

CODE
###
XXX=[6,2,4]

big=0
for i in XXX:
    if i>big:
        big=i    

###
so, big=6

HOW WORK THE LOOP REALLY? in this mode:

i=6
if 6>0 #true, pass
big=6

i=2
if 2>6 #false,stop

i=4
if 4>6 #false,stop



repeat 3 time , 3 time is the lenght of the list

"big" get a value only the first time in this case

you can see , the piece of code is fairly ever the same
python required only a bit pratice :wink:
(unfortunately is the manual of BGE which lack)

Here ya go:
Just hit P and watch the missile kill all the cubes in the scene.

If the missile’s choices seem erratic it’s just because more than one missile locks on to an object at the same time, and then when it is destroyed has to pick another target.

There is now a few more modules in that script, but I’ve left them out of the blend because they aren’t necessary. (AI against the player mainly.)

Attachments

Homing.blend (646 KB)

I would like to change the homing to have it activate at X distance from an enemy, so the missile flies in one direction, and locks on when its near, I have tried editing the code you provided, but am have some trouble.

Here is what I was going to change that is not working

if ob==own:
continue
if not own[‘property’] in ob:
continue
dist = own.getDistanceTo(ob)
if dist<3

    continue
if distance==None:
    distance = dist
    object = ob
if dist&lt;distance:

alright so now I have the missile working but need to set a delay on allowing the missile adder to fire,
So if I fire I have to wait 5 secs before firing again I thought I could do it with frequency trigger on the keayboard, but If I tap the
key rapidly, it still fires

Thanks again for All of your help!!

@sdfgsdfg:
a little 'uncomfortable to bring up another game(perhaps)
but it works really well!
nice!:wink:

@BluePrintRandom:
you are using the agoose script?

I do not think it is complete… without speed…?

hey, I’m so used to using the static objects(more light on CPU) that I could not move the missile, really, I did not know how!

must be a obj dynamic the missile

this is fully functional and
tested now (2.59)

script for missle:


from bge import logic as BG
from bge import events
c=BG.getCurrentController()
ob=c.owner
sce=BG.getCurrentScene()



    
if not "TARGETFOUND" in ob:
    ob["TARGETFOUND"]=0
    
if ob["TARGETFOUND"]==0:
    minDist=5000
    target=0
    for i in sce.objects:
        if "enemy" in i:
            dist=ob.getDistanceTo(i)
            if dist&lt;minDist:
                minDist=dist
                ob["TARGETFOUND"]=i
                

if ob["TARGETFOUND"]!=0:
    ob.alignAxisToVect(ob.getVectTo(ob["TARGETFOUND"])[1],1,0.2)
    ob.localLinearVelocity[1]+=5
    if ob.getDistanceTo(ob["TARGETFOUND"])&lt;30:
        #sce.addObject("framment",ob,50)
        ob["TARGETFOUND"].endObject()
        ob.endObject()


e blend :wink:

Attachments

missile.blend (123 KB)

Feel free to use the script, but as it says in the header, keep it (the header) there.

I need some help with a simple delay state

1.Object is on state #1 sensing message “Fire”

2.Fire message sent- spawn 1 missile, activate state 2

  1. state 2 switch triggers 3 second delay, after delay switch to state #1

and here is what I have now
CC license


Attachments

BroBotsTest.blend (1.23 MB)

You want it to wait before firing, so you can’t just tap the key to fire faster?

You need a property and then:


Always -----------------------------------&gt; add 1 to property
Keyboard---------\                / -----&gt;set property to 0
property &gt; 30-----\AND ---------/------&gt;Fire

can be done in logic or python

property > 30 how do I do this ?

I see interval, changed, equal, and not equal not greater then or an input condition

interval
min 30 max 1000?

Or an expression