ℹ️ 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
For a shop, an inventory or a simulation game: instead of creating one variable per product, write all your products in a data table: one row per product, one column per piece of information (price, image, stock). The game then builds one card per product.
- a 2D project open in Yop2D Next and one image per product.
Step 1: the table
- Tap Object → Tools → Data tables, then Open.
- Tap + New table and name it Products.
- Tap + Add a row for each product: Apple, Bread, Milk. The first column, Name, holds the product's name.
- Tap + (at the end of the columns) to add Price, Image and Stock.
- Tap the name of the Image column and choose Kind: image. Price and Stock stay as number.
- Tap each cell to fill it (Price 2, 3, 1.5; one image per product; Stock 10, 5, 8). Then Done.
Step 2: the models
- Put an object in the scene (an image or a Square) and name it Card. In the Inspector, line Tag (family of objects), type produit.
- Tap Object → Texts → Text and name it Price.
- Place these two objects off the screen: they are the models, the game makes one copy per product.
Step 3: one card per product
- Open the Blueprint.
- On Start → For each table row: Data table Products.
- Output For each one → Create object: Object Card, X position
=numero_ligne * 300, Y position 300. - → Give a row's values (table): Object the involved object (the card just created), Data table Products, Row
=ligne_tableau. - Output Found → Create object: Object Price, X position
=numero_ligne * 300, Y position 450. - → Modify Text: Object the involved object, New text
=tableau("Products", ligne_tableau, "Price").
For each table row goes through each product, one after the other: the variable ligne_tableau holds its name (Apple, then Bread, then Milk) and numero_ligne its number (1, 2, 3). Give a row's values (table) copies the row into the card: each column becomes a variable of the card (Price → price, Stock → stock) and the image column changes its image.
Step 4: change the stock
The table keeps the starting values. During the game, you change the card's stock variable:
- On Object Click: Object # produit → Add to Variable: Object the involved object, Value to add -1.
- Line Variable: tap New variable… and create stock (Whole number). Because the node targets the card, it is the tapped card's stock that goes down.
Read a value in a formula
Anywhere you write a formula: =tableau("Products", "Apple", "Price") gives 2 (the table, the row, then the column). For example Modify Text of a text, New text =tableau("Products", "Apple", "Price").
The nodes
Try it
Tap Play: three cards appear, each with its image and price. Add a Banana row to the table: a 4th card appears, without touching the nodes.