Python Script Help

Why isn’t this working? :smiley:

import GameLogic
cont = GameLogic.getCurrentController()
Kay = cont.getOwner()
WhereKayisAt = Kay.getPosition()
Smack = applyImpulse()
daforce = [0.0,10.0,0.3]
Kay.Smack(WhereKayisAt,daforce)

I have a Keyboard sensor connected to this script so that when I press a key it should move the owner object with an applied impulse but it keeps telling me

“name applyImpulse not defined”

Well, the interpreter said it best: “name appliedImpulse not defined”

However, even if it was, the variable “Smack” would only receive what the function appliedImpulse returned (if it returned anything). For example:


def getSum(x=0, y=0)
    return x + y
  
Smack = getSum() # Smack is 0, because 0+0=0
Smack = getSum(1, 2) # Smack is 3, because 1+2=3

Smack = getSum # Now "Smack" is a shorthand for the function getSum()

# And now you could use it in place of getSum():
print Smack(5+5) # prints 10

# !!!!! NOTE !!!!!!!
# "Smack = getSum" does not "replace" every instance of "Smack" with "getSum"
# Python does not "cut and paste" like that.
# Instead "Smack" becomes a *reference* to "getSum"

Anyway, going back to why the interpreter complained; the function “applyImpulse()” does not exist in your global scope; It’s a method of the owner, or in your case, the “Kay” object.

So, the working version of your code would look like this:


cont = GameLogic.getCurrentController() # GameLogic is imported automatically
Kay = cont.getOwner()
WhereKayisAt = Kay.getPosition()
Smack = Kay.applyImpulse # "Smack" is now a reference to "Kay.applyImpulse"
daforce = [0, 10, 0.3]
Smack(WhereKayisAt, daforce)

A cleaner, more “general purpose” version could look (and be used) like this:


cont = GameLogic.getCurrentController()

Kay = cont.getOwner()
Social = GameLogic.getCurrentScene().getObjectList()["OBSocial"]
Jimmy = GameLogic.getCurrentScene().getObjectList()["OBJimmy"]

def Smack(dyn_GameObject, daforce):
    dyn_GameObject.applyImpulse(dyn_GameObject.getPosition(), daforce)

daforce = [0, 10, 0.3]
Smack(Kay, daforce)

Smack(Social,  [0, 0, 0]) # Let's face it....no one would dare.

daforce = [100, 100, 100]
Smack(Jimmy, daforce) # He's been a very bad boy.

Lol, well, if there’s still something not working, or just something you don’t understand, feel free to ask.

okay thanks! now I understand the situation a lot better

I do have one more question. Which is better for performance, a lot of small simple Python scripts. Or a few big and comprehensive ones?

thanks

It all depends on what you’re doing in a script, and how often each script is executed.

I would suggest going the “few big and comprehensive ones” route, not because it’s necessarily better for performance, but mainly because it’s better for managing complexity.

If you have a lot of “little things” spread out over many objects, it’s much more difficult to keep track of active functionality, or manage rising levels of complexity.

Ideally, there would be an option to use a single python script from which we could access all BGE functionality. Sadly, there seem to be no developers competent enough to implement such an option.

So yea, a “few big and comprehensive ones” is the next best step.


Smack(Social,  [0, 0, 0]) # Let's face it....no one would dare.

Haha. I couldn’t resist. I had to reply and laugh.
You’re right, no one would dare to smack, punch, slap or gently push you without asking permission.

- AniCator

PS: Hmm… On the other hand. I wouldn’t slap you at all even if you gave me permission to slap you.

Smack(Social, [0, 2, 0])

if Social.isPissed():
me.runForDearLife()
else:
me.nervouslyLaugh()

Couldnt resist = )

http://s2.subirimagenes.com/imagen/previo/thump_822261roflmao.gif