Sync instances of the same scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Blueberry

Hi, I have a scene which includes my inventory. It has 16 tiles where items can be placed.

On the one hand I like to use this scene to add items which the player picks up and on the other hand I like to use the same scene with all the collected items in the shop page.

So that, the user is able to click on the “sell item” button (which is invisible while the scene is the player inventory) to sell the item, and remove it from the inventory.

So the shop page and inventory should be updated allways at the same time (they should be in sync)

What’s the best way to do that?

:bust_in_silhouette: Reply From: godot_dev_

You could use a model-view-controller design, where the items the player has are stored in some internal object (array, children of a node, hash map, etc.) (the model), and then every time a inventory scene is displayed (the view), you dynamically populate the inventory slots to visually represent what’s stored in the player’s internal inventory data model (the controller).

This way both the shop and inventory don’t have to be in sync, since they are only populated when displayed. From the player’s perspective they will appear to be in sync. Even if the player’s inventory is always visible, the same design can be applied, just update the inventory UI every time the player has his/her inventory data model updated.

So if I understood your approach right, I should create 2 instances (shop =1, player inventory =2) of my inventory scene and store the inventory items globally. The if 1 or 2 is opened, I should use my global items array to show what’s inside, right?

Blueberry | 2023-01-05 16:11

Exactly. I think that should work

godot_dev_ | 2023-01-05 16:36