rob-revisited/extensions/externals/doc/tutorial.md

44 lines
1.2 KiB
Markdown

# Externals: tutorial
## Step 1: require it
```bash
bin/jumphp composer mygame require jumphp/externals
```
## Step 2: a local folder while you develop
Put a few files in `games/mygame/data/` and map them in `config/project.yaml`:
```yaml
externals:
local: { "mygame/data": "data" }
```
## Step 3: list them in a scene
```php
public function __construct(private \JumPHP\Externals\Externals $externals) {}
public function enter(array $params = []): void
{
$this->externals->list('mygame/data')->then(fn (array $entries) => $this->files = $entries);
}
```
Draw `$entry->name()` and `$entry->size` in `render()`: the files appear.
## Step 4: read a part of a file
```php
$this->externals->open('mygame/data/level1.bin')->then(
fn ($file) => $file->read(0, 16)->then(fn (string $bytes) => $this->header = $bytes));
```
Only the block holding those 16 bytes is read.
## Step 5: on the playground
Push the game (without the data: keep `data/` out of git). Send the files to the server's `externals/mygame/data/`
and ask the administrator to share `mygame/data` with your project. The same code reads them from the server, by
blocks, with a cache: nothing else to change.