2 Dimensional array and .push() .length methods

Hello!

In this thread I was asking if my code is correct. Now I tested it and it doesn't work. I suppose it has something to do with the 2 dimensional arrays and the .push() / .length methods that I use.

Example:

Deck[1].length
Deck[1].push(cardID)

I think I'm doing something wrong.
How can I use .push() and .length with 2 dimensional arrays?

Thanks in advance!
I extracted one of the codes from the linked thread.
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] = []

    }

}

There's a typo in the length word. Perhaps that is why it doesn't work?
Other than that, the code in your example is correct and should be working.
There's a typo in the length word. Perhaps that is why it doesn't work?
Other than that, the code in your example is correct and should be working.

Oh thanks for pointing that out! I forgot to mention that the problem starts with the first section of code. When the battle starts.

At the moment the "battle start" code looks like this:

Code:
$skillsDeck = []

$skillsLearn = []

$handCards = []

for (var i = 4; i >= 1; i--) {

$skillsDeck[i] = []

$skillsLearn[i] = []

$handCards[i] = []

}


for (var i = 1; i <= 4; i++) {

if ($gameActors.actor(i).currentClass().id == 5) {

for (var b = 67; b < 119; b++) {

if ($gameActors.actor(i).skills().contains($dataSkills[b]) == true) {

$skillsDeck.push(b)[i];

$skillsLearn.push(b)[i];

$gameActors.actor(i).forgetSkill(b); }}

for (var c = 0; c < 3; c++) {

let drawID = Math.floor(Math.random() * $skillsDeck[i].length);

$gameActors.actor(i).learnSkill(drawID);

$handCards.push($skillsDeck.splice(drawID,1)[i]);

}}}

I tried many different ways to handle .push() with 2D arrays.

I tried:

Code:
$skillsDeck[i].push(b);
$skillsDeck.push(b)[i];
$skillsDeck[i].push = b;
$skillsDeck.push([i,b]);
$skillsDeck.push([i][b]);

and

Code:
$skillsDeck[i].length
$skillsDeck.length[i]

But it all doesn't seem to work. The "forget skills" part does work. But redrawing them from the $skillsDeck array doesn't work. I guess that is because the IDs are stored/pushed wrongly in the 2D arrays before.
Well...

Code:
for (var i = 4; i >= 1; i--)
will never work. The computer has a tremendous problem with <= and >= inside for loops and immediately terminates the loop. You need to use > 0 instead of >= 1.

Code:
if ($gameActors.actor(i).skills().contains($dataSkills[b]) == true)
The == true can be omitted.

Code:
$skillsDeck[i].push(b);
is correct.
Code:
$skillsDeck[i].length
is correct too.

It's your for loops that are at fault here.
seems like a lot of global looking variables tbh.
The computer has a tremendous problem with <= and >= inside for loops and immediately terminates the loop. You need to use > 0 instead of >= 1.

It's your for loops that are at fault here.

Oh. I didn't know that. I fixed the code now:

Code:
$skillsDeck = []

$skillsLearn = []

$handCards = []

for (var i = 4; i > 0; i--) {

$skillsDeck[i] = []

$skillsLearn[i] = []

$handCards[i] = []

}


for (var i = 1; i < 5; i++) {
    if ($gameActors.actor(i).currentClass().id == 5) {
        for (var b = 67; b < 119; b++) {   
            if ($gameActors.actor(i).skills().contains($dataSkills[b])) {
                $skillsDeck[i].push(b);
                $skillsLearn[i].push(b);
                $gameActors.actor(i).forgetSkill(b); }}
        for (var c = 0; c < 3; c++) {
            let drawID = Math.floor(Math.random() * $skillsDeck[i].length)+67;
            $gameActors.actor(i).learnSkill(drawID);
            $handCards[i].push($skillsDeck[i].splice(drawID,1));
        }
    }
}

But unfortunately the drawing skill mechanism still doesn't work. Only the forget skill part. All 4 characters forget their skills at the start of battle but none of them draw new skills. (I set all actors to that class)


