High Score List

About advanced scenes, and the Thyme scripting language used in Algodoo.

High Score List

Postby TShrepel85 » Thu Mar 07, 2019 11:28 pm

Hello again! So this time I'm in need of something that will function as a high score list for my pinball machine. It'll need to have 10 entries, and each entry will need to contain rank, name, and the score achieved. I know I need to use an array (or multiple arrays) but I don't even know where to start. I've been combing the forums, and will continue to do so, to try to figure out how arrays are even handled in thyme. Thank you so much for your support!
TShrepel85
 
Posts: 7
Joined: Mon Mar 04, 2019 9:00 pm

Re: High Score List

Postby TShrepel85 » Sat Mar 09, 2019 6:44 pm

Okay, I'm too stupid to understand the single array element modification procedure outlined in Xray's response to Skrubs question at this post viewtopic.php?f=13&t=12152&p=86409&hilit=array&sid=f5a48638bb611adb33e17e876143d958#p86409 , so I endeavored to make my own solution.

I'm using ten arrays, each holding a name and a score, and an eleventh temporary array that is used for comparison. I call each array in the text of ten separate boxes on display next to my pinball playfield. At the end of the game, my script shoves the name entered by the player and the score accumulated into the temporary array, then begins comparing it to all the other arrays to see which one is greater. If it finds an score less than the temporary array score, starting at the bottom of the list, it will shift all arrays downward and make room for the new score. Looks something like this:
Code: Select all
(e)=>{
    hstemp(1) > hs1(1) ? {
        hs10 = hs9;
        hs9 = hs8;
        hs8 = hs7;
        hs7 = hs6;
        hs6 = hs5;
        hs5 = hs4;
        hs4 = hs3;
        hs3 = hs2;
        hs2 = hs1;
        hs1 = hstemp;
        hstemp = [0, 0]
    } : {}
}


This particular bit of code refers to the #1 score slot, and all the code resides in the update portion of each of the ten score boxes. Slot #2 would be missing the hs1=hstemp line and instead would have hs2=hstemp and would be comparing hstemp(1) to hs2(1). I'm sure there was an easier way of doing this, but it works for now. Let me know what you think! Thanks!
TShrepel85
 
Posts: 7
Joined: Mon Mar 04, 2019 9:00 pm

Re: High Score List

Postby FRA32 » Mon Mar 11, 2019 11:00 pm

You can actually stack arrays, thus only requiring one array for the entire scoreboard, which then again holds 10 sub-arrays per entry, which then hold the data.

An array in thyme is a multi-entry-variable. It is a single variable with with a specific size, and each element of the array(numbered 0 to size-1) holds a variable of it's own. Imagine it like being bad at naming stuff and naming your images image0, image1, image2, image3, but all grouped together nicely in an "image" folder.

For reasons only algoryx can tell us, array's are read-only, meaning that once you create an array, you can only find out what's inside it, or replace the entire thing. That is the principle that X-Ray's script propably circumvents by completely re-assembling an array from scratch by reading all elements, replacing the one you want to change, and writing all elements into a new array, which replaces the old one.

for your example, your array would look like this:
hs = [["",0],["",0],["",0],["",0],["",0],["",0],["",0],["",0],["",0],["",0]]
As you can see, its one array holding 10 smaller arrays for the scoreboard. The temporary array you need should be created whenever you need one by typing something like "tempvar = ["",0]".
Now to editting the array:

As mentioned, editting single elements of an array requires assembling a new array from scratch, where a single element is replaced with the new entry in the process. I coded a simple code for replacing entries here, which does not require XRay's Xfor in it.

Open the object holding your script, and copy this entire code into the little empty black box in the top left:

(Pick the first for a function only existing in one object, or the second for a global function)

Code: Select all
_changeEntry/scene.my.changeEntry = (a, m, d)=>{
    templist = [];
    for(4, (i)=>{
        tempentry = [];
        i == m ? {
            tempentry = d
        } : {
            tempentry = a(i)
        };
        templist = templist ++ tempentry
    });
    templist
}


You can now use it by typing _changeEntry/scene.my.changeEntry(array, index, new entry in format ["name", score])
so for example, to change the 8th entry in the scoreboard, you would type

_changeEntry(hs, 7, ["Fra32Roolz", 9001]);

Keep in mind that, since arrays start counting from 0, the 8th entry is number 7, just as the first entry is number 0.
FRA32
 
Posts: 227
Joined: Wed Dec 03, 2014 9:51 pm

Re: High Score List

Postby TShrepel85 » Thu Mar 21, 2019 12:19 am

Thank you SO MUCH for this code! I made adjustments to lines 10 and 12 to make it work in my scene:

Code: Select all
(a, m, d)=>{
    templist = [];
    for(10, (i)=>{
        tempentry = [];
        i == m ? {
            tempentry = d
        } : {
            tempentry = a(i)
        };
        templist = templist ++ [tempentry]
    });
    hs = templist
}


Then I added this code to the "text" field of the box that got that code:

Code: Select all
{
    " \tHighscores\n1.)   " + hs(0) + "\n2.)   " + hs(1) + "\n3.)   " + hs(2) + "\n4.)   " + hs(3) + "\n5.)   " + hs(4) + "\n6.)   " + hs(5) + "\n7.)   " + hs(6) + "\n8.)   " + hs(7) + "\n9.)   " + hs(8) + "\n10.) " + hs(9)
}


Kinda bulky I know, but I couldn't figure out how to put a for loop in here. Lastly, I placed this code in the "update" field of that same box:

Code: Select all
for(10,(i)=>{
   hsComp=hs(i);
   hsTemp(1)>hsComp(1)?{
      for(9-i,(x)=>{
         hsShift=hs(8-x);
         _changeEntry(hs,9-x,hsShift)
      });
      _changeEntry(hs,i,hsTemp);
      hstemp=[0,0]
   }:{}
})


And it seems to be working great! Now we'll see if this translates to the Algobox unlike the rest of my variables in this scene, but that's a problem for my other thread. Thanks again!
TShrepel85
 
Posts: 7
Joined: Mon Mar 04, 2019 9:00 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 3 guests