コンテンツにスキップ

Fixing shop system pricing issues with lists in Scratch

このコンテンツはまだ日本語訳がありません。

💡 Having trouble with shop systems and list management? Don’t know how to debug pricing issues? 🚀 Get Help Now

SS

ShopSystem_Dev

Posted on January 25, 2024 • Intermediate

🛒 Shop pricing problem - last items showing as FREE!

I’m working on a YouTube Simulator game with a shop system where players can buy upgrades like cameras and PCs. Most of the pricing works correctly, but I have a frustrating bug:

The Problem:

  • Most shop items have correct prices
  • The last few items (3D Camera and Mac OS 10.7.2) show as FREE
  • I’ve added these items to my price lists
  • The pricing logic should be the same for all items

I’m using lists to store item names and their corresponding prices, but something is going wrong with the last items. The issue seems to be in my “Shop” sprite logic.

Has anyone encountered similar list indexing issues with shop systems? What could cause the last items to not read their prices correctly? 🤔

LD

ListDebugger_Pro

Replied 45 minutes later • ⭐ Best Answer

I can help you solve this @ShopSystem_Dev! This is a classic list synchronization issue. The problem is likely that your item names list and prices list have different lengths, causing index mismatches for the last items. 🎯

🔍 Common Shop System Issues

Here’s what typically goes wrong with list-based shop systems:

flowchart TD A[Shop Item Selected] --> B[Get Item Index] B --> C{Check Lists} C --> D[Item Names List] C --> E[Item Prices List] D --> F[Length: 10 items] E --> G[Length: 8 prices] F --> H{Index > 8?} H -->|Yes| I[❌ No Price Found] H -->|No| J[✅ Price Found] I --> K[Shows as FREE] J --> L[Shows Correct Price] style A fill:#e1f5fe style I fill:#ffebee style K fill:#ffebee style J fill:#e8f5e8 style L fill:#e8f5e8

🛠️ Step 1: Debug Your Lists

First, let’s check if your lists are properly synchronized:

    // Debug script - check list lengths
when flag clicked
say (join [Items: ] (length of [Item Names v])) for [2] seconds
wait [2] seconds
say (join [Prices: ] (length of [Item Prices v])) for [2] seconds

// Check specific items
repeat (length of [Item Names v])
set [index v] to (index)
say (join (item (index) of [Item Names v]) (join [ costs ] (item (index) of [Item Prices v]))) for [1] seconds
end
  

🔧 Step 2: Proper Shop System Structure

Here’s how to build a robust shop system with synchronized lists:

    // Initialize shop system
when flag clicked
delete all of [Item Names v]
delete all of [Item Prices v]
delete all of [Item Descriptions v]

// Add items in order
add [Basic Camera] to [Item Names v]
add [100] to [Item Prices v]
add [A simple camera for beginners] to [Item Descriptions v]

add [HD Camera] to [Item Names v]
add [500] to [Item Prices v]
add [High definition recording] to [Item Descriptions v]

add [3D Camera] to [Item Names v]
add [2000] to [Item Prices v]
add [Professional 3D recording] to [Item Descriptions v]

add [Mac OS 10.7.2] to [Item Names v]
add [5000] to [Item Prices v]
add [Premium editing computer] to [Item Descriptions v]
  

🎯 Step 3: Safe Price Lookup

Implement error-checking when getting prices:

    // Safe price lookup function
define get item price (item name)
set [item index v] to [0]
repeat (length of [Item Names v])
change [item index v] by [1]
if <(item (item index) of [Item Names v]) = (item name)> then
if <(item index) ≤ (length of [Item Prices v])> then
set [current price v] to (item (item index) of [Item Prices v])
else
set [current price v] to [999999] // Error price
say [Price not found!] for [2] seconds
end
stop [this script v]
end
end
set [current price v] to [0] // Item not found
  

🚀 Step 4: Complete Shop Interface

Here’s a complete shop system with proper error handling:

    // Shop interface
when this sprite clicked
ask [Which item? (1-4)] and wait
set [selected item v] to (answer)

if <<(selected item) > [0]> and <(selected item) ≤ (length of [Item Names v])>> then
set [item name v] to (item (selected item) of [Item Names v])

// Safe price check
if <(selected item) ≤ (length of [Item Prices v])> then
set [item price v] to (item (selected item) of [Item Prices v])
else
set [item price v] to [ERROR]
say [Price data missing for this item!] for [3] seconds
stop [this script v]
end

// Purchase logic
if <(money) ≥ (item price)> then
change [money v] by (0 - (item price))
add (item name) to [Owned Items v]
say (join [Bought ] (item name)) for [2] seconds
else
say [Not enough money!] for [2] seconds
end
else
say [Invalid item number!] for [2] seconds
end
  

🔍 Step 5: Advanced Shop Features

Add these features for a professional shop system:

    // Check if item already owned
define check if owned (item name)
set [is owned v] to [false]
repeat (length of [Owned Items v])
if <(item (index) of [Owned Items v]) = (item name)> then
set [is owned v] to [true]
stop [this script v]
end
end

// Dynamic pricing (discounts, etc.)
define calculate final price (base price) (discount percent)
set [final price v] to ((base price) * ((100 - (discount percent)) / [100]))

// Shop categories
define filter items by category (category)
delete all of [Filtered Items v]
repeat (length of [Item Names v])
if <(item (index) of [Item Categories v]) = (category)> then
add (item (index) of [Item Names v]) to [Filtered Items v]
end
end
  

The key is keeping your lists synchronized and always checking bounds before accessing list items. This will fix your FREE item issue! 🛒✨

SS

ShopSystem_Dev

Replied 20 minutes later

@ListDebugger_Pro You’re absolutely right! 🎉 I checked and my Item Names list had 10 items but my Item Prices list only had 8 entries. The last two items had no corresponding prices!

The debug script helped me spot the issue immediately. Thank you so much for the comprehensive solution! 🙏

GE

GameEconomist_Maya

Replied 1 hour later

Great debugging session! 💡 For YouTube Simulator games, here are some economy balancing tips:

    // Progressive pricing formula
define calculate upgrade price (tier)
set [base price v] to [100]
set [price multiplier v] to [2.5]
set [final price v] to ((base price) * ((price multiplier) ^ (tier)))

// Tier 1: $100
// Tier 2: $250  
// Tier 3: $625
// Tier 4: $1,562
  

This creates a balanced progression where each upgrade feels meaningful! 📈

VB

Vibelf_Community

Pinned Message • Moderator

🛒 Master Advanced Shop Systems

Excellent debugging discussion! For those building complex shop and economy systems, our community can help you implement:

  • 💰 Dynamic pricing algorithms
  • 🏪 Multi-category shop systems
  • 📊 Economy balancing tools
  • 🎁 Special offers and discounts

📚 Related Shop System Topics

Ready to create sophisticated shop and economy systems? Get personalized guidance from our expert tutors in the Vibelf app!