Non-Python Way to Target Lock-On Action RPG?

Hi.

So I used an Edit Object Track-to actuator to lock onto an enemy,

But in the game logic, I can only select one object.
How do I make my character lock onto the nearest enemy?
And then after that enemy’s defeated, how do I make my character lock onto the next nearest enemy?

The only thing I can think of is to have an Edit Object track-to actuator for each and every single enemy.
Each activated when each enemy is near.
But there must be a more efficient way for a game that has lots of enemies.

Thanks.

The responsibility of the TrackToActuator is to track a single target object. It does not care finding the target object (unless you set the target as name via python).

Imagine how many options there are to do so. The tracking will still be the same.

Hundreds of trackToActuators will not help as you still do not know which one to activate.

There is nothing build-in that provides you the closest enemy (unless there is only one). You will need Python to do the distance checks.

I suggest you look for threads dealing with “closest object”.

Don’t try to make a RPG if you don’t want to use Python.

you can change the track to object in python…I do not see a way to do it without some scripting…


    cont = bge.logic.getCurrentController()
    own = cont.owner
    scene = bge.logic.getCurrentScene()
    obj = scene.objects
    someobject = obj['someobject']
    track_to = own.actuators['Track to']
    
    if own.getDistanceTo(someobject) < 3:#if my distance to the object is less than 3 units(meters for me)
        track_to.object = someobject
    else:
        track_to.object = None#or a different object etc

Monster is correct though…you need some way to ‘point’ to that object…you can build a list that stores the objects that are within range of the player…

and assign a key to ‘tab’ through the objects in the list…

I recommend you investigate the use of python(I’m still learning python myself ;))
specifically how to use lists(read, write, index)

using ‘for’ loops

and looking for objects with a specific property inside the ‘for loop’…these are actually all trivial things to do, but if you are very new…your best bet is to go to youtube and watch some BGE tutorals or google ‘BGE’ + for loop, or the other things I mentioned.
Try to lean a bit on your own…if you get stuck…comeback, more than likely if people see you are legitimately trying they will help you out :wink:

as for testing the distance of one object to another…it is a simple line of code as seen above in my example.
I realize this is all a bit off subject from your question…but it is meant to guide you and not lead you down the wrong path…bricks are fine for jumping games or very simple things but, as 3D Upgrade said…you cannot make an rpg with bricks…you would go insane trying.

Well, if you really want to use logic bricks, you can actually! But it’s extremely limited and it wouldn’t be flexible.

You could setup a Near sensor that detects one thing you might want to lock-on to, and a button to activate it. When the sensors are true, you could then switch to a different state where the player is tracking to the same object the near sensor detected (this would be manually done). In this state switch back to the previous state if the button for lock-on is released.

Yes, you’d need to create a separate state for every singe instance of an object. Yes, it would use up a lot of states. Yes, the maximum amount of things to lock-on to would be 29. You also wouldn’t be able to lock-on to the nearest thing, you’d lock-on to the first object one of the near sensors activates on.

If you want to stick with logic bricks, try simplifying your game - otherwise you will need to learn some python (there’s not much python required to do this simply).

import bge
cont = bge.logic.getCurrentController() 
own = cont.owner 
NEAR = cont.sensors['NEAR']
near = [] 

Target = None
Length = len(NEAR.hitObjectList)
if Length==1:
    Target = NEAR.hitObject

elif Length>1:
    for ob in NEAR.hitObjectList:
        near.append([ob, own.getDistTo(ob)]) 
    near = sorted(near, key=lambda x: x[1])
    Target = near[0][0]
    
if Target:
    V2 = own.getVectTo(Target) 
    own.alignAxisToVect(V2, 0,.1)
    own.alignAxisToVect([0,0,1],2,1])

If you want to track to closest, you may want to consider point on line from view angle for a FPS

you can use a lot of different methods for cycling through, my favorite in games is lowest health :wink:

Thanks everyone for your support.

Two more questions:
Question 1:
Do you recommend this place to start learning Python?
https://en.wikibooks.org/wiki/Non-Programmer’s_Tutorial_for_Python_3
EDIT: for RPG dialogue / I can’t find any targeting stuff in the table of contents.

Long version of Question 1:
Is there a go-to tutorial site like this?:

  • skips the 1+1= stuff.
  • “this is the simplest way to do this simple game-mechanic thing (shows video or gif)”
  • explains the code, or links to where to learn it.
  • ready to test in Blender i.e. hands-on tutorial.

I took a university c-coding class once.

I tried Codeacademy,
but it was way too basic,
and I couldn’t test it in blender,
just wasn’t motivated.

As for most people’s responses on these forums, I just don’t understand their code.

Question 2:
Is there a chance I’ll have to recode a bunch of stuff when a new version of Blender/Python comes out???
I read that version 2.5/2.6 are completely different than older version :confused:
https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Introduction

I once asked a similar question, and people said not worry too much since old skills will transfer.

I’ll probably have to regularly test, review and understand my code.

name the near sensor NEAR

NEAR------------[THIS PYTHON]



import bge

#get access to bge commands


cont = bge.logic.getCurrentController() 
# this is the python controller calling the script


own = cont.owner 
#this is the gameobject containing the controller running the script


NEAR = cont.sensors['NEAR']
#this is the near sensor connected to the controller


near = [ ] 
#we create a empty list


Target = None
#we set a property called Target to None 




Length = len(NEAR.hitObjectList)
#we see how many objects are inside the near sensor


#if only 1 object look at that
if Length==1:
    Target = NEAR.hitObject


#if more than 1 object sort a list of objects, and get the closest
elif Length>1:
    for ob in NEAR.hitObjectList:
        # we build a list of tuples - small lists
        near.append([ob, own.getDistTo(ob)])


    near = sorted(near, key=lambda x: x[1])
    #here we sort the list by the second value in each tuple
    #this is advanced python, so read up on it some time it is handy
    

    #this is the sub list with the lowest distance 
    smallList = near[0]    

    #the first value of the sublist is the game object
    Target = smallList[0]


#if Target is not None 
if Target:
    #instead of using track to actuator we can just do the alignment in python
    V2 = own.getVectTo(Target) 
    #Get vect to returns a list
    distTo = V2[0]
    angleTo = V2[1]
    #this is the angle and distancec to a vector or object
    
    own.alignAxisToVect(V2[1], 0,.1)
    #this command aligns the axis of a object to a angle over time
    own.alignAxisToVect([0,0,1],2,1)
    #this keeps Z axis pointing up, one could also use a normal of a surface
    #one could also use a local axis of a parent to make a turret etc
    

This is the way I learned!

code comments are your friend!

ask questions and break stuff.

Attachments

NearTrack.blend (430 KB)

Thanks, BluePrintRandom!
By looking at your comments, i just realized cont means controller!!!
This really helps as a starting point,
because it’s more motivating to look things up when I can see the results,
and comments explaining things.
Thanks!