2 Dimensional array and .push() .length methods

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
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!
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,646
First Language
Czech
Primarily Uses
RMMV
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.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
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.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,646
First Language
Czech
Primarily Uses
RMMV
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.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,302
First Language
Binary
Primarily Uses
RMMZ
seems like a lot of global looking variables tbh.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
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.
 
Last edited:

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,646
First Language
Czech
Primarily Uses
RMMV
Nice :D
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.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
Nice :D
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. :biggrin:

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()?
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,646
First Language
Czech
Primarily Uses
RMMV
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.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
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. :rolleyes::biggrin:

Before I mark it as solved I want to test it some more. Thanks again Poryg. :smile:
 
Last edited:

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,123
First Language
English
Primarily Uses
RMMV
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.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,646
First Language
Czech
Primarily Uses
RMMV
@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.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
40
First Language
German
Primarily Uses
RMMV
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) :confused:
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,302
First Language
Binary
Primarily Uses
RMMZ
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.
 

Latest Threads

Latest Posts

Latest Profile Posts

I have an idea, I am making low polygon 3D resources for a package in itch. but it occurred to me that I could export these as images and make MV / MZ compatible tilesets. What do you think of this?
I hate seeing threads where less-experienced user's ask for an opinion of their project, and the thread is full of negative feedback by others who are clearly not the target audience. I feel bad for the OP, and I hope they understand that no game can appeal to everyone. :frown:
Hm, just found out I can't use loops, yay.
-Ele
I just wanna Covid to be over and I have my normal life back... Feeling so bad these days, what can I do?

Forum statistics

Threads
109,074
Messages
1,041,866
Members
141,571
Latest member
1a23z11
Top