how to insert automatically a bunch of data with "for" into an array?

Xyonel

Indie Developer, All-in-one artist.
Veteran
Joined
Jul 22, 2018
Messages
183
Reaction score
37
First Language
Italian
Primarily Uses
RMMV
wordList = []

for ( i=0; i<16; i++){
actorName = $gameActors.actor(i).name.toUpperCase();
wordList.push(actorName(i));
}

this does not work, someone have a solution? i'm beginner in javascript for now, still learning.

cannot read property " name " of null.
 

deykuzor

Korean Guy Screaming in Italian American
Member
Joined
Sep 7, 2017
Messages
23
Reaction score
5
First Language
English
Primarily Uses
RMMV
My javascript is also weak but if you're getting a "cannot read" message, it's probably because whatever it's trying to find the property in is not actually an object (yet). Javascript has no private variables so like, you can technically read anything if you extend it properly..

Looks like you're attempting to push a list of party member names to an array. I think, not sure, but the problem may be related to not having an actual object made before trying to access the name property in $gameactors
 

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
713
Reaction score
564
First Language
French
Primarily Uses
Your syntax is all over the place.
You define actorName as a string that will contain an actor's name and in the line below you call it as a function with actorName(i). You just need to push the currently defined actorName inside the loop.

That's not the error you're getting though. It sounds like you've tried to access the name of an actor that doesn't exist. Look at your database, you'll see that actor ids start at 1 and not 0. That's why you're getting the crash. You're trying to get $gameActors.actor(0) and it doesn't exist. Start your loop from actor 1 after fixing the first mistake I pointed out and it should work.
 

Xyonel

Indie Developer, All-in-one artist.
Veteran
Joined
Jul 22, 2018
Messages
183
Reaction score
37
First Language
Italian
Primarily Uses
RMMV
Ast, do you mean this?

for ( i=1; i<17; i++){
wordList.push($gameActors.actor(i).name().toUpperCase());
}

and what do you mean syntax all over the place?
 

deykuzor

Korean Guy Screaming in Italian American
Member
Joined
Sep 7, 2017
Messages
23
Reaction score
5
First Language
English
Primarily Uses
RMMV
Yeah sorry, I totally blanked. You don't need this:

wordList.push(actorName(i))

It should be

wordList.push(actorName);

cos as Astfgl66 pointed out, you already set the actor at that pointer in the array to actorName. And yeah, like we both pointed out, there was nothing at index 0, so

for ( i=0; i<16; i++)

should have been (i=1;i<16;i++)

I would recommend looking at how operators, arrays, pointers, and properties within RMMVs current classes work.

As someone who is also learning and needed Astfgl66's insight to remind me, programming is a lot of remembering what the building blocks you have can do, and then using them correctly.
 

deykuzor

Korean Guy Screaming in Italian American
Member
Joined
Sep 7, 2017
Messages
23
Reaction score
5
First Language
English
Primarily Uses
RMMV
Ast, do you mean this?

for ( i=1; i<17; i++){
wordList.push($gameActors.actor(i).name().toUpperCase());
}

and what do you mean syntax all over the place?
Sorry for the double post, but I think what he meant was you're calling actorName like it was a function by putting the (i) there when you already set actorName as a string object. Look at my second post.
 

Xyonel

Indie Developer, All-in-one artist.
Veteran
Joined
Jul 22, 2018
Messages
183
Reaction score
37
First Language
Italian
Primarily Uses
RMMV
btw as it is works fine, isn't better this format? more compact?
 

deykuzor

Korean Guy Screaming in Italian American
Member
Joined
Sep 7, 2017
Messages
23
Reaction score
5
First Language
English
Primarily Uses
RMMV
If it works, then it works but compactness can impede clarity. Code clarity enables you or others to quickly read it and change/adjust it. It's a good practice to be clear during development and then upon release, try and then compact the code. You can write an easily read version and comment out a more compact version to use later if it helps.
 

Xyonel

Indie Developer, All-in-one artist.
Veteran
Joined
Jul 22, 2018
Messages
183
Reaction score
37
First Language
Italian
Primarily Uses
RMMV
yeah but this little script is meant to be used as a personal tweak and not for public scope, i got the info btw i knew that thanks anyway
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Would Jump into Javascript be best off continuing to break down the MV core files, or would people prefer a quick detour to cover everything that's changed in the core from MV to MZ?
Not going to lie... PIXI Particles make fantastic graphics... But predicting the hitbox of something with a "fixed size" but whose graphics can stick out past that, is quite challenging. :kaoswt:
Would you like a glass of spiders?
Besides my Might and Magic inspired project, I have always wanted to make a Fire Emblem-like game. Time is the culprit... Between life, composing music packs, and working on a game can it be done!? The tactical plugins for MV work quite well based on what I have tested.

Forum statistics

Threads
100,530
Messages
976,891
Members
132,081
Latest member
Kaezyelnatz
Top