Advanced python scripting tutorials for BGE

Morning all,
I was wondering if anyone knew of some advanced python scripting tutorials for the blender game engine. I want to script the AI for my game. I was wondering if blender had methods for figuring out the dot product and the cross product or in other words the distance from one object to another and the location of one object from another.

My goal is to make two bots.

Bot 1: Patrols till the player comes close enough, then persues, then when in range, shoot.

Bot2: Locates the closestā€¦ lets say gold coin, moves toward the gold coin, picks up the gold coin, finds the next gold coin, repeat.

Any AI examples would be greatly appricated. Iā€™ve searched the forums and google but havenā€™t found quite what I am looking for.

Thanks in advance,
delmarco

P.S. Anything I finish I will post back with comments to help the rest of the community!

I did not use python,

But here is an AI example, it is for a pathnode based AIā€¦ i give instructions on how to do other behaviours as wellā€¦

you can translate logic blocks into python code pretty easyā€¦ I bet you could improve lots of things with scripts :slight_smile:

hope this helps

http://blenderartists.org/forum/showthread.php?t=73680

Poof,
Thanks for the response, but I didnā€™t see a link to follow. Would you please post it?

thanks!

Oh, I see it now!

oh wow!

sorry about thatā€¦

Here you go
http://blenderartists.org/forum/showthread.php?t=73680

I edited the first post after I clicked the post buttonā€¦ sorry about thatā€¦

I may do a more advanced ai later today if I have timeā€¦ I can add a lot of other behaviours to the monster proxies nowā€¦

I plan to write a continuation of my ā€œBeginning BGE Pythonā€ tutorial, as soon as I find some free time. Seeing how AI is such a popular topic, Iā€™ll probably base the tutorial around a simple enemy AI setup. Unlike my previous tutorial though, this one will be for people who have grasped the basic concept of the BGE API quite well. Meaning that just going through my first python tutorial wonā€™t be enough (I suggest taking some of my suggestions at the end of the current tutorial, and trying to add new functionality to that controll scheme example file).

The problem with this whole thing is not so much with the lack of documentation on the matter, as it is with the people who are looking for it. Usually these people are fresh green rookies with the BGE in general, yet all of them want somekind of a magical tutorial that can make them into programming experts overnight. Of course theese tutorials donā€™t exist, and I personally donā€™t think I could ever write such a tutorial even if I tried.

The point is: As with many other complex things, programming and game design are skills that usually require a few years to be learned properly.

So true, social, so true

My blender/python scripting skills are not the strongest. I think even more I need to brush up on my vector math skills. I also donā€™t know how to express exactly what I am looking for in a tutorial. After I finish the AI for this game (its due at the end of this month:eek:) I will post the .blend and maybe we can use it as one of those intro tutorials.

If you wouldnā€™t mind pointing me in the direction of some scripts that do implement some basic AI, it would be greatly appricated!

Thanks

Here, I made a quick example for you.

Download

I kept the script short and simple. It shows the very basics.

Hope it helps you out.

Shock and Awe, shock and aweā€¦

all that would be needed to do this should be a little 3d math, the scripting portion should be simple I believe.

basically having the gold coins in a list, get the position of the AI character, then doing the math of what distance it is to each of the gold coins in the array, keeping track of the shortest distance (should be simple geometry to get the distance if youā€™re only using 2 axis, 3d math needed if your X,Y, and Z are all used) then adding a trackto actuator

I havenā€™t looked at Socialā€™s code so he might have a different/better way of doing it, Iā€™m just explaining the concept of one solution.

1 Like

1)Social,
That tutorial is amazing. I am going to spend the next few days implementing it in my game. I was thinking too low level and trying to do alot of the math myself. Can I help you in any way finishing the tutorial? I wouldnā€™t mind writing up the steps and getting play by play screen shots. Let me know what you think.

2)McAden,
I could swear I have the same write up as my solution for the project! Iā€™m glad I am on the right track! I do have a request if youā€™re game:

