Logic Bricks to Pyhton

Hello members of the forum. I’m relative new to BGE, and totally neophyte to Python. Already read some about introduction to programming and to Python.

My difficulty now starts with the Blender API, and how to make what I did with logic bricks with Python. If any one can help me with this, will be a first step to understand better how to call objects, properties, sensors, etc with Python under Blender. (I read the API on web, but felt lost).

What I would like to do by code is:


if
prop (bomb_num > 0) # number of bombs available are greater than 0
and
collision (ground) # player is colliding with ‘ground’ property (of ground), means player not in the air
and
keyboard (space_key) # send possitive pulse when SPACE is pressed
and

prop (bo_type_a = true) # bo_type_a = true (they are 3 type of bomb, you can only have one active)
then
add object (bomb_a) # spawn the object named bomb_a

if
prop (bomb_num > 0)
and
collision (ground)
and
keyboard (space_key)
and

prop (bo_type_b = true)
then
add object (bomb_b)

if
prop (bomb_num > 0)
and
collision (ground)
and
keyboard (space_key)
and

prop (bo_type_c = true)
then
add object (bomb_c)


As you can see, they are parts repeat same conditions (bomb_num>0 ; collide with ground ; press SPACE) and only change what kind of bomb you have… but making in a code would be no sense to repeat the same part many times…

Could be this made with a list, tuple or bucle for? what you need to put at start to import the sensors and objects? (bombs objects are in an inactive layer).

Really wish someone can help me, cause tried couple times but can’t make it work, so guess my code is no sense for Python, hehe. (shame on me).

What I wanna do in a pic (cause an image speaks more than a thousand words). Thanks for any help. :frowning:

Attachments


First of all, instead of having three different Boolean properties, I would have one single String property and assign words to it to identify the bombs that way. It saves on space and makes things less cluttery.

Second, here’s a script I wrote that should do this for you. It assumes that you’ve taken my above advice and went with a single string property called “bomb_type”. NOTE: This will only work if your player has the Character physics type.

All you should have connected to your Python controller is that Keyboard sensor and one of your Add Object sensors (renamed to “throw_bomb”). Naming your logic bricks is important in Python.



cont = bge.logic.getCurrentController()
own = cont.owner
obl = bge.logic.getCurrentScene().objects
sen = cont.sensors
act = cont.actuators
char = bge.constraints.getCharacter(own)


def main():
    
     if sen['space_key'].positive and own['bomb_num'] > 0 and char.onGround:
          if own['bomb_type'] == "a":
               act['throw_bomb'].object = "Name_Of_Bomb_Object_A_Goes_Here"
          elif own['bomb_type'] == "b":
               act['throw_bomb'].object = "Name_Of_Bomb_Object_B_Goes_Here"
          elif own['bomb_type'] == "c":
               act['throw_bomb'].object = "Name_Of_Bomb_Object_C_Goes_Here"
          cont.activate(act['throw_bomb']) #This activates your throw actuator, adding a bomb to the scene.
          own['bomb_num'] -= 1 #This will deplete 1 from your remaining bomb count
    
main()




Hi, thanks ‘MichelleSea’ for your help. I have some questions :frowning:

sen[‘space_key’].positive

this “.positive” is the default way to tell blender to send positive pulse if sensor gets true once?

char.onGround

char makes reference to character physics objects (mean, if have more than one character independant of name?) “.on” means collide? (example, if object X (char physic) collide with Y, would be “X.onY”?)

So
‘cont’ will indicate we in the current controller (the python one) ;
‘own’ is for the object properties (bomb_num & bomb_type) ;
‘obl’ will get all object from current scene (even in inactive layers) ;
‘sen’ is for all sensors ONLY connected to controller ; and
‘act’ is for all actuators ONLY connected to controller… right?

Please tell me if something I said is wrong, so will be able understand 100% the code you shared.

Thanks a lot and sorry for asking too much. :eek:

It’s a bit pointless to just post a script that does a very specific gameplay mechanic on request.
I’d suggest going straight to learning python instead because even though logic bricks can be “mastered” in a day or two, you’ll realise how much clutter it takes if you “attempted” building a rather simple system.
Anyway here’s a very basic python script that contains the basic elements you’d use.


scene = bge.logic.getCurrentScene() #getting the current scene
objects= scene.objects #A list of the objects in the scene

myObject = objects["Object"] #Getting an object, named "Object" in this example
property = myObject["prop"] #getting the property named "prop" in myObject, which we referenced above.

Also you can use scene.addObject(“toBeAdded”, hostObject) where hostObject can be any object, so that you don’t have to create an actuator which can cause clutter.

Edit: I made this post with the page opened for about 30 minutes so didn’t see your reply.
Anyway, own doesn’t mean properties, it means the object carrying the python script. And no, obl doesn’t get objects in inactive layers. The rest is all a yes.

Hi Jackii, thanks for the explanation. What shared MichelleSea works pretty well.

As I said, I’m not a programmer, but learning Python. The Blender API is other stuff (what Michelle shared helps me a lot to understand various other things appart from being a specific gameplay mechanic :slight_smile: ). I noticed ‘obl’ is the object, not the properties of this… I did the correction some time ago, just looks it take some time to appear. :stuck_out_tongue:

I would have then one last question, to finish with this topic… You said ‘obl’ for objects in scene but no for inactive layers… then, if will spawn ‘bombs’ (that are in inactive layer of the scene) how would I refer to them so can spawn them?

Edit: scene.addObject(“toBeAdded”, hostObject) will work for any (even not active) object? Thanks.

Yes, that’s why it uses a string instead of a referenced object from scene objects list.

Got it. Thanks a lot for all your help Michelle & Jackii. Greetings.