Skip to content

Commit

Permalink
docs(py): add definition of end_program_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Apr 3, 2024
1 parent bce1b05 commit 07cf514
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion content/python/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Library supported callback functions.
## Scene callbacks

### on_msg_callback
This is called whenever there is a new message sent to the client. Use this whenever you want to sniff out __all__ incoming messages.

This is called whenever there is a new message sent to the client. Use this whenever you want to sniff out **all** incoming messages.

#### Usage:

```python
# [scene] is the Scene that called the callback
# [obj] will be an Object instance
Expand All @@ -31,11 +33,13 @@ scene.on_msg_callback = on_msg_callback
```

### new_obj_callback

This is called whenever there is a new object that has been created in the scene,
one that the user does not have a reference to. Use this to make references to any
new objects that may appear during a programs lifetime. Also a good way to find camera ID's.

#### Usage:

```python
# [scene] is the Scene that called the callback
# [obj] will be an Object instance
Expand All @@ -52,12 +56,14 @@ scene.new_obj_callback = new_obj_callback
```

### delete_obj_callback

This is called whenever there is an object has been deleted in the scene.
arena-py will look for all "action" = "delete" messages and call this callback.
Use this to delete references and to be notified when an object is removed by
another user or program.

#### Usage:

```python
# [scene] is the Scene that called the callback
# [obj] will be an Object instance
Expand All @@ -73,13 +79,35 @@ def delete_obj_callback(scene, obj, msg):
scene.delete_obj_callback = delete_obj_callback
```

### end_program_callback

This is called whenever the program is ending from the SIGINT, Crtl-C, or other kill process.
Use this to cleanup resources you don't want in the scene when the program ends.
This is especially useful for persisted objects use created that you want to be removed.

#### Usage:

```python
# [scene] is the Scene that called the callback
def end_program_callback(scene):
## do stuff with scene root objects here
scene.delete_object(appParentObject)
scene.delete_object(otherAppParentObject)
print("App Terminated.")
# etc.

scene.end_program_callback = end_program_callback
```

## User callbacks

### user_join_callback

This is called whenever the library detects/finds a new user that it hasn't seen before in a scene.
Note: this is not neccesarily called when a user "joins" a scene, rather, it is called when the library first sees a `Camera` object/receives an "update" message from a user.

#### Usage:

```python
def user_join_callback(camera):
## Get access to user state
Expand All @@ -94,9 +122,11 @@ scene.user_join_callback = user_join_callback
```

### user_left_callback

This is called whenever a user leaves a scene/sends a delete message.

#### Usage:

```python
def user_left_callback(camera):
## Get access to user state
Expand All @@ -111,7 +141,9 @@ scene.user_left_callback = user_left_callback
```

### Adding callbacks when you instantiate the class

You can also add callbacks like so:

```python
from arena import *

Expand All @@ -128,7 +160,9 @@ scene = Scene(..., on_msg_callback=on_msg_callback, new_obj_callback=new_obj_cal
```

## Custom Message Callbacks

If you need to use an MQTT client, the `Scene` object exposes a way to use its MQTT client to subscribe to custom topics.

```python
def led_toggle(client, userdata, msg):
# do stuff here
Expand Down

0 comments on commit 07cf514

Please sign in to comment.