Define a variable in mygame.js?


#1

I’ve been trying to create a variable in mygame.js for storing the player name. Every time I try to run the game (which is running locally if that matters?) it says undefined variable. It doesn’t seem to read any attempt to define the variable in that file, but in stats it will read any integer value I set up in there.

I have modified two things, in the startup.txt I added one line at the end of a sentence to call the variable

${given_name}

and in the mygame.js file I added this line outside of nav/stats etc. so its on its own line:

var given_name = “bob”;

I have also tried just
given_name = “bob”;
but that doesn’t work either.

So whenever I launch the index file and it calls given_name it says undefined. Does choicescript require it be defined in a certain location or way to be read by the other files?


#2

stats = {
given_name: “Bob”
,last_name: “Smith”
,nick_name: “Lucky”
(etc.)
};

That should do the trick. New variables should be included within “stats”. Also note the use of colons, and where the commas go to separate variables.


#3

Actually, the commas could just as easily go at the end of the lines instead of at the beginning of the lines. It’s just that you can’t have one at the beginning of the first line or at the end of the last line. Some people think it’s easier to have them at the beginnings of lines so that you can easily add another line to your list without having to remember to put a comma at the end of the current last line.


#4

Hehe. I wouldn’t know (not being a programmer myself) which is why, with another obvious CS noob, it’s probably best to advise them to stick as close to CoG’s own layout as possible. That way, should he have another problem with mygame.js, we’re not all (with one or two exceptions!) telling him he has his commas in the wrong place for a start . . . :smiley:


#5

Good point, @Vendetta. Still, my judgment of you is that you have progressed far enough with programming to appreciate additional pertinent information, and will be better armed if someone else does come along and decides to put the commas at the end instead of the beginning. Not that anyone is likely to do so in this community. Hell, I even keep the commas at the beginning of the lines in mygame.js, contrary to my strong personal preference, but I’m just adhering to the established CoG pattern. When in Rome…


#6

One thing I had to learn the hard way was that all variables defined in mygame.js need to be entirely lowercase if you want them accessible by your CS scene files. CS isn’t case sensitive the way JavaScript is, and so the variable invSize in CS is not the same as invSize in mygame.js. On the other if you define invsize in mygame.js, it doesn’t matter if you access it as invsize, INVSIZE, or invSize in CS, they’re all the same as far as your CS scene files are concerned.