i am learning how to script

(alf0) #1

hi everyone

so i want this thread to be as a tutorial ,section for me , i want to learn python scripting for the bge and i want to start step by step,

i have a character girl that i named her “dal” and i want to start testing my scripting on her,
i made all the animation,s that i need for her and i just want to control how she moves by scripting instead of logic

what i want is for those who are willing to help me just write the code i want on this thread and ill test it and ask for more every time and then ,if you dont mind

so the character mesh is parented to the armature and the armature is parented to the player phy body,

what i want is:
when i press [w,s,a,d] the character would move to each direction ,
the directions are a track actuator with a time of 6
while the player body move,s the armature plays a run animation
and if nothing is pressed and the player body is touching the ground by ,ray (-z) axis , a stand animation is played
all of this works only if a property call move is true , on an empty attached to the player phy body
if it,s not true then instead of ruining she will walk

post if there is something i didn’t mention
and please please i need this

NOTE: i can do all of this using simple logic script with the logic brackets , but i dont want this anymore

for more of the character :https://www.artstation.com/artwork/bkGRk

(MichelleSea) #2

I think this is a great idea, and I love your initiative for learning like this!

However, I think you’d be better off learning if people explain code, instead of just writing it for you. :stuck_out_tongue:

Are you familiar with how Blender’s modular running works?

(alf0) #3

first of all thank you for saying that,
it,l really be a nice thing if thy explained it
, and sadly no, i know nothing about it ,

(Fred/K.S) #4

Join us on Discord !!!
Then you’ll learn !!!

Srry here’s the proper link !!!
BGE Coding

(MichelleSea) #5

Okay, so the Python controller has two subtypes, “Script” and “Module”. “Script” runs and executes the entire script file when it’s activated, while “Module” can cherry-pick certain parts of a script to execute. This is good for things like organization and cleaner code, at least in my experience. It also lets you do more with fewer script files.

A good practice when you’re writing scripts is to name your text files “nameofscript.py”. The “.py” tells Blender that this is a Python file, and that’ll let you access the files modularly.

I also like module-running for the fact that I don’t have to use a main() module in my scripts. I tend to just treat scripts as catalogs of modules to run.

So say I want to write a script to make my player walk. Here’s what I’ll do:

  • First, I’ll make a script called “player.py” in Blender’s text editor.
  • I’ll then add a “Python” controller to my player, with the subtype “Module”.
  • I’ll type “player.walk” into the Module field of the controller. This tells Blender that I want to run the “walk” module within the “player” script file.
  • I then go to write the script, starting with a definition:

def walk():

  • I don’t have time right now to get into the script itself, sorry :stuck_out_tongue:
  • I’ll add any sensors and actuator I’d need (such as keyboard sensors and movement or track-to actuators)
  • Then I’ll test it with the coneole open to make sure it all works!

That’s a good basic rundown, I think. I’m sure that those more advanced than myself could elaborate further.

(alf0) #6

wow @MichelleSea this really great , even though it,s just the first step of coding, but you still did something to me thanks,
ill try to remember this

(AudioFreakXD) #7

And to add to modules is that you can also write basic code in one script file so that you can keep the files to a minimal. For example if you are writing walking, attacking, jumping, animations you don’t want a bunch of script files to clutter up. It makes it unorganized and you’ll have to scroll and scroll if you are trying to find a script to update/optimize. Modules are great for organizing amongst other things.

What helps the most is intro to python. You wanna know the basics and you want a clear demonstration and a full explanation. There are great youtubers out there. Anyways, the first thing I learned about python is that python is real time and that it doesn’t use symbols like {}. It uses colons and indents.

(Nick Manchul) #8

… with the logic brackets , but i dont want this anymore

  • yeah man, Python (and scripting) is cool!:cool:
import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
keyboard = bge.logic.keyboard

Wkey = bge.logic.KX_SENSOR_ACTIVE == keyboard.events[bge.events.WKEY]

body = scene.objects'[body_dal']

if Wkey:
    body.playAction("bodyAction", 0, 25)

This is a simple code, when you press and hold the “W” key, the animation is constantly playing

here more about animation

(Cotaks) #9

yeah man, Python (and scripting) is cool

Better start using module mode, for everything, even 1 line is faster within the module mode, so your script can/will be like:

from bge import logic, events

def your_function(cont):
    own = cont.owner
    scene = own.scene
    keyboard = logic.keyboard
    Wkey = logic.KX_SENSOR_ACTIVE == keyboard.events[events.WKEY]
    body = scene.objects'[body_dal']

    if Wkey:
        body.playAction("bodyAction", 0, 25)

  • scene grabbed from the owner
  • controller (cont) from the function itself
  • importing modules rather then importing the whole bge

Writes faster, works faster, is faster.

