The beauty of using arrays, especially if you prepopulate them with ONLY the valid options, then remove the options as they're chosen, is that you're not going to ever get the freakish situation where you draw invalid option after invalid option and end up in a very long loop while it's trying to choose a valid option (you could get this is only one option out of 20 were valid, and you were randomly choosing from the same full list each time)
Yeah that's a point. I'm trying to do so.
Splice creates a new array from the item(s) removed from the array it's called on. If you want to create a discard pile/array, it'd probably be better to push the spliced value into your discard array, then reference the discard array's last entry (what you just pushed) to add it to the player.
Wow that's hard for a noob like me to imagine. In my project I currently will not need a discard pile but for the sake of learning I would like to understand this.
So I have one array for the valid "Deck mechanic skills",
skillsDeck. I could have one array for the discard pile,
discardDeck. And let's say an array for hand cards,
handCards. Would that be correct?
Code:
for (var i = 4; i >= 1; i--) { //i = number of actors
if ($gameActors.actor(i).currentClass().id == 5) { //check if it's the deck mechanic class
var skillsDeck = [10, 11, 12, 13, 14, 15];
var handCards = [];
for (var i = 0; i < 3; i++) {
let drawID = Math.floor(Math.random() * skillsDeck.length);
$gameActors.actor(i).learnSkill(drawID);
handCards.push() = skillsDeck.splice(drawID, 1);
}}}
I'm pretty sure this is wrong. It's hard for me to understand code descriptions if I do not see the actual code. Because I'm still a rookie.
🙂
Also, because that was a quickie, I forgot to mention that it's worth checking to make sure a valid skill id is retrieved. If the array is empty, nothing should be added to the player obviously because there's nothing left.
Ah yeah. The array will never be empty in my case. The drawn/learned skills will be unlearned at the end of turn and the array will be reset to contain its former values.
But if I would need to check this that would be:
if (skillsDeck.length>0) { .. }
Right?
I have another question regarding this. What is the difference between
Math.floor(Math.random() * x) and
Math.randomInt(x) + y? Is there a benefit (technical or performance wise) over using one over the other?
Thank you for the patience!
😀
Edit:
I modified the code above as there were some things that I didn't consider before.
The actual code looks like this:
Code:
for (var i = $player_count; i >= 1; i--) { //$player_count is 1 to 4
if ($gameActors.actor(i).currentClass().id == 5) { //check if class with drawing mechanism
for (var b = 67; b < 119; b++) { //run through all of classes skills
if $gameActors.actor(i).skills().contains($dataSkills[b]) { //if actor/player learned the skill
$skillsDeck[i].push(b) //insert the skill into the deck array
$skillsLearn[i].push(b) //store unlearned skills in an array to learn them back at the end of combat
$gameActors.actor(i).forgetSkill(b) //unlearn skill (player needs to draw it instead, for learning it again)
}
}
for (var c = 0; c < 3; c++) { //drawing mechanism, 3=number of cards to draw
let drawID = Math.floor(Math.random() * $skillsDeck[i].length);
$gameActors.actor(i).learnSkill(drawID);
$handCards[i].push($skillsDeck[i].splice(drawID,1)) ; //store hand cards in array and remove deck cards from skillsDeck array of actor i
}
}
}
It will be run at the start of combat and the drawing part below each round.
Then at the start of each round this will be run:
Code:
for (var i = $player_count; i >= 1; i--) {
if ($gameActors.actor(i).currentClass().id == 5) {
$skillsDeck[i] = $skillsLearn[i] //reset the deck
for (var b = $handCards[i].length; b > = 0 ; b--) { //unlearn previously drawn skills and delete handCards array entry for them
$gameActors.actor(i).forgetSkill(b)
$handCards[i].splice(b,1)
}
for (var c = 0; c < 3; c++) {
let drawID = Math.floor(Math.random() * $skillsDeck[i].length);
$gameActors.actor(i).learnSkill(drawID);
$handCards[i].push($skillsDeck[i].splice(drawID,1));
}
}
}
The previously learned/drawn skills will be unlearned and the hand array will be reset. Then new skills will be drawn.
At the end of battle all skills stored in $skillsLearn[x] will be relearned and the arrays will be emptied:
Code:
for (var i = $player_count; i >= 1; i--) {
if ($gameActors.actor(i).currentClass().id == 5) {
for (d = skillsLearn[i].lenght; d = 0; d--) {
$gameActors.actor(i).learnSkill(d);
}
$skillsDeck[i] = []
$handCards[i] = []
}
}
I know it is much to read and instead of asking I would just test this out. But at the moment I can not test this in my project yet.
Is the code and thought behind it correct the way I did it?
🙂