Skip to content

Commit 2207b6d

Browse files
bors[bot]chitoyuu
andauthored
Merge #1005
1005: Add a script for launching examples properly r=chitoyuu a=chitoyuu Close #461 Co-authored-by: Chitose Yuuzaki <chitoyuu@potatoes.gay>
2 parents a7479c3 + 4bc990a commit 2207b6d

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ godot_init!(init);
113113

114114
> **Important note:**
115115
>
116-
> Before launching the examples in the Godot editor, you must first run `cargo build` and wait for the build operations to finish successfully.
117-
>
118-
>At startup, the Godot editor tries to load all resources used by the project, including the native library. If the latter isn't present, the editor will skip properties or signals associated with the missing native scripts in the scene. This causes the scene tree to be non-functional for any sample that relies on properties or signals configured in the editor.
116+
> To run or edit an example, you need to build the native library for it first. Otherwise, the project will be broken. You can do so manually with `cargo build`, or use the `example.sh` shell script for convenience: `./example.sh run hello-world` or `./example.sh edit hello-world` for the editor.
119117
120118
The [/examples](https://github.com/godot-rust/godot-rust/tree/master/examples) directory contains several ready to use examples, complete with Godot projects and setup for easy compilation from Cargo:
121119

@@ -130,6 +128,7 @@ The [/examples](https://github.com/godot-rust/godot-rust/tree/master/examples) d
130128
- [**rpc**](https://github.com/godot-rust/godot-rust/tree/master/examples/rpc) - Simple peer-to-peer networking.
131129
- [**native-plugin**](https://github.com/godot-rust/godot-rust/tree/master/examples/native-plugin) - Create custom node plugins.
132130

131+
At startup, the Godot editor tries to load all resources used by the project, including the native library. If the latter isn't present, the editor will skip properties or signals associated with the missing native scripts in the scene. This causes the scene tree to be non-functional for any sample that relies on properties or signals configured in the editor.
133132
### Third-party projects
134133

135134
To see a list of games and integrations developed on top of godot-rust, have a look at our list of [third-party projects](https://godot-rust.github.io/book/projects.html) in the book.

example.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
3+
# Small utility to compile and run examples
4+
5+
# No args specified: do everything
6+
if [ "$#" -eq 0 ]; then
7+
args=("--help")
8+
else
9+
args=("$@")
10+
fi
11+
12+
# --help menu
13+
for arg in "${args[@]}"; do
14+
if [ "$arg" == "--help" ]; then
15+
echo "Usage: example.sh <command> <example-name>"
16+
echo ""
17+
echo "Commands:"
18+
echo " run run the specified example"
19+
echo " edit open the specified example in the editor"
20+
echo ""
21+
echo "Examples:"
22+
echo " example.sh run hello-world"
23+
exit 0
24+
fi
25+
done
26+
27+
if [ "$#" -ne 2 ]; then
28+
echo "Both the command and the name of the example are required."
29+
exit 1
30+
fi
31+
32+
function findGodot() {
33+
# User-defined GODOT_BIN
34+
if [ -n "$GODOT_BIN" ]; then
35+
echo "Found GODOT_BIN env var ($GODOT_BIN)"
36+
godotBin="$GODOT_BIN"
37+
38+
# Executable in path
39+
elif command -v godot &>/dev/null; then
40+
echo "Found 'godot' executable"
41+
godotBin="godot"
42+
43+
# Special case for Windows when there is a .bat file
44+
# Also consider that 'cmd /c' would need 'cmd //c' (https://stackoverflow.com/q/21357813)
45+
elif
46+
# Godot returns 255 for older versions, but 0 for newer ones
47+
godot.bat --version
48+
[[ $? -eq 255 || $? -eq 0 ]]
49+
then
50+
echo "Found 'godot.bat' script"
51+
godotBin="godot.bat"
52+
53+
# Error case
54+
else
55+
echo "Godot executable not found"
56+
exit 2
57+
fi
58+
}
59+
60+
example_path="${BASH_SOURCE%/*}/examples/${args[1]}"
61+
62+
if ! [[ -d "$example_path" ]]; then
63+
echo "The example ${args[1]} is not found."
64+
exit 2
65+
fi
66+
67+
findGodot
68+
69+
cmds=()
70+
71+
case "${args[0]}" in
72+
run)
73+
cmds+=("cargo build --manifest-path $example_path/Cargo.toml")
74+
cmds+=("$godotBin --path $example_path")
75+
;;
76+
edit)
77+
cmds+=("cargo build --manifest-path $example_path/Cargo.toml")
78+
cmds+=("$godotBin -e --path $example_path")
79+
;;
80+
*)
81+
echo "Unrecognized command '$arg'"
82+
exit 2
83+
;;
84+
esac
85+
86+
for cmd in "${cmds[@]}"; do
87+
echo "> $cmd"
88+
$cmd || {
89+
exit 1
90+
}
91+
done

0 commit comments

Comments
 (0)