(Nick Manchul) #10
  • no problem Cotaks - faster so faster. Only it seems to me that BGE modules are connected only when they are addressed to them. At least in the examples of their official reference they turn completely to BGE, and afterwards through BGE to the right module. - Is there really a difference in performance?
    For some reason it seems to me that the string “import bge” hangs in the empty until it is addressed to the necessary module through it.

(Cotaks) #11

Only it seems to me that BGE modules are connected only when they are addressed to them //For some reason it seems to me that the string “import bge” hangs in the empty until it is addressed to the necessary module through it.

so it really waits for your input, that is never good. And what behind true pulse it loads in the bge every tic?

I see it like this:
BGE is a class, with tons of sub classes, while not all code get’s read from the sub classes, their title/function gets read and processed.
So if bge holds for example 150 sub classes, all of them get’s processed and if you only import 1 or 2 from the bge that in my eyes is way faster.

Just basic math and my opinion about importing bge or just a module out of the bge.

But with that said, what is easier/faster to write?
bge.logic.addScene() or just logic.addscene()
bge.logic.getCurrentController() or grabbing no controller at all, due to the function already provide this for us.
bge.logic.getCurrentScene or own.scene

So you lose the bge. every single time you need it (it’s a lot) and longer lines that you always use over and over are now way shorter, this makes your scripting easier, faster, and faster to write for yourself.

(Daedalus_MDW) #12

the only speed gain of importing a few modules is on load and having one less dot to lookup.

python only processes things which are directly called, which is what makes it painful to test. you have to get your code to run every line. with the exception of some syntax errors.

(Nick Manchul) #13
  • It is clear, but 150 sub classess like overdone it already … Of course, it’s better to write a short code, without repetition.

(alf0) #14

@Nick Manchul _ @Cotaks this is very nice thank you both, for your time,
i dont have to worry about attacking a brick and name it again,
i tryd both of the script,s and i note,s some thing,
on this section

““if Wkey: body.playAction(“bodyAction”, 0, 25)””

what is the name of the armature and what is the name of the body that contain,s the armature ?
where is the section where i should name the animation that i want to play?

i looked up on the https://docs.blender.org/api/2.79/bge.types.KX_GameObject.html?highlight=playaction#bge.types.KX_GameObject.playAction but i didnt understand a thing at all,

(alf0) #15

ok this is a new character i call her hind.

it,s fully rigged, animated, and ready parented to the other objects, like the father object, then the armature thin the character body,

her is a link for it if you would like to check it out and understand it before helping


(Cotaks) #16

what is the name of the armature and what is the name of the body that contain,s the armature ?
where is the section where i should name the animation that i want to play?

to break it down

wkey = key_w #the key details
body = own.scene.objects['your_armature']

action_to_play = 'your_armature_action'
start = 0
end = 25

if Wkey: 
    body.playAction(action_to_play, start, end)

(alf0) #17

oh ok, ill try this one
and sorry for the bad attachment above

(alf0) #18

hi again

so i did tried the 2 script,s above and thy work perfectly, and i note,s something wile using it.

the play method is always set to play , i went to the blender KX game object https://docs.blender.org/api/2.79/bg…ect.playAction
and i searched for a way to change the play mode from play to loopstop ,
i found this script

“” playAction(name, start_frame, end_frame, layer=0, priority=0, blendin=0,play_mode=KX_ACTION_MODE_PLAY, layer_weight=0.0, ipo_flags=0, speed=1.0,
blend_mode=KX_ACTION_BLEND_BLEND) “”

which i didn’t understand, specially this part


1-how can i set the PLAYMODE from paly to LOOPSTOP ,
2- how can i trigger the track actuator ,


where can i set the target and time of the tracking

thanks again

(Liebranca) #19
  1. As far as playAction knows, mode can be one of three values: 0 = play, 1 = loop, 2 = pingpong. If my memory isn’t lying to me you’d then have to halt playback manually ( body.stopAction(layer) ) so that it doesn’t animate until the specified end frame. You might find this useful: http://bgepython.tutorialsforblender3d.com/Action , each page has an example.

trackTo = cont.actuators['trackTo_actuator_name']
target = trackTo.object #you can assign an object reference to this
time = trackTo.time #likewise, you can assign a new time value here

bge.logic. KX_TRACK_UPAXIS_POS_Y is a constant you can use to specify to the actuator which axis the object should
rotate on. No need to bother bother writing it like this though…

trackTo.upAxis = 0
#it's the same as
trackTo.upAxis = KX_TRACK_UPAXIS_POS_X


trackTo.upAxis = 1
# equals
trackTo.upAxis = KX_TRACK_UPAXIS_POS_Y
trackTo.upAxis = 2
# equals
trackTo.upAxis = KX_TRACK_UPAXIS_POS_Z

cont.activate(trackTo) triggers it
cont.deactivate(trackTo) turns it off

(alf0) #20

ok @Liebranca ill try this and see how it works