At that stage I strongly suggest to split your code. Here you do not only get two different evaluation behaviors (measuring two different distances. You perform two separate operation as well (adding and ending lungs). The code becomes really complex.
I suggest to switch to module mode and establish various functions that encapsulate the different operations.
module “lungManager.py”
import bge
#bge callable - run it via Module: lungManager.manageLungs
def manageLungs(controller):
if isJustInCreationDistance():
createLungs()
if isJustInDeletionDistance():
deleteLungs()
With that you look from a very high level on what should happen. The details are encapsulated into the different functions at a different place (typically at the same module, but they could be at an imported module too).
Here I would say … have fun implementing isJustInCreationDistance(), isJustInDeletionDistance(), createLungs(). You already have the according code.
But I know you are just confused right now. Therefore I show you samples.
Creation distance evaluation
def isJustInCreationDistance():
owner = bge.logic.getCurrentController().owner
player = owner.scene.objects["player"]
currentStatus = owner.getDistanceTo(player) > 29
lastStatus = owner.get("last creation distance evaluation", False)
owner["last creation distance evaluation"] = currentStatus
return currentStatus and currentStatus != lastStatus
You might remember this looks pretty much the same as in post python code suddenly stopped working.
As you have at least to distance checks you need to use different internal properties (otherwise you would not be able to differentiate between the two stored evaluation states).
Indeed the code looks a bit complicated, but it runs as it is. It just needs a “import bge” statement.
Deletion distance evaluation
Now have a look at the second evaluation:
def isJustInDeletionDistance():
owner = bge.logic.getCurrentController().owner
player = owner.scene.objects["player"]
currentStatus = owner.getDistanceTo(player) > 30
lastStatus = owner.get("last deletion distance evaluation", False)
owner["last deletion distance evaluation"] = currentStatus
return currentStatus and currentStatus != lastStatus
This code looks pretty much the same as the code for creation distance evaluation. The differences are:
- different distance to check (30 rather than 29)
- different property name (“last deletion distance evaluation” rather than “last creation distance evaluation”)
With a bit of redesign could merge these two functions into a single one with arguments. But this is not what I wanted to show. I wanted to show that even when this “lower level” function looks complicated, you “higher level” funciton stays very simple and still expresses what should happen.
But lets continue to the next function.
Creating Lungs
my first approach is very simple.
def createLungs():
print("create lungs")
This allows me to test if the code gets called at all (so I can test to above functions). When I’m sure it gets called as I want I can start detailing how to create lungs. Again you already did that in the mentioned thread (called actator part). So lets put it into the function.
def createLungs():
owner = bge.logic.getCurrentController().owner
player = owner.scene.objects["player"]
positions = [[3,4,5],[2,2,2],[1,1,1],[3,3,3],[8,8,8],[10,10,10]]
reference = player
for i in range(0, len(positions)):
addedLung = reference.scene.addObject("lung", reference, 0)
addedLung.worldPosition = positions[i]
The benefit is the “creation” code is isolated from the distance chakes or anything else. So oyu can focus on what the code should do … create lungs.
Deleting Lungs
Here we do the same thing - test with a simple implementation
def deleteLungs():
print("delete lungs")
How to implement that I explain with the next post.