Attack AI and AI attack python code?


(modelinblender) #1

Hi,

I would like to get some code that gets the player to attack the AI using a knife weapon.

The player is called player, and the Ai is called enemy movement.

The proprty on player is player float, and health integer 10. And on enemy movement float.

I do have one screen which is a simple ‘you died’ When the player falls in the center of the scene it goes to that. ‘end’ is the name of the scene so that is the end screen if the AI kills the player.

Thank you


(modelinblender) #2

Okay, well I could probably get this to work somehow with the bricks.

Once enemy hits the player counting down to 1, then scene ends.

So I can only do this with a property brick, I imagine that is the only way to get this going.


(TheDave) #3

I doubt anyone will want to just do the whole thing for you, however if you start learning Python and get stuck on specific issues, then by all means ask for help.


(modelinblender) #4

I don’t know how to program, I tried in the past with it, more than five years ago for a basic 2D game.

Programming isn’t a strong point for me at all, that was why I chose the game engine that was loaded with blender’s now old versions. It had the bricks. That was the only reason. I did achieve something with some help from some of the users on here.

I thought a program in python would probably be simple for a basic mini game like this. I tried yesterday by using a brick to end once killing the enemy, but that happens before the enemy is killed. The program brick being used is a ray for the hand weapon to hit the AI so it is on a counter, I got this from youtube at the time, somebody had a tutorial which showed how to use bricks for a weapon AI use to simply end the AI, I didn’t want that so it needed to be able to be knifed and then another event would be possible, but I have settled on just simply end game with a scene.

It isn’t as if I could create the special effects for the scene needed for the AI to be killed.

How ever while it needs a program for a AI -player attack / die scene, it needs a bit better animation to hit the player back a bit, but the challenge of attacking the AI would probably be difficult. So I’m not too sure tampering with that may give the wrong result, but definitely needs a working program, otherwise it looks broken.


(Lyeb) #5

Some say everyone should program, they’re full of it; logical gymnastics require a certain aptitude.
That being said, Python is very easy to learn, even for begginers. Give it a shot.


(TheDave) #6

It probably would be, for the person making the game who knew exactly what it needed to do. Someone could decide to help you with it, but there’d be a lot of back and forth around what exactly it’s doing, then helping you plug the script in, then inevitably you’ll ask them for help changing the script later on as your game evolves.

While it might be a 5 minute job for a game dev making the script for their own project, it certainly isn’t a 5 minute job to help you in the way you’re asking.

If you’ve hit the point in your game where you need Python scripts in order to progress, then it’s time you learned Python.


(wkk.py) #7

I tried my very best to only use logic bricks in the past, was really proud of my no-python-projects!

But then I started scripting in Python on the BGE, and one thing lead to another, it is now my day to day job to develop software.

Do what you can in bricks, once you get stuck, come to forums with your question, just try to keep the scope narrowed down, asking things like How to make an AI is way way too broad (just an example).

To help a bit on your thread, when attacking, you could have a keyboard sensor triggering a script, from the script you can do all kinds of raycasts, look for the enemy, fetch its life from some gameproperty, decrease the value :slight_smile:

(I don’t really know what you got working as of now and what doesn’t)


(wkk.py) #8
from bge import logic

# what is the controller that is being executed right now?
controller = logic.getCurrentController()

# what is the object that owns that controller?
owner = controller.owner

# scripts can be triggered when a logic brick is going from positive to negative
if all(sensor.positive for sensor in controller.sensors):
    
    ray_length = 2 # blender units
    ray_target = owner.worldPosition + owner.getAxisVect((0, ray_length, 0))

    # we are going to cast a ray from owner to some distance in front of it (+y)
    # the ray will return something if it hits an object with a "life" property
    hit, point, normal = owner.rayCast(ray_target, None, ray_length, 'life')
    
    if hit:
        # from that point, hit is an object
        hit['life'] -= 1

        if hit['life'] < 0:
            # oh no, hit is dead!
            hit.endObject()

I mostly used the online documentation to know what functions were available.
Example: rayCast documentation.

Scripts usually simply check what is the controller calling them, and from that you can get what is the object that owns the Python controller, what is the scene in which that object exists, you can do raycasts, etc…

Just try to change lines and have fun :slight_smile:

Maybe you’ll have to fix things, just try.


(BluePrintRandom) #9

this is how I learned also - people helped me fix my mistakes until I understood how to fix them myself.


(modelinblender) #10

I don’t know, some users are good at 3d modeling and some are good at animation. Programming isn’t in all of us, that is why dev team always has more than one individual.

