Skip to content

Commit 57f2bf3

Browse files
committed
Update README with examples
1 parent 9135bf8 commit 57f2bf3

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,75 @@ A Python module implemented in Rust for serializing and deserializing RLBot's fl
66

77
To provide a fast, safe, and easy to use Python module for serializing and deserializing RLBot's flatbuffers.
88

9-
A majority of the code is generated in the `build.rs` upon first compile and thrown into `src/`.
9+
A majority of the code is generated in the `build.rs` upon first compile and thrown into `src/python`.
1010

11-
This includes the code generated by `flac`, the Python wrapper binds to the generated Rust code, and the Python type hints.
11+
This includes the code generated by `flatc` (living in `src/generated`), the Python wrapper binds to the generated Rust code, and the Python type hints (`rlbot_flatbuffers.pyi`).
12+
13+
### Basic usage
14+
15+
All classes and methods should have types hints readable by your IDE, removing the guesswork of common operations.
16+
17+
#### Creating
18+
19+
```python
20+
import rlbot_flatbuffers as flat
21+
22+
desired_ball = flat.DesiredBallState(
23+
physics=flat.Physics(
24+
location=flat.Vector3Partial(z=200),
25+
velocity=flat.Vector3Partial(x=1500, y=1500),
26+
angular_velocity=flat.Vector3Partial(),
27+
),
28+
)
29+
30+
desired_game_info = flat.DesiredGameInfoState(
31+
world_gravity_z=-100,
32+
game_speed=2,
33+
)
34+
35+
desired_game_state = flat.DesiredGameState(
36+
ball_state=desired_ball,
37+
game_info_state=desired_game_info,
38+
)
39+
```
40+
41+
In the above code, we:
42+
43+
- Set the ball to:
44+
- Location (0, 0, 200)
45+
- Velocity (1500, 1500, 0)
46+
- Angular velocity of (0, 0, 0)
47+
- Don't set the car states
48+
- Set the game info state:
49+
- World gravity to -100
50+
- Game speed to 2x default
51+
- Don't set end match or paused
52+
- Don't set any console commands
53+
54+
All values are optional when creating a class and have the proper defaults.
55+
56+
#### Reading values
57+
58+
```python
59+
def handle_packet(packet: flat.GameTickPacket):
60+
if packet.game_info.game_state_type not in {
61+
flat.GameStateType.Active,
62+
flat.GameStateType.Kickoff,
63+
}:
64+
# Return early if the game isn't active
65+
return
66+
67+
# Print the ball's location
68+
print(packet.ball.physics.location)
69+
70+
for car in packet.players:
71+
# Print the every car's location
72+
print(car.physics.location)
73+
```
74+
75+
The goal of the above was to feel familiar to RLBot v4 while providing a more Pythonic interface.
76+
77+
- All enum types are hashable and can be used in a set, allowing for easy checks against them.
78+
- Every class implements `__str__`, `__repr__`, and `__hash__` methods.
79+
- All enums also implement `__int__` and `__richcmp__`.
80+
- Lists no longer have `num_x` fields accompanying them, they are just Python lists of the appropriate length.

build.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,16 +1364,18 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
13641364
}
13651365
}
13661366

1367-
file_contents.push(Cow::Borrowed(""));
1368-
1369-
if is_enum {
1367+
if types.is_empty() {
1368+
file_contents.push(Cow::Borrowed(" def __init__(self): ..."));
1369+
} else if is_enum {
1370+
file_contents.push(Cow::Borrowed(""));
13701371
file_contents.push(Cow::Borrowed(" def __init__(self, value: int = 0):"));
13711372
file_contents.push(Cow::Borrowed(" \"\"\""));
13721373
file_contents.push(Cow::Borrowed(
13731374
" :raises ValueError: If the `value` is not a valid enum value",
13741375
));
1375-
file_contents.push(Cow::Borrowed(" \"\"\""));
1376+
file_contents.push(Cow::Borrowed(" \"\"\"\n"));
13761377
} else {
1378+
file_contents.push(Cow::Borrowed(""));
13771379
file_contents.push(Cow::Borrowed(" def __init__("));
13781380
file_contents.push(Cow::Borrowed(" self,"));
13791381

@@ -1407,7 +1409,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
14071409
};
14081410

14091411
let python_type = &python_types[i];
1410-
file_contents.push(Cow::Owned(format!(" {variable_name}: {python_type}={default_value},")));
1412+
file_contents.push(Cow::Owned(format!(" {variable_name}: {python_type} = {default_value},")));
14111413

14121414
i += 1;
14131415
}

pytest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __add__(self, other):
2121
print()
2222

2323
dgs = DesiredGameState(
24-
game_info_state=DesiredGameInfoState(game_speed=Float(1), end_match=Bool())
24+
game_info_state=DesiredGameInfoState(game_speed=2, end_match=Bool())
2525
)
2626
dgs.game_info_state.world_gravity_z = Float(-650)
2727
dgs.game_info_state.end_match.val = True

0 commit comments

Comments
 (0)