[SOLVED] Getting images to match sorted integer array

I’m nearly done with ETA’s initiative functionality. However, one small problem remains. I don’t know how to make the token images match the initiative scores!

Here’s an example of what I mean. The following initiative scores are generated for each token and placed in order:

In this example, the order of token images should be (from top to bottom): green, gold, cyan, and magenta.

Here is the script in question. I’m open to ideas about how to make this work.

// generates initiative rolls for each tokenUI
goldTokenInitRoll = (pc.math.random(1, 21) + goldTokenInitMod).toFixed(0);    console.log("Gold:", goldTokenInitRoll);
cyanTokenInitRoll = (pc.math.random(1, 21) + cyanTokenInitMod).toFixed(0);    console.log("Cyan:", cyanTokenInitRoll);
magentaTokenInitRoll = (pc.math.random(1, 21) + magentaTokenInitMod).toFixed(0);    console.log("Magenta:", magentaTokenInitRoll);
greenTokenInitRoll = (pc.math.random(1, 21) + greenTokenInitMod).toFixed(0);    console.log("Green:", greenTokenInitRoll);

// place the rolls in an array
var rolls = [goldTokenInitRoll, cyanTokenInitRoll, magentaTokenInitRoll, greenTokenInitRoll];
// call the position function to order the rolls from greatest to least
console.log(rolls);
this.Position(rolls);
console.log(rolls);

// -------------------------------------------------------------------------------------------------------------------------
// make an array for the texture assets
var textures = [this.goldAsset, this.cyanAsset, this.magentaAsset, this.greenAsset];
// 
// ideas for getting numbers and textures to match:
    // 
// 
// ORDER THE POSITION ROLLS TOKEN IMAGES USING ATTRIBUTES
// this.app.root.findByName("Track_1st_Place_Img").element.texture = ;
// this.app.root.findByName("Track_2nd_Place_Img").element.texture = ;
// this.app.root.findByname("Track_3rd_Place_Img").element.texture = ;
// this.app.root.findByName("Track_4th_Place_Img").element.texture = ;

// print the initRolls to the initiative tracker
this.app.root.findByName("Track_1st_Place_Txt").element.text = rolls[0];
this.app.root.findByName("Track_2nd_Place_Txt").element.text = rolls[1];
this.app.root.findByName("Track_3rd_Place_Txt").element.text = rolls[2];
this.app.root.findByName("Track_4th_Place_Txt").element.text = rolls[3];

// disable this UI
// this.app.root.findByName("Init_Button").script.initTracker.deSelect();
this.app.root.findByName("Initiative_Gen_Group").enabled = false;
// enable the init tracker UI and bring it into frame
this.app.root.findByName("Init_Track_Group").enabled = true;
this.app.root.findByName("Init_Track_Group").setPosition(0, 0, 0);

};

// a function for ordering initRolls
InitRoller.prototype.Position = function(tokens) {
// orders the initiative rolls from first to fourth (descending order)
tokens = [tokens.sort(function(a, b){return b-a;})];
};

I think the easiest way to do it would be to add some more script to the Position() function creation. Specifically, a way to record the order that each token got sorted into. So, in our example, in terms of index locations, gold came in second [1], cyan came in third [2], magenta came in fourth [3], and green came in first [0].

So, in this example, the Position() function might also re-arrange the textures array as [this.greenAsset, this.goldAsset, this.cyanAsset, this.magentaAsset]. But I would have to understand exactly how
tokens = [tokens.sort(function(a, b){return b-a;})];
worked before I could do that.

1 Like

Holy crud, I actually found a good example of how to do what I’m trying to do.
https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_sort_object1

1 Like