My task is pretty basic, it is just a fight scene, where the Ai go to the player and hits the player and the health goes down, that is it, there is no end screen. Like wise for the player to kill the AI after a number of stabs.

What is lacks is many textures, and special effects for a complete scene which is seen in the LS film, but with what I know, there had to be a big limitation.


(TheDave) #11

The Python required for what you want to do is incredibly basic; simple ‘if’ statements that activate actuators or change properties. I’d expect everyone to be able to write that kind of code.

For example, lets start with what you want to do in English:

On left click, show firing animation and reduce my ammo by 1.

And now to convert that into Python:

if sensor['left_click'].positive: // On left click
   controller.activate(actuator['firing_animation']) // show firing animation (by activating an animation actuator)
   owner['ammo'] -= 1 // and reduce my ammo by 1

It’s hard to learn something new and unfamiliar, but the longer you put it off, the more you’ll resist doing it. I’m sure everyone here can recall how weird it was diving in to programming the first time. It doesn’t take long before you’re able to use your new knowledge.

-edit-

Just a nitpick, but that entirely depends on the team. My entire dev team is programmers. If you want to write something that works with the rest of your team’s work then it helps (or is arguably required) that you understand at least a little bit of what they do.


(wkk.py) #12

I almost sent everything needed to do the simple task you talked about, the only work required is to actually use it / read it!

On the other hand, if you really think that you shouldn’t do programming, and that you are only good at modelling / animating, then think about teaming up with someone that actually feel comfortable programming (or someone ready to go through the effort of trying).

Games require art for display, and logic for gameplay. As easy as bricks feel, once you hit a wall you will need a touch of Python, so you need to be ready somehow to address that. Games are interactive pieces of art, so you need a logic guy.


(modelinblender) #13

I’m not good at much, the programs were done by some on here, and some of it myself using the video site. Now that the game engine has been cancelled by the foundation, some of what I had used isn’t up anymore.

But that isn’t much of point really worth mentioning. I at least was able to produce something thanks to blender and the bricks. I wouldn’t of made anything I had imagined years ago.

I tried to learn python in the past, some helped via email, and it didn’t work out. This was before blender usage.

As for getting a program going,

Your telling me that alone is what I need to get the player and AI to interact and produced an event of If this or that result?

from bge import logic

# what is the controller that is being executed right now?
controller = logic.getCurrentController()

# what is the object that owns that controller?
owner = controller.owner

# scripts can be triggered when a logic brick is going from positive to negative
if all(sensor.positive for sensor in controller.sensors):
    
    ray_length = 2 # blender units
    ray_target = owner.worldPosition + owner.getAxisVect((0, ray_length, 0))

    # we are going to cast a ray from owner to some distance in front of it (+y)
    # the ray will return something if it hits an object with a "life" property
    hit, point, normal = owner.rayCast(ray_target, None, ray_length, 'life')
    
    if hit:
        # from that point, hit is an object
        hit['life'] -= 1

        if hit['life'] < 0:
            # oh no, hit is dead!
            hit.endObject()

(wkk.py) #14

I am telling you that this would be the player code to run in order to trigger an attack on any object with a life property.

Now as I said, just find someone to work with you as your logic guy.


(modelinblender) #15

I had, but he is too busy. So I either have to try this myself, as I have by inquiring here.

I did another search on youtube, nothing on what I want to do.

Right, so what do I do with the bricks? I will have to remove them before creating a text file window?


(BluePrintRandom) #16

near sensor------------------python controller

python is called by logic bricks


(modelinblender) #17

Okay I added the code to the enemy, and once the player approaches, the screen goes black, so I guess that is end scene.

Hmm, I need the code to attack the player before an event like that?

if hit:
        # from that point, hit is an object
        hit['life'] -= 1

        if hit['life'] < 0:
            # oh no, hit is dead!
            hit.endObject()

(- Click for resources) #18

or use the code within the attack(just look at the script i created for you in the past, it;s the same thing, attack and remove hp.

also do not forget to check if life even exist in the object


    if hit:
        
        if 'life' in hit:
            # from that point, hit is an object
            hit['life'] -= 1

            if hit['life'] < =0:
                # oh no, hit is dead!
                hit.endObject()

also added a = because 0 hp is dead.


(modelinblender) #19

This is a fight scene, where the player must stab the enemy, this isn’t a simple shoot mini game, it requires some dynamic.


(- Click for resources) #20

nope, put a cube with sensor physics on the weapon, parent it.
now you got your weapon ready, add the script and tada hp goes down when the sensor hits the object, if the hp goes to 0 or lower it will end the object