If you want to have a general code avoid the use of object names within the script as much as you can
… NO …
just do not do that!
It makes the code very, very, very inflexible. Especially if the required name is hidden in a bunch of code lines (as in your case).
If you really need it that way, define the string at the top of your code. Add some comments, that the reader can easily identify it as requirement.
e.g.:
AimerName = "Aimer" # requires an object with this name
...
aimer = allObj.objects[AimerName]
So it might be better to define the name in a property of the owner. Which should be setup at the top of the code too:
# mandatory configuration properties
PaimerName = "aimer" # the name of the aimer object (the object must exist)
...
aimerName = own[PaimerName]
aimer = allObj.objects[AimerName]
With that you can setup for each object that executes this code with an individual “Aimer’s name” in a string property “aimer”.
This is flexible.
But it can be more efficient.
You can cache the already found object reference:
# mandatory configuration properties
PaimerName = "aimer" # the name of the aimer object (must exist)
#internal properties
Paimer = "_aimer"
...
aimer = getByNameCached(own, Paimer, PaimerName)
...
def getByNameCached(own, key, nameProperty):
'''
own = owner
key = lookup key of the cached object
nameProperty= property to find the object name if it is not cached
'''
obj = own.get(key)
if obj is not None:
return obj
obj = getByName(own, nameProperty)
own[key] = obj
return obj
def getByName(own, nameProperty):
'''
own = owner
nameProperty= property to find the object name if it is not cached
'''
objName = own[nameProperty]
obj = allObj.objects[objName]
return obj
That means only the first access is looking for the object (inperformant). All further accesses are simple property reading (much more efficient).
I guess from your post that you have the aim object as child of the code owner?
Than you have much better chances with:
own.children
You just need to know what children to look for. Use an property to mark the object as aimer. It does not matter what value or type it has. If the property “aimer” is present the object is marked as an aimer. Avoid multiple aimers at one object. Only one will be taken:
...
# optional configuration properties
PisAimer = "aimer" # marks a child of owner as aimer
#internal properties
Paimer = "_aimer"
...
aimer = getChildWithPropCached(own, Paimer, PisAimer)
...
def getChildWithPropCached(own, key, indicatorProperty)
'''
own = owner
key = lookup key of the cached object
indicatorProperty= property to identify an object
'''
obj = own.get(key)
if obj is not None:
return obj
obj = getChildWithProp(own, indicatorProperty)
own[key] = obj
return obj
def getChildWithProp(own, indicatorProperty):
'''
own = owner
indicatorProperty= property to find the object name if it is not cached
'''
for child in own.children:
if indicatorProperty in child:
return child
Rather to look through all objects, this looks through the children only. The found gameObject will be cached.
I wrote down the code from scratch. It is untested und can contain errors.
I hope you get the idea