You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/showscript3.md
+150-1Lines changed: 150 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -306,7 +306,120 @@ ticks(0) {
306
306
307
307
### Sharing data between shows: `export` and `load`
308
308
309
-
// TODO: add
309
+
ShowScript 3 provides a powerful mechanism to share data and functions between shows using `export` and `load`. This allows you to define reusable components in one show and use them in another, promoting code reuse and modularity. Let's go through both concepts with examples.
310
+
311
+
#### Exporting and Loading Data
312
+
313
+
Imagine you have a scenario where multiple shows need to use the same piece of data, such as the coordinates of a specific location. Instead of defining the coordinates in each show, you can define them once and export them.
In this example, `mainLocation` is exported with the name "mainLocation".
329
+
330
+
You can then load this data in another show:
331
+
332
+
```groovy
333
+
// useLocationData.groovy
334
+
def importedData = load("locationData")
335
+
336
+
ticks(0) {
337
+
cmd {
338
+
def loc = importedData.mainLocation
339
+
"teleport @a ${loc.x} ${loc.y} ${loc.z}"
340
+
}
341
+
}
342
+
```
343
+
344
+
Running `/show start useLocationData` will teleport all players to the coordinates defined in `mainLocation`.
345
+
346
+
#### Exporting and Loading Functions
347
+
348
+
Similarly, you can export functions from one show and use them in another, allowing you to create reusable components.
349
+
350
+
```groovy
351
+
// greetingFunctions.groovy
352
+
def sayHello = { name ->
353
+
return "hello ${name}"
354
+
}
355
+
356
+
export("sayHello", sayHello)
357
+
358
+
ticks(0) {
359
+
cmd {
360
+
sayHello("world")
361
+
}
362
+
}
363
+
```
364
+
365
+
In this example, the function `sayHello` is exported with the name "sayHello". When this show is run, it will broadcast "hello world".
366
+
367
+
You can then load and use this function in another show:
368
+
369
+
```groovy
370
+
// useGreetingFunction.groovy
371
+
def importedFunctions = load("greetingFunctions")
372
+
373
+
ticks(0) {
374
+
cmd {
375
+
importedFunctions.sayHello("tyler")
376
+
}
377
+
}
378
+
```
379
+
380
+
Running `/show start useGreetingFunction` will broadcast "hello tyler".
381
+
382
+
#### Combining Data and Functions
383
+
384
+
You can combine both concepts to create even more powerful and reusable components. For example, you can have a show that exports both data and functions:
In this example, both `mainLocation` and `sayHello` are exported. You can then load and use both in another show:
404
+
405
+
```groovy
406
+
// useDataAndFunctions.groovy
407
+
def imported = load("dataAndFunctions")
408
+
409
+
ticks(0) {
410
+
def loc = imported.mainLocation
411
+
cmd {
412
+
"teleport @a ${loc.x} ${loc.y} ${loc.z}"
413
+
}
414
+
cmd {
415
+
imported.sayHello("everyone")
416
+
}
417
+
}
418
+
```
419
+
420
+
Running `/show start useDataAndFunctions` will teleport all players to the coordinates defined in `mainLocation` and broadcast "hello everyone".
421
+
422
+
By using `export` and `load`, you can create modular and reusable code components in ShowScript, making it easier to maintain and extend your shows.
310
423
311
424
### Accessing server info
312
425
@@ -385,3 +498,39 @@ Get the mathematical cosine of an angle
385
498
386
499
#### `tan(double angle)` (returns double)
387
500
Get the mathematical tangent of an angle
501
+
502
+
503
+
### Accessing information from ANY plugin
504
+
505
+
The Groovy interpreter that runs ShowScript shows has access to the entire Java classpath. You can `import` any Java class you want for use in your shows, even classes from plugins you have installed.
506
+
507
+
Here’s an example of how to use TrainCarts to get information about a specific train:
0 commit comments