Building a highscore table system
このコンテンツはまだ日本語訳がありません。
💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now
ScoreKeeper_Pro
Posted on January 23, 2024 • Advanced
🏆 Need help with highscore table system
Hey everyone! I’m working on a comprehensive highscore table for my game and need help with the logic. My system stores data in cloud variables with this format:
I need to figure out how to:
- Check if a new score beats any existing scores
- Insert the new score in the correct position
- Shift other scores down properly
- Extract individual scores from the encoded data
I already have the encode/decode working, but the comparison and insertion logic is tricky! 🤔
LeaderboardMaster
Replied 2 hours later • ⭐ Best Answer
Excellent question @ScoreKeeper_Pro! Highscore tables are complex but very rewarding to implement. Here’s a complete solution for your leaderboard system:
🏆 Highscore System Flow
Here’s how the complete leaderboard system works:
🔧 Step 1: Data Structure Setup
First, set up your lists and variables:
when flag clicked // Create lists for leaderboard data delete all of [Leaderboard Names v] delete all of [Leaderboard Scores v] delete all of [Leaderboard Places v] // Load data from cloud variable set [leaderboard data v] to (☁ HighscoreTable) parse leaderboard data
📊 Step 2: Parse Leaderboard Data
Extract individual scores from the encoded string:
define parse leaderboard data set [current position v] to [1] set [data position v] to [1] repeat [5] // Top 5 scores // Extract place number set [place v] to [] repeat until <(letter [data position] of (leaderboard data)) = [|]> set [place v] to (join (place) (letter [data position] of (leaderboard data))) change [data position v] by [1] end change [data position v] by [1] // Skip the | // Extract username set [username v] to [] repeat until <(letter [data position] of (leaderboard data)) = [|]> set [username v] to (join (username) (letter [data position] of (leaderboard data))) change [data position v] by [1] end change [data position v] by [1] // Skip the | // Extract score set [score v] to [] repeat until <<(letter [data position] of (leaderboard data)) = [;]> or <(data position) > (length of (leaderboard data))>> set [score v] to (join (score) (letter [data position] of (leaderboard data))) change [data position v] by [1] end change [data position v] by [1] // Skip the ; // Add to lists add (place) to [Leaderboard Places v] add (username) to [Leaderboard Names v] add (score) to [Leaderboard Scores v] change [current position v] by [1] end
🎯 Step 3: Score Comparison System
Check if the new score qualifies for the leaderboard:
define check new score (player name) (new score) set [insertion position v] to [0] set [i v] to [1] repeat (length of [Leaderboard Scores v]) if <(new score) > (item [i] of [Leaderboard Scores v])> then set [insertion position v] to (i) stop [this script v] end change [i v] by [1] end // Check if it qualifies for 6th place (if leaderboard not full) if <<(length of [Leaderboard Scores v]) < [5]> and <(insertion position) = [0]>> then set [insertion position v] to ((length of [Leaderboard Scores v]) + [1]) end if <(insertion position) > [0]> then insert new score (player name) (new score) (insertion position) else say [Sorry, your score didn't make the leaderboard!] for [3] seconds end
📝 Step 4: Score Insertion Logic
Insert the new score and shift others down:
define insert new score (player name) (new score) (position) // Shift scores down from the bottom set [i v] to [5] repeat until <(i) < (position)> if <(i) ≤ (length of [Leaderboard Scores v])> then if <(i) = [5]> then // Remove the 5th place score delete [5] of [Leaderboard Places v] delete [5] of [Leaderboard Names v] delete [5] of [Leaderboard Scores v] else // Shift score down one position replace item ((i) + [1]) of [Leaderboard Places v] with (item [i] of [Leaderboard Places v]) replace item ((i) + [1]) of [Leaderboard Names v] with (item [i] of [Leaderboard Names v]) replace item ((i) + [1]) of [Leaderboard Scores v] with (item [i] of [Leaderboard Scores v]) end end change [i v] by [-1] end // Insert the new score if <(position) ≤ (length of [Leaderboard Scores v])> then replace item [position] of [Leaderboard Places v] with (position) replace item [position] of [Leaderboard Names v] with (player name) replace item [position] of [Leaderboard Scores v] with (new score) else add (position) to [Leaderboard Places v] add (player name) to [Leaderboard Names v] add (new score) to [Leaderboard Scores v] end // Update the cloud variable encode and save leaderboard
💾 Step 5: Encode and Save Data
Convert the lists back to cloud variable format:
define encode and save leaderboard set [encoded data v] to [] set [i v] to [1] repeat (length of [Leaderboard Scores v]) set [encoded data v] to (join (encoded data) (join (item [i] of [Leaderboard Places v]) [|])) set [encoded data v] to (join (encoded data) (join (item [i] of [Leaderboard Names v]) [|])) set [encoded data v] to (join (encoded data) (join (item [i] of [Leaderboard Scores v]) [;])) change [i v] by [1] end set [☁ HighscoreTable v] to (encoded data)
🎮 Step 6: Main Game Integration
Use the system in your game:
// At game end when I receive [game over v] ask [Enter your name for the leaderboard:] and wait check new score (answer) (Score) display leaderboard
📺 Step 7: Display System
Show the leaderboard to players:
define display leaderboard clear set [display text v] to [🏆 HIGH SCORES 🏆\n\n] set [i v] to [1] repeat (length of [Leaderboard Scores v]) set [rank emoji v] to [📍] if <(i) = [1]> then set [rank emoji v] to [🥇] else if <(i) = [2]> then set [rank emoji v] to [🥈] else if <(i) = [3]> then set [rank emoji v] to [🥉] end end end set [display text v] to (join (display text) (join (rank emoji) (join [ ] (join (item [i] of [Leaderboard Names v]) (join [ - ] (join (item [i] of [Leaderboard Scores v]) [\n])))))) change [i v] by [1] end say (display text) for [10] seconds
This system handles all the complex logic of comparing scores, inserting them in the right position, and maintaining a proper leaderboard! 🏆
ScoreKeeper_Pro
Replied 1 hour later
@LeaderboardMaster This is absolutely incredible! Thank you so much! 🎉
The parsing logic is exactly what I needed. One quick question - how do I handle cases where the cloud variable might be empty initially?
CloudVariable_Expert
Replied 30 minutes later
@ScoreKeeper_Pro Great question! Here’s how to handle empty cloud variables:
define initialize leaderboard if <(length of (☁ HighscoreTable)) = [0]> then // Create default leaderboard set [☁ HighscoreTable v] to [1|CPU|1000;2|CPU|800;3|CPU|600;4|CPU|400;5|CPU|200;] end parse leaderboard data
Also add error checking for corrupted data:
define safe parse leaderboard data if <(☁ HighscoreTable) contains [|]> then parse leaderboard data else // Data is corrupted, reset to defaults initialize leaderboard end
This prevents crashes and ensures your leaderboard always works! 🛡️
Vibelf_Community
Pinned Message • Moderator
🏆 Want to Master Advanced Leaderboard Systems?
Fantastic discussion on highscore tables! For those looking to create even more sophisticated leaderboard systems, our community can help you implement:
- 🌍 Global vs. local leaderboards
- 📊 Multiple scoring categories
- 🔒 Anti-cheat protection systems
- ⏰ Time-based leaderboard resets
📚 Related Discussions
- How to prevent leaderboard cheating?
- Creating tournament bracket systems
- Building achievement tracking systems
Ready to create professional-grade leaderboard systems? Get personalized guidance from our expert tutors in the Vibelf app!