# AI asset generation for PlayDrop games

Generate images, music, sound, speech, video and 3D for a game in PlayDrop, from the browser with player confirmation or from a server paid by the creator.

Source: https://www.playdrop.ai/create/learn/ai-assets

PlayDrop can generate images, music, sound effects, speech, video and 3D models. Use it while making a game, or inside a game so players get generated content.

## While making the game

Signed-in creators generate assets from Create on the PlayDrop website (image, music, sound, speech, video and 3D) or ask PlayDrop Cloud while building a game. Each generation costs credits, shown before it starts, and the result becomes an asset you own. Use it like any other asset: download it into your package or reference it in `uses` ([game assets](https://www.playdrop.ai/create/learn/game-assets)).

## Inside a game: from the browser

`sdk.ai.<kind>.createTask(request)` asks PlayDrop to generate media for the current player. PlayDrop shows the player a confirmation with the credit cost, the player pays, and the result is private to them.

```js
try {
  const task = await sdk.ai.image.createTask({ input: "A friendly robot pet, flat colors", aspectRatio: "1:1" });
  const done = await waitForTask(task.id);
  const blob = await sdk.ai.tasks.downloadFile(done.id);
  showPet(URL.createObjectURL(blob));
} catch (error) {
  if (error.message === "ai_generation_cancelled") return;
  throw error;
}

async function waitForTask(id) {
  for (;;) {
    const task = await sdk.ai.tasks.get(id);
    if (task.status === "DONE") return task;
    if (["FAILED", "CANCELLED", "EXPIRED"].includes(task.status)) throw new Error(task.status);
    await new Promise((resolve) => setTimeout(resolve, 2000));
  }
}
```

- Kinds: `image`, `music`, `sfx`, `speech`, `video` and `model3d`. `sdk.ai.inputs.upload(blob)` sends a reference image.
- Only one confirmation can be open at a time; a second call rejects with `ai_generation_in_progress`.
- Text and JSON generation from the browser is not available to hosted games and returns `app_ai_generation_disabled`. Use a server.
- Call it from a player action, during `play`, and handle cancellation.

## Inside a game: from a server

On a [game server](https://www.playdrop.ai/create/learn/game-servers), `playdrop.ai` from `@playdrop/server` generates text, JSON, images, music, sound effects, speech, video and 3D. The creator pays, and owns the results by default; pass `{ owner: client.auth }` to give an eligible result to that player.

```js
import { playdrop } from "@playdrop/server";

const task = await playdrop.ai.speech.createTask({ input: "Welcome back, captain." }, { owner: client.auth });
// Later, once the task is DONE:
const file = await playdrop.ai.tasks.downloadFile(task.id, { role: "primary" });
client.send("speech", { bytes: new Uint8Array(await file.arrayBuffer()), contentType: file.type });
```

- A generated file is private: download it on the server and send the bytes only to the intended player.
- Speech requests accept at most 40 words.
- Test players' generations stay owned by the creator and run in test mode.
- These calls work on uploaded games, not in local development.

Generation costs credits, so limit how often players can trigger it, and prove one complete request, from task to the player actually seeing or hearing it, before building a game around it.

## Related guides

- [Game assets](https://www.playdrop.ai/create/learn/game-assets): Ship images, sound, video and 3D with a PlayDrop game: bundled files, game-owned assets, public assets and packs, and runtime lookup with sdk.assets.
- [Realtime game servers](https://www.playdrop.ai/create/learn/game-servers): Run a hosted Colyseus 0.17 server with a MongoDB database for your game, connect from the browser with the SDK, test with two players and know the limits.
- [Monetization](https://www.playdrop.ai/create/learn/monetization): Ask for reviews with requestReview, receive tips, sell items with sdk.shop, show interstitial and rewarded ads, and see what share of each the creator earns.
