A2A.py - the All-To-All library - 1.0

Hello all,

There is a new library in town :D. It is for BGE users that do not want to take the step into deep Python scripting. Be warned it requires a bit of Python knowledge. But I will provide examples that makes it easier for you to use. So you can use them and learn from them as well.

What is the weakness of the logic bricks?

You can only transfer TRUE/FALSE data from sensors to actuators.
In a lot of cases this is good enough, but sometimes not.
Here is the solution!
[INDENT][INDENT]The All-To-All Library
[/INDENT][/INDENT]What does A2A do?

It provides a way to

  • get any value into properties (e.g. mouse positions, object velocity)
  • use any value from properties (e.g. set object positions)
  • directly assign values (e.g. mouse position -> object position)
  • execute small python statements (e.g. setting mouse position, print statements)
    The library itself is not that large, but contains a lot of documentation. Don’t worry, you do not need to study the file, just follow the tutorial.

A2A is compatible with 2.49b and 2.5x (at least 2.53). Clarification - you need a Blender version with Python 2.6+ or Python 3.

Here you can find the full documentation including the sources. Thanks to Sunjay03 :cool: for hosting.

How to use A2A?

Connect one or more sensors of any type with a Python controller in Module mode. Choose one of the available functions.
Now connect one or more Property actuators.

You control the module by configuring the Property actuators. What field contains what depends on the controllers function.

  • fromProp(cont)
    [LIST]

  • Assigns the property from the Prop field to the expression in the Value field of the connected actuators. The property belongs to the actuator’s owner.

  • toProp(cont)

  • Assigns the expression from the Value field to the property in the Prop field of the connected actuators. The property belongs to the actuator’s owner.

  • assign(cont)

  • Assigns the expression from the Value field to the expression in the Prop field of the connected actuators.

  • execute(cont)

  • Executes the expression in the Prop field of the connected actuators.

  • The content of the Value field can be used as paramter list (use &Value in the Prop field expression)

[/LIST]The expressions use Python syntax. This allows nearly any Python code to be used. But the lengths of the actuator fields are resticted.

