Skip to content

Commit 8ba48fd

Browse files
authored
[JS in extensions] object and resource parameters (#575)
1 parent 76985b9 commit 8ba48fd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

docs/gdevelop5/events/js-code/javascript-in-extensions/index.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ const angle = eventsFunctionContext.getArgument("Angle");
9797

9898
![](wrap-function.png)
9999

100-
For object parameters, `getObjects` must be used. It gives an `Array` of `RuntimeObject`.
100+
### Get object parameters in JavaScript
101+
102+
For object parameters, `getObjects` must be used. It gives an `Array` of `gdjs.RuntimeObject`.
101103

102104
```javascript
103105
const players = eventsFunctionContext.getObjects("Player");
@@ -107,6 +109,35 @@ for (const player of players) {
107109
}
108110
```
109111

112+
When you chose a specific type for the object in the parameter list, you need to declare the type in the code to get all the autocompletions. For instance, for sprite objects, you will add the following:
113+
114+
```javascript
115+
/** @type {Array<gdjs.SpriteRuntimeObject>} */
116+
const players = eventsFunctionContext.getObjects("Player");
117+
```
118+
You can find the list of all object types in the [GDJS documentation page for RuntimeObject](https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.RuntimeObject.html).
119+
120+
121+
### Get resources in JavaScript
122+
123+
Resource parameters are strings containing the name of the resource. You can get the resource content from some [ResourceManager](https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/interfaces/gdjs.ResourceManager.html).
124+
125+
For instance, to get an image for a 2D game you can do as follows:
126+
127+
```javascript
128+
const resourceName = eventsFunctionContext.getArgument("MyImage");
129+
runtimeScene.getGame().getImageManager().getPIXITexture(resourceName);
130+
```
131+
132+
For 3D games, you can get a texture with this other method:
133+
```javascript
134+
runtimeScene.getGame().getImageManager().getThreeTexture(resourceName);
135+
```
136+
137+
!!! warning
138+
139+
By default, resource names look like a relative path, but it should not be used as a path because it won't necessarily be a valid path (and it can be changed by users to anything).
140+
110141
### Call a library from an event-function
111142

112143
After getting the events function parameter values, you can pass them to a JavaScript function.
10.4 KB
Loading

0 commit comments

Comments
 (0)