Really, really stuck making a live-play game

Hey!
I’m making a Harry Potter quiz app, in which players can play against each other on different devices. So I’ve got a list viewer, and every time the score of a player changes, I want the list-viewer to arrange the scores (in the format player name: score), so that the player with the bigger score is on the top. Each player should be able to see both the names on the list-viewer.
I’m getting stuck- can you please suggest a way that I can do this?
Help would be really appreciated.
Thanks!

It should definitely work to update two different rows in Airtable (player 1 score, player 2 score) and sort the list of those two values in Thunkable and then attach the list value to the list viewer’s text items. Then you’d need to refresh the list viewer.

Which part do you need help with?

1 Like


That’s my code. I am supposed to save the scores and player names in the RTDB.
This is what I want to happen:
Player answers question
Score gets updated
Score shows on Rank-Board
Rank-Board should have the current leader on the top, and one with least points at the bottom
Rank-Board should delete previous accounts of players.
I don’t know how to do it from the 3d point to the last.

If you’re saving the values to a realtime database, then you don’t need to have an event for DataChanged. You just sort the two values whenever you change them (for example, when you set player1score to player1score + 50). And I wouldn’t even bother sorting the database itself… I would just sort the values for displaying.

I don’t have a lot of experience with Realtime databases but let’s say you have two cloud variables called player1score and player2score. Whenever you increase/decrease either value do something like this:

…I was going to show you how to sort a list but I’m realizing as I write this there’s a simpler way…

Oops… just reading that you want multiple players’ scores, not just two. Nevermind, you’ll have to sort a list.

1 Like

Maybe this will help: How to sort Firebase values for a Leaderboard

1 Like

Thanks a lot! Really appreciate this. No problem, players is good by me. One problem is that I have not asked the players to choose whether they are player1 or player2. This quiz isn’t asking player1 to answer the first question, and then only player2 to answer the second question. Both players can answer the same question. I really liked your way, though, so I guess I’ll just ask them to select their player number.

1 Like

@codeswept,

I would take a slightly different approach that my esteemed Thunker @tatiang. Games are often good candidates for object oriented-ish data models. I would use a JSON Object Hierarchy such as

QuizGame → Games (List) ->Game (Object) → Players (list) → Player (Object) ->Score (property).
To add a game, I would do something like the blocks below. First, chek to make sure there is at least one game (which is mostly for the testing/development phase). Then add a new game to the list of games, retaining the RFDB key in an app variable (stored would probably be better…). Then add the listener to the game node, that way only the game the player joined is being listened to (vs. all of the games in the databse).

Adding a play to an existing game is pretty easy. Just give them the index of the game and add them as a player.

Now you are ready to pull the scores. The key here is the sorting of objects, which Thunkable cannot do. But never fear, DrTed has a prescription for that (More JSON - Sort Object List by Object Property) with the SortObjectList function. Once you copy that function to your screen, you can build this listener.

Debugging listeners can be pretty tricky, since the events happen based on data updates by other users. So robust interpreting of the data (null checks, property checks, etc.) will save you time in the end. After verifiying the listener returned the expected object, sort the players list by the score and loop through the sorted list, adding players to the List Viewer.

One final tip. I found that for testing apps where different users are communicating, I found it easiest to sign into Live test with 2 different Firebase accounts, one on a device and one on the web live test.

Disclaimer: I haven’t tested these blocks, so I’m sure there are a few bugs. But hopefully this will get you started.

Happy Thunking!

P.S. It would probably be a good idea to create an Alias in the Player Object to display in the leader board, again, hopefully this gets you started…

2 Likes

Thanks so much! This is really helpful.

Yes, I do that. I click on live test twice, so it opens on 2 different tabs.
I am still stepping into JSON and Objects, but I can try this out. Before I stated coding for this, I thought it was just a matter of in list sort numeric ascending and a list-viewer, but it turns out it’s so much more😶
QuizGame → Games (List) ->Game (Object) → Players (list) → Player (Object) ->Score (property).

I have 3 screens: A splash screen where the user has to enter their name, and a quiz screen, where the actual quiz is taking place, and a last, insignificant screen which is just the end-screen that should display your final score and the result (who the winner is).
In which screen should the following code take place (the code that you posted after the part I have quoted)? Should it be the splash screen where the user actually gets put into a game, or the quiz screen? Also, you have initialized a variable currentGameIndex. Since your first step was to check whether there was already a game, should I initialize it to 1, or is there some connection I have missed?
Where have you initialized this variable


and what have you initialized it to ?
Thanks!

The Splash Screen would be my recommendation.

My intention was that if there is no Games list, the index should be 0, if there is a Games List, then the index should be the length of the list. Firebase list start at 0 (not 1) so the first list should be a zero index.

Oops, my bad! I had some “Stored” variables that should have been cloud variables. My Apologies. I a have updated the original post to make them cloud variables.

1 Like

Thanks very much @drted! I also tried using Airtable to save scores and player names, but it’s not working. Could you share the link of this project which has this code so I can take a closer look?
Here is my Airtable code:


There’s no data as of now.


image
All the images have been given in sequence. Could anyone please check this out and if any, give me suggestions? Also @drted, the link for that code would be really helpful, because I am not yet good enough to do independent JSON, and need a closer look.
Thanks @thunkers!

1 Like

I haven’t read this entire thread but I do have a multiplayer “game” working in the sense that multiple people on different devices can join a hosted game. I use “game” in quotes because all it does so far is host/join but there’s no actual gameplay.

I set it up with Firebase. It’s a stretch for me, even with my fairly strong understanding of JSON and data structures.

1 Like

I already have created a multi-player game(Tic Tac Toe) using RTDB. It’s working properly, but I don’t know how to do it keeping in mind the rules of this game. I didn’t use any JSON for the tic tac toe, though,

Yes, tat’s what I’m trying to make too.