Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[HELP] Cross-script global variables?

July 12 (6 years ago)

Edited July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

In my Fire Emblem Tabletop Script, for convenience' sake when testing it on my own, I have all the functions divided up separately into different files. Let's say I declare a global variable in one file.

var queue = [];

Can this variable be accessed from another separate script in some way, or are all scripts sandboxed separately, making that impossible? My goal is to make a global queue that can be accessed from all scripts, though I don't know if that's possible.

Technically all of the API scripts are concatenated with each other. So that Roll20 only sees one javascript file like apiscripts.js. So you could create this global variable in one script and use it in others. I would just recommend that you put this variable in the very first API script in the list to ensure that it is created first and usable in each later script.

July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

That's weird, though, since if I declare queue on the first line of the first script and try to log it on the first line of the second script, it immediately throws a reference error.

July 12 (6 years ago)
The Aaron
Pro
API Scripter

When you say you get an immediate reference error, are you talking about when you put the scripts in roll20, or in something else like a local sandbox for testing or the like?

July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

In Roll20, that is. It automatically gives me ReferenceError: queue is not defined when I attempt to save, presumably because the scope for error scanning is only limited to the current API script?

How are you defining the variable? Can we see it's code?

July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

It's literally just

Script 1:
var queue = [];

Script 2: log(queue);

That's it.

July 12 (6 years ago)

Edited July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

Okay, so it seems to have a problem with this when I use a pre-existing game, but I get no such error when I make a new game and type up the exact same thing, which is weird.

Edit: Nevermind, the problem solved itself?

Is it possible that the problem occurred because you didn't save the first script before writing the second script?

July 12 (6 years ago)
Missingquery
Pro
Sheet Author
API Scripter

That's probably it, yeah. ^^