Lets say I have N coins, thier object names are coin1, coin2, ā€¦ ,coinN. Is it possible to have blender/python search the scene and return the # of coins. A better question would be how would I go about making that list of coins besides hard coding the amount in a for loop

list_Of_Coins

//is this the only way??
for (i=0, i< N,i++){

list_Of_Coins[i]= coin.append(i)

}

I donā€™t want to do it this way because:

  1. Iā€™d have to declare the number of coins before hand
  2. Iā€™d have to update this list during every pass (I may have to do this anyway)
  3. Even if the coin is not in the scene anymore I would have to count it, or at least check for it.

Let me know what you think

Oh by the way, here is how we solve for distance:

import math

loc1 = object1.getLocation()
loc2 = object2.getlocation()

distance = sqrt(loc2-loc1)
#I am assuming blender is subtracting each element ie. x2-x1, y2-y1, ect.

Thanks for everyoneā€™s help!

You guys are thinking far too much in C. There is no need to do distance calculations.

Here, I made an example for the coin collector. (I think itā€™s along the lines of what you asked for, you can modify it additionally if need be of course):

Download

I simply used an 8 sided cylinder with a size (expansion) IPO to improvise a ā€œsonarā€. I kept the script very simple (again), so that you could get in the swing of things quickly.

When my IPO driven sonar hits the closest coin (by expanding), I use ā€œgetHitObject().getPosition()ā€ to get the location of the coin. Then I set the Position of a ā€œGuiding Emptyā€ (to which I have the collector tracking) to the same position of the hit coin, which gets the collector moving to the closest coin.

Simple.

Heh, I didnā€™t start it yet. The examples I gave you here are ā€œquick and dirty thingsā€. Actually Iā€™m not even sure if my next tutorial should be on AI. Iā€™m just brainstorming at this stage.

Iā€™m ā€œplanningā€ to write one, but itā€™s hard to find the time to do it right. Anyone can put together examples, but good documentation is a whole other story.

Thank you for offering your help however. I appreciate it.

Social,
Great example! You are right once again I was thinking to low level. The BGE API is deeper then I thought. There is also a radar sensor, have you ever messed around with that?

My friend and I are finishing the soundwave game for a class. Iā€™ll probably have another post by the end of this week. I will have to comment anything I do for class so by the end it should be a decent example.

I have an idea for a guided path. Put ā€˜markerā€™ nodes along the path with thier own property. So the sonar not only returns the nearest coin but it also returns the cloest ā€˜markerā€™.

Thanks again for your help, its appricated!

Yes, I pretty much messed around with everything at this point (Iā€™v been using blender for 2 yrs now).

The near, radar, and touch sensors have not been very stable for me in the past, so I try not to use them all that much (at least until the bugs get ironed out completelly). Actually, I donā€™t really need them, because I can always write a workaround with some other sensor, or just do everything with pure math (what you call ā€œlow-levelā€).

If your interested in learning more about logic bricks and how to use them visit the docs:

http://download.blender.org/documentation/NaN_docs/BlenderGameReference/logicbricks.html

I wish theyā€™d update that documentationā€¦

Social : thank you very much for your Examples. your scripts for AI are very interesting.

Actually, those docs are still pretty valid. I mean they are old, but I donā€™t see anything missing.

Do you?

No problem, Iā€™m glad you find it interesting.

As you can see, setting up basic AI in the BGE is no big deal. Many people just tend to think of ā€œAIā€ as something beyond complex when mentioned.

However, in many cases ā€œAIā€ is nothing more than just a glorified event handler.

those examples were very good, do you have any more?

I only write examples for cases dealing with things that are not that well documented within the BGE community (like AI).

I suggest you make a new thread describing exactly what you are having trouble with.

Iā€™ll try to help you out either way, but weather or not Iā€™ll write an example will depend on the question.

Hi Social ! I tried you AI_Coin and it only goes to the first coin and just stop there.

Itā€™s the 2.43 parent bug thatā€™s causing the problem. I made a quick workaround for it, and updated the original link, so just download the .blend again.

Heh, sorry, but thatā€™s how blender works. Things that work fine in one version, may or may not work fine in the next. I wish there was something I could do about it.