ANOTHER QUESTION ON PAGE 3 :P Project LearnMorePython

SolarLune, you have saved my script again, thank you. :stuck_out_tongue:

I have successfully implemented a run animation and movement into my script without any problems ('cept one tiny one that I will fix later.) I will update the 1st post with my updates. :stuck_out_tongue:

QUESTION!!! I am doing attack for my character.

1st question, I want my character to stop when I push the attack key (if he’s moving). What I did so far was, whenever the attack key is pressed, my and mx are set to 0. But it only happens for a second then my character keeps moving forward (if I’m holding the forward key). How would I make it longer?

2nd question, If my character has a cube on his hand, so that when he punches, the cube on his hand hits the enemy. How would I activate and deactivate the properties so that if his hand touches a monster and he is not using the attack button it won’t hurt the monster?

For both questions, variables are the answer. You can define variables and store them for usage in a game object, for example, by using obj[‘variable’] = value. So, you could set an ‘attackingstate’ variable to be 1 when you press ‘Attack’. If that variable is 1, then you shouldn’t be able to move (in other words, you’ll only be able to move if ‘attackingstate’ is 0).

… So I won’t need to add a property in the property thing in the game logic window? I will see what I can do! Be prepared for updates/questions! :smiley:

QUESTION: I know, it was fast.

  1. Should I create a whole new python script for the attack cube and have a python brick and always sensor on that attack cube?

  2. Is there a way I can show debug properties in-game for properties in python code?

You can create different scripts for each individual action. I prefer to place all relevant code together (i.e. all Player related code together). Also, it would be of some use to use an external file editor, as they support things like syntax error highlighting, code correction / suggestion, and code folding. I would recommend Notepad++ - there’s also a code completion file for Blender on my site, which I recommend looking at, as there are several tutorials on Python there.

For your first question, you can do that - it would be a valid method of attacking, though there are alternate methods. As for your second question, there’s no built-in way to display properties - you just have to use the print function along with the console. However, I believe you can display Python values onscreen somehow with another Python module (Monster did something like this, I think).

My brain is mush right now. Are there any tutorials about doing attacks with the properties using python and stuff?

I would suggest something like:


if attack_key.positive:
    own["attack"] = True
else:
    own["attack"] = False

if collision.positive and own["attack"] == True:
    obj = collision.hitObject()
    obj["health"] -= own["attack_value"]
elif collision.positive: and own["attack"] != True:
    pass

Ok, I think I figured part of it out. But I still am having some problems. Here’s the code.

import bge
from bge import logic

cont = logic.getCurrentController()

obj = cont.owner

obj[‘hp’] = 1

collide = cont.sensors[‘Collision’]

if collide.positive:
obj[‘hp’] -= 1

if obj[‘hp’] <= 0:
obj.endObject()

This is the enemy health code. The 2 problems I am having are:

  1. If I change the numbers to “2” hp, and I play the game, I hit the enemy and his health goes down to 1, then it goes back up to 2 for some reason.

  2. I still am not sure how to make it so the block only hurts the enemy when I attack, and not when I move right next to him to where the block touches him.

Please respond. :stuck_out_tongue:

I figured out how I would do number 2. I send messages. :stuck_out_tongue:

You know, you don’t have to use messages. You can directly set variables with Python:


hitobj = hitsensor.hitObject

hitobj['damaged'] = 1

As for your first question, the enemy’s HP always goes back to 2 because you’re constantly setting it to 2. You need to add a check to ensure that it only gets set once:


if not 'hp' in obj:
     obj['hp'] = 1

Here is my health code for the player:

if not ‘hp’ in obj:
obj[‘hp’] = 5.0

if enemycollide.positive:
obj[‘hp’] -= 1.0
if defending == 1:
obj[‘hp’] -= 0.5

if obj[‘hp’] <= 0.0:
obj.endObject()

The problem that I am having is that, whenever I touch the enemy cube, my health goes from 5 to 0 almost in a split second.

Since the code runs 60x a second, you could just subtract a tiny amount each frame.

or, do you want it to only hurt you the instant you first touch the cube, like this?

if enemycollide.status == logic.KX_SENSOR_JUST_ACTIVATED:
    obj['hp'] -= 1.0

Thanks, Lakitu. I have taken some stuff out of the first post for personal reasons, so my next thing to do is make my character pull himself up onto a ledge like link does in zelda games.

Well I’ve already got a question. I have it set up so that when the player collides with a block, then the pulling up animation will play. But, how exactly would I make the entire character actually get up onto the block?

What I do is suspend the character’s dynamics, then play an IPO of him moving up and over (with “Add” enabled so he moves relatively), then finally restore the dynamics again when he’s at the top.

But, how exactly would I make the entire character actually get up onto the block?

You could suspend the dynamics, play an IPO of him moving up and over (with “Add” enabled so he moves relatively), then finally restore the dynamics again. It might work better if you use a different state for ledge climbing.

Thanks, Lakitu! I have another question. I know there is a “blablabla.positive” but is there a “blablabla.negative”?

I can suspend dynamics at the right time, but how would I make the cube restore dynamics just when the animation finishes?

For your first question, no, there isn’t a sensor.negative. However, you can use not sensor.positive. Well, you can track the current frame of the animation in the actuator (use the FrameProp box and set the property). Then, read the object property and, if it is at the end of the animation, use obj.restoreDynamics().