Following keywords are available:

  • sensor’s name for all connected sensors
  • actuator’s name for all connected actuators
  • obj for the owner of the current actuator
  • cobj for the owner of the current controller (better use obj)
  • scene for the current scene
  • RST for the Rasterizer
  • GL for the GameLogic
  • adict for a dictionary “obj.name” in globalDict (will be created if not present by “globalDict”
  • cdict for a dictionary “cobj.name” in globalDict (will be created if not present by “globalDict”
  • gdict for global dict
  • $Value the content of the Value field (execute only!) to be the parameter list (it is a string replacement)
    All BGE API operations are supported. Please, do not use deprecated syntax.

The A2A-functions support the Property actuator’s Mode flag with

  • assign (default)
  • add (+ operation)
  • others are the same as assign
    The following posts contain examples how to use the A2A-library.

How does the A2A work?

Basicly the functions take the content of the Property actuator’s fields. Assembles them to a string and execute the string as Python code.

This is the reason why it uses Python syntax. The above functions define how the string is assembled.

If you want to see the final string, add a property called “debug” with a True value (!= 0) to the controller object. The command strings will be printed to the console.

Troubleshooting

If the string produces errors following information will be printed to the console:

  • the command string
  • the error
  • the actuator’s name
    I hope it helps

Attachments

A2A.blend (303 KB)a2a.html.zip.blend (26.8 KB)

Object position to properties:



Properties on actuator’s owner: float: objX, float: objY, float: objZ
always sensor (True pulse)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: objX Value: obj.worldPosition[0]
. ->connect to Property actuator Assign Prop: objY Value: obj.worldPosition[1]
. ->connect to Property actuator Assign Prop: objZ Value: obj.worldPosition[2]

Mouse position to properties:



Properties on actuator’s owner: float: mouseX , float: mouseY
Mouse sensor Movement “sMouse” (True pulse)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: mouseX Value: float(sMouse.position[0])/100
. ->connect to Property actuator Assign Prop: mouseY Value: float(sMouse.position[1])/100

Property values to object position:



Properties on actuator’s owner: float: objX
Property sensor (True pulse) Changed Prop: objX on the actuator’s owner
.-> connect to Python module controller A2A.fromProp
. ->connect to Property actuator Assign Prop: objX Value: obj.worldPosition[0]
Always sensor (True pulse) at any object
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: objX Value:cobj.worldPosition[1]

Direct assign mouse position to object position incl. scaling the value:



Mouse sensor Movement “sMouse” (True pulse) on any object
.-> connect to Python module controller A2A.assign
. ->connect to Property actuator Assign Prop: obj.worldPosition[0]Value: float(sMouse.position[0])/100
. ->connect to Property actuator Assign Prop: obj.worldPosition[2]Value: float(sMouse.position[1])/100

Copy screensize from Rasterizer to properties:


Properties on actuator’s owner: int: windowX, int: windowY
always sensor (no pulses)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: windowX Value: RST.getWindowWidth()
. ->connect to Property actuator Assign Prop: windowY Value: RST.getWindowHeight()

Center the mouse cursor at the middle of the screen:


Properties on actuator’s owner: int: halfX, int: halfY
always sensor (no pulses)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: halfX Value: RST.getWindowWidth()/2
. ->connect to Property actuator Assign Prop: halfY Value: RST.getWindowHeight()/2
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.showMouse(1) Value: —

Mouse sensor (True pulse)
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.setMousePosition($Value) Value: obj[“halfX”],obj[“halfY”]

Move an object with the mouse relative to its current position:
Part 1 (init)


Properties on actuator’s owner: int: halfX, int: halfY
always sensor (no pulses)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: halfX Value: RST.getWindowWidth()/2
always sensor (no pulses)
.-> connect to Python module controller A2A.toProp
. ->connect to Property actuator Assign Prop: halfX Value: RST.getWindowWidth()/2
. ->connect to Property actuator Assign Prop: halfY Value: RST.getWindowHeight()/2
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.showMouse(1) Value: —

Mouse sensor (True pulse)
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.setMousePosition($Value) Value: obj[“halfX”],obj[“halfY”]

Part 2 (move):



Properties on controller’s owner: float: mX, float: mY

always sensor (no pulses)
.-> connect to Python module controller A2A.toProp
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.setMousePosition($Value) Value: obj[“halfX”],obj[“halfY”]

Mouse sensor “sMouse” Movement (True pulse)
.-> connect to Python module controller A2A.execute
. ->connect to Property actuator Assign Prop: RST.setMousePosition($Value) Value: obj[“halfX”],obj[“halfY”]
.-> connect to Python module controller A2A.assign
object holding the properties from rasterizer:
. ->connect to Property actuator Assign Prop: cobj[“mX”] Value: sMouse.position[0]-obj[“halfX”]
. ->connect to Property actuator Assign Prop: cobj[“mY”] Value: sMouse.position[0]-obj[“halfY”]
object that should be moved:
. ->connect to Property actuator Add Prop: obj.localPosition[0] Value: float(cobj[“mX”]/500)
. ->connect to Property actuator Add Prop: obj.localPosition[1] Value: float(cobj[“mY”]/500)

Here is an example how to save data in globalDict and use the game actuator to save globalDict to a file:


I hope you can read it. The GameActuator can be setup at any object, triggered at any time. Just make sure globalDict is already filled .

I try it more compact:
any sensor you like to make saving (here keyboard “S-Key”)
. -> connect to Python module controller A2A.assign
. ->connect to Property actuator Assign Prop: adict[“pos”] Value: obj.worldPosition
. -> connect to AND controller
. -> connect to Game actuator Save GameLogic.globalDict

Here is how to load the data back from file into globalDict and restore it to the object:


any sensor you like to make saving (here keyboard “L-Key”)
. -> connect to AND controller
. -> connect to Game actuator Load GameLogic.globalDict
. ->connect to Property actuator Assign Prop: loaded Value: True
Property sensor Equal Prop: loaded Value: True
. -> connect to Python module controller A2A.assign
. ->connect to Property actuator Assign Prop: obj.worldPosition Value: adict[“pos”]
. -> connect to AND controller
. ->connect to Property actuator Assign Prop: loaded Value: False

There is a special obstactle you need to know:

The A2A is performed by the controller. That means regardless which order the actuators have, the A2A is executed before any actuator is executed.

This makes it tricky with the Game actuator. We need to make sure the globalDict is loaded before we can restore values of the object.

As you see, I solved that with an intermediate property “loaded”. After loading the gloablDict from file, this property gets True.
When this proeprty is True, we know the globalDict is loaded and can restore the data from globalDict.
To ensure we can load a second time, the property needs to be reset to False again.

You can achive a similar bahaviour with states.

General, saving to/loading from disk should be performed by a separate object to separate saving from store logic. You could send out messages, to notify all objects, loading was done.

Very nice! If only this had been available when I started to work with the BGE!

This is fantastic! It looks like a hybrid game programming setup!
You are a genius Prof. Monster! I which I was your disciple!
Now I’ve got to find ways to use it as efficiently as possible.
BTW, prof., can you give more guidance? Can I for example copy an object’s orientation from one scene to another in another scene?
This is so cool! I’m really excited…

Yes it is possible, I’m preparing more examples.
If you have more ideas, what you want to see, let me know.

I planned to start with general examples as already shown and continue with more complex once (e.g. using mouse to move an object incl. cursor reset).

Have you seen that you even can access the globalDict?
This comes quite handy in conjunction with the save/load actuators.

Awesome, nice LogicPy-Fu you provide us, I’ll call you Sensei from now on!
The basic most demanded are indeed indeed mouse functions. I always wanted to get some mouse info for armature animation. If the cursor has a certain value, I rotate torso, arm and neck to a certain degree.
Also drag and drop objects with the mouse… just to mention 2!

this indeed looks very exciting, however, I can’t actually get it to do anything. Do I need to do something with the .blend before it is operable?

switch to any scene you like.

The demos do not do that much fancy things as the demonstrate the basics. Look at the debug output of the properties, how they change (You might need to switch on the debug option in game menu). Sometimey you need to move the mouse.

At least in the mouse scene the cube should move with the mouse.

If really nothing happen, have a look at the console. Maybe there are some error messages. If so let me know what they are. Please do not to forget to say what version of Blender you are using.

I am using blender 2.49, but it doesn’t work in 2.5 either.

and here is the console output

Last login: Tue Sep  7 12:30:42 on ttys000
Pearce-Michals-MacBook-Pro:~ pearcemichal$ /Applications/design\ applications/blender\ 2.49b/blender.app/Contents/MacOS/blender ; exit;
Compiled with Python version 2.5.4.
Checking for installed Python... got it!
Detected GL_ARB_texture_env_combine
Detected GL_ARB_texture_cube_map
Detected GL_ARB_multitexture
Detected GL_ARB_shader_objects
Detected GL_ARB_vertex_shader
Detected GL_ARB_fragment_shader
Detected GL_ARB_vertex_program
Detected GL_ARB_depth_texture

Blender Game Engine Started

A2A.py:134: Warning: 'as' will become a reserved keyword in Python 2.6
  File "A2A.py", line 134
    except Exception as error:
                      ^
SyntaxError: invalid syntax
Python module not found - controller "A2A.toProp#CONTR#2":
SystemError: NULL result without error in PyObject_Call
A2A.py:134: Warning: 'as' will become a reserved keyword in Python 2.6
  File "A2A.py", line 134
    except Exception as error:
                      ^
SyntaxError: invalid syntax
Python module not found - controller "A2A.execute#CONTR#3":
SystemError: NULL result without error in PyObject_Call
A2A.py:134: Warning: 'as' will become a reserved keyword in Python 2.6
  File "A2A.py", line 134
    except Exception as error:
                      ^
SyntaxError: invalid syntax
Python module not found - controller "A2A.execute#CONTR#4":
SystemError: NULL result without error in PyObject_Call
A2A.py:134: Warning: 'as' will become a reserved keyword in Python 2.6
  File "A2A.py", line 134
    except Exception as error:
                      ^
SyntaxError: invalid syntax
Python module not found - controller "A2A.assign#CONTR#5":
SystemError: NULL result without error in PyObject_Call

Blender Game Engine Finished


You get these errors when using 2.49a, use 2.49b for it to work. You need Python 2.6 installed!

Yoda Almighty, it seems your Vlender 2.49b version is compiled with Python 2.5. It looks like a syntax problem between Python 2.5, 2.6 and 3.x.

There are two options for you:

  1. install Python 2.6 or 2.7
  2. replace in A2A.py at line 134 “as” with “,” from
except Exception as error:

to

except Exception, error:

but that makes it incompatible to 2.5x

if someone knows another solution please post

If you looked at the source and though …whoa, that is large… this is because of the included documentation. You can find a link to the compiled epydoc at the first post. It provides you with the source as well.
Thanks to sunjay03 for hosting

I made the examples a little bit more compact. I hope you like it.