Edit:

Yay! I got it to work! The problem was with the random part. I forgot to add +67, because thats where the skills for that class start. Now it seems to work.
I will fix the part where new cards will be drawn at the start of the turn and check if "$handCards.push($skillsDeck.splice(drawID,1))" is correct or not.
Nice 😀
I completely overlooked that. Nevertheless, that's why we have console.log(x) for debugging. If something doesn't work as it is supposed to, you ought to throw a couple of console.logs in the functions to check if the values check out.
Nice 😀
I completely overlooked that. Nevertheless, that's why we have console.log(x) for debugging. If something doesn't work as it is supposed to, you ought to throw a couple of console.logs in the functions to check if the values check out.

Ahhh. I will use "console.log(x)" to test why the redrawing isn't working properly. Sometimes the actors draw 3 skills (as supposed), sometimes only 2 or even only 1. 😀

Code:
for (var i = 1; i < 5; i++) {
    if ($gameActors.actor(i).currentClass().id == 5) {
        $skillsDeck[i] = $skillsLearn[i];
        for (var b = 0; b < $handCards[i].length + 1;  b++) {
            $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)+67;
            $gameActors.actor(i).learnSkill(drawID);
            $handCards[i].push($skillsDeck[i].splice(drawID,1));
        }
    }
}

Thats the code that I use from turn 2 to end of battle.

I know it's a totally noob question but does Math.floor(Math.random() contain 0? And should I use it over Math.randomInt()?
Math.random does contain 0. And Math.floor(Math.random() * x) is exactly the Math.randomInt (x) function, so it's not really an improvement.
Okay. Then I will use Math.randomInt(x), as I find it much more readable.


Edit:

I solved the problem now. It was an issue with the random code. I completely mixed the array positions with the skills ID position up in my head.

Code:
for (var i = 1; i < 5; i++) {
    if ($gameActors.actor(i).currentClass().id == 5) {
        for (var b = 67; b < 119; b++) {       
            if ($gameActors.actor(i).skills().contains($dataSkills[b])) {
                $skillsDeck[i].push(b); console.log("skillsDeck[" + i + "] = " + b);
                $skillsLearn[i].push(b);
                $gameActors.actor(i).forgetSkill(b); }}
        for (var c = 0; c < 3; c++) {
            let drawID = Math.randomInt($skillsDeck[i].length); console.log("draw iteration #: " + c); console.log("$skillsDeck[" + i + "].length + 67 = " + ($skillsDeck[i].length + 67));
            $gameActors.actor(i).learnSkill($skillsDeck[i][drawID]); console.log("randomly rolled Deck["+i+"] array position #= "+drawID); console.log("attempting to learn skill ID: "+$skillsDeck[i][drawID]);
            $handCards[i].push($skillsDeck[i].splice(drawID,1));
}}}

The console.log method helped a lot to understand what the code is trying to do. I realised that, at first, the random rolled IDs didn't match the IDs in the array at all. 🙄😀

Before I mark it as solved I want to test it some more. Thanks again Poryg. 🙂
Well...

Code:
for (var i = 4; i >= 1; i--)
will never work. The computer has a tremendous problem with <= and >= inside for loops and immediately terminates the loop. You need to use > 0 instead of >= 1.

Curious, why do you say this? I have never heard this to be true.
@Aloe Guvner Experience. I've had code snippets not work properly due to the <= in for loop that got fixed just by turning it from <= 4 to < 5.
I do not want to double post, so I link to my other post. The drawing mechanism works just fine. But now I have the problem, that whatever I try to do the game doesn't let the actors forget their drawn skills. In the variable part it looks fine what the code does. I used an extensive amount of console.log() methods.

I need help please. (At my last post) 😕
upload_2019-5-11_13-0-40.png

~ seems to do what it says on the tin tbh..

I personally havent had that issue before.

Original thread: https://forums.rpgmakerweb.com/threads/2-dimensional-array-and-push-length-methods.108888/