Results 1 to 20 of 20
  1. #1
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680

    i am learning how to script

    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


    digrame.png

    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



  2. #2
    Member MichelleSea's Avatar
    Join Date
    Apr 2013
    Location
    Cananananananana (BATMAN) ada
    Posts
    587
    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. :P

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



  3. #3
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    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 ,



  4. #4
    Member Fred/K.S's Avatar
    Join Date
    Dec 2013
    Location
    The Blender universe.za
    Posts
    894
    Join us on Discord !!!!!
    Then you'll learn !!!

    Srry here's the proper link !!!
    BGE Coding
    Fred/K.S
    Last edited by Fred/K.S; 27-Mar-18 at 11:30.



  5. #5
    Member MichelleSea's Avatar
    Join Date
    Apr 2013
    Location
    Cananananananana (BATMAN) ada
    Posts
    587
    Originally Posted by alf0 View Post
    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 ,
    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:

    Code:
    def walk():
    • I don't have time right now to get into the script itself, sorry :P
    • 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.



  6. #6
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    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



  7. #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.



  8. #8
    Member Nick Manchul's Avatar
    Join Date
    Apr 2016
    Location
    Ukraine
    Posts
    395
    ... with the logic brackets , but i dont want this anymore
    - yeah man, Python (and scripting) is cool!

    Code:
    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
    Last edited by Nick Manchul; 28-Mar-18 at 03:59.
    my main Thread | my Youtube channel | my Cloud



  9. #9
    Member Cotaks's Avatar
    Join Date
    May 2012
    Location
    The Netherlands
    Posts
    1,353
    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:
    Code:
    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.
    '- It's not working' then check your console like: this
    Did you know that you can make a complete game with the .blends that i share?



  10. #10
    Member Nick Manchul's Avatar
    Join Date
    Apr 2016
    Location
    Ukraine
    Posts
    395
    Originally Posted by Cotaks View Post
    Better start using module mode, for everything, even 1 line is faster within the module mode, so your script can/will be like:
    Code:
    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.
    - 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.
    Last edited by Nick Manchul; 28-Mar-18 at 09:26.
    my main Thread | my Youtube channel | my Cloud



  11. #11
    Member Cotaks's Avatar
    Join Date
    May 2012
    Location
    The Netherlands
    Posts
    1,353
    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.
    Last edited by Cotaks; 28-Mar-18 at 12:00.
    '- It's not working' then check your console like: this
    Did you know that you can make a complete game with the .blends that i share?



  12. #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.
    Last edited by Daedalus_MDW; 28-Mar-18 at 12:19.
    System "IVAN" (rev 2.0) - Win7 64bit - Blender 2.74:
    Intel i7-3770 4.10 Ghz | EVGA GTX 970 | GSkill Ares 16GB 1600 Mhz | ASUS P8Z77-V LK
    Where my Code Lives - https://github.com/DaedalusMDW



  13. #13
    Member Nick Manchul's Avatar
    Join Date
    Apr 2016
    Location
    Ukraine
    Posts
    395
    Originally Posted by Cotaks View Post
    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.
    - It is clear, but 150 sub classess like overdone it already .... Of course, it's better to write a short code, without repetition.
    my main Thread | my Youtube channel | my Cloud



  14. #14
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    @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/bg...ect.playAction but i didnt understand a thing at all,



  15. #15
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    ok this is a new character i call her hind.


    hind post.png

    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

    https://mega.nz/#!3051nDQZ!TANB38TW_...7JCxFtOka1DMOI



  16. #16
    Member Cotaks's Avatar
    Join Date
    May 2012
    Location
    The Netherlands
    Posts
    1,353
    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
    Code:
    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)
    '- It's not working' then check your console like: this
    Did you know that you can make a complete game with the .blends that i share?



  17. #17
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    oh ok, ill try this one
    and sorry for the bad attachment above



  18. #18
    Member alf0's Avatar
    Join Date
    Nov 2012
    Location
    medal east sudan
    Posts
    680
    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

    play_mode=KX_ACTION_MODE_PLAY

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

    bge.logic. KX_TRACK_UPAXIS_POS_Y

    where can i set the target and time of the tracking

    thanks again



  19. #19
    Member Liebranca's Avatar
    Join Date
    Nov 2017
    Location
    La Tomba
    Posts
    42
    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.

    2.

    Code:
    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..

    Code:
    trackTo.upAxis = 0
    #it's the same as
    trackTo.upAxis = KX_TRACK_UPAXIS_POS_X
    
    #therefore
    
    trackTo.upAxis = 1
    # equals
    trackTo.upAxis = KX_TRACK_UPAXIS_POS_Y
    #and
    trackTo.upAxis = 2
    # equals
    trackTo.upAxis = KX_TRACK_UPAXIS_POS_Z
    cont.activate(trackTo) triggers it
    cont.deactivate(trackTo) turns it off



  20. #20



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •