← Yop2D

How do I create and use a variable?

Beginner guide · no code

Français · English · Español · Deutsch · Italiano · Português · Русский · 中文 · 日本語 · العربية

ℹ️ For Yop2D Next, the new 2D + 3D version of Yop2D (coming soon). Button and node names are those of Yop2D Next.

🔍 How do I…? All short recipes, with a search

A variable is a box with a name that keeps a number or a text during the game: the score, the lives, the money, the player's name.

You need:

Step 1: create the variable

  1. In the RESOURCES panel, GAME VARIABLES part, tap New variable.
  2. Name (no spaces): for example score. The name starts with a letter, with no space or sign.
  3. Type: Whole number (score, lives), Decimal number (speed, time), Text (a name), Yes / No (a door open or closed), Inventory list (the objects picked up).
  4. Starting value: for example 0 for the score, 3 for the lives. Then Apply.
👉 You can also create the variable while setting a node: New variable…, at the bottom of the list of variables.

Step 2: change it

Step 3: show it

Place a text object, then use Modify Text with =score. The = sign is required: without it, the text shows the word “score”.

Step 4: test it

The nodes

Example: the hero picks up a coin, the score goes up by 1 and is shown.

If object touches TagHero · piece
→
Add to Variablescore · 1
→
Modify TextScoreText · =score

Try it

Tap Play and pick up coins: the number goes up.

👉 “Restart the scene” does not set the variables back to their starting value: add “Modify Variable” just before it (see My game doesn't work).