Skip to content

Commit 2b82141

Browse files
committed
Document instantiation modes
1 parent e182605 commit 2b82141

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

docs/godot_intro/sandbox.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ You can also reach Sandbox properties from the node.
8686

8787
:::warning
8888

89-
It is very tempting to assign an ELF program as the script to a Sandbox node. Don't do that. Calls you make to the Sandbox node will not go to the script first. The empty Sandbox will fail on random things and just appear confusing. Just make a regular Node and assign the ELF to that. You can use a Sandbox node, but don't set an ELF program as the script.
89+
It is very tempting to assign an ELF program as the script to a regular Sandbox node. Don't do that. Calls you make to the Sandbox node will not go through the script instance. A script instance has a loaded Sandbox inside it, but the Sandbox node won't _unless you load it_, and then you have 2 instances, one of which you aren't using. An empty Sandbox will fail on random things and just appear confusing. Make a regular Node and assign the ELF to that.
9090

9191
:::
9292

@@ -166,6 +166,56 @@ You can also load an ELF resource and use its content as a program. And, finally
166166
If you want to create a program shared with many nodes, use `set_script(elf_resource)` which sets the ELF resource directly and references it. This method enables sandboxed properties.
167167

168168

169+
## Sandbox creation summary
170+
171+
So, let's summarize the different methods:
172+
173+
1. Setting an ELF program as a script
174+
175+
The script method allows us to use other nodes, eg. a Node2D. Godot will call regular functions on the script, and so you can implement eg, `_process` in the program. Properties are exposed etc.
176+
177+
```py
178+
var n = Node.new()
179+
n.set_script(Sandbox_HelloWorld)
180+
n.hello_world() # Public API methods work
181+
```
182+
183+
2. Add a new Sandbox_HelloWorld using _Add Child Node_ in the editor
184+
185+
You should see the different types of available Sandbox-derivatives in the list under Sandbox. This is the most useful (and weirdest) form of Sandbox. It's automatically created from each ELF seen. It will give you auto-completion and generally easy access through GDScript.
186+
187+
Add a regular Node first and then add the custom sandbox under it. Then attach a GDScript to the node.
188+
189+
```py
190+
var h = get_node("Sandbox_HelloWorld") as Sandbox_HelloWorld
191+
h.hello_world()
192+
```
193+
194+
3. Setting an ELF program on a Sandbox node
195+
196+
Here you won't get auto-completion but you can still call any method as before.
197+
198+
```py
199+
var s = Sandbox.new()
200+
s.set_program(Sandbox_HelloWorld)
201+
#s.hello_world() # Public API methods DON'T work
202+
s.vmcall("hello_world") # VMCall the function instead
203+
```
204+
205+
4. Loading a program as a buffer on a Sandbox node
206+
207+
Same as above, you won't get auto-completion but you can still call any method as before.
208+
209+
```py
210+
var s = Sandbox.new()
211+
s.load_buffer(buffer)
212+
#s.hello_world() # Public API methods DON'T work
213+
s.vmcall("hello_world") # VMCall the function instead
214+
```
215+
216+
The last two methods are mostly intended for UGC and modding.
217+
218+
169219
## Sandbox API reference
170220

171221
The public API of the Sandbox node, which [originates from here](https://github.com/libriscv/godot-sandbox/blob/main/src/sandbox.h).
@@ -431,5 +481,10 @@ class Sandbox : public Node {
431481
/// @param use_argument_names If true, use argument names with default values in the generated API. Increases the size of the generated API and the compilation time.
432482
/// @return The generated API code as a string.
433483
static String generate_api(String language = "cpp", String header_extra = "", bool use_argument_names = false);
484+
485+
/// @brief Download a named program from the Godot Sandbox programs repository.
486+
/// @param program_name The name of the program to download. Must be a program built in the Godot Sandbox programs repository.
487+
/// @return The downloaded program as a byte array.
488+
static PackedByteArray download_program(const String &program_name);
434489
};
435490
```

0 commit comments

Comments
 (0)