[Got caught up in something else and posting this a little late, but….]
I’m surprised you received any error from that, considering your example doesn’t call cPrompt()
at all—you forgot the call parentheses. Beyond that, you’re indexing the wrong thing $NPC.name
vs. $NPC
—i.e., you should be doing $NPC[i].name
rather than $NPC.name[i]
.
Corrections:
Actually calling the function within the conditional expression:
<<if cPrompt('James')>>
Test: Found James.
<</if>>
Properly indexing the array:
window.cPrompt = function (n) {
for (let i = 0; i < State.variables.NPC.length; i++) {
if (State.variables.NPC[i].name === n) {
return true;
}
}
return false;
};
Though, as a suggestion, if you’re going to reference State.variables
multiple times within a function, caching it isn’t a bad idea:
window.cPrompt = function (n) {
const sv = State.variables;
for (let i = 0; i < sv.NPC.length; ++i) {
if (sv.NPC[i].name === n) {
return true;
}
}
return false;
};
Further, in this case, you could simply cache $NPC
instead:
window.cPrompt = function (n) {
const NPC = State.variables.NPC;
for (let i = 0; i < NPC.length; ++i) {
if (NPC[i].name === n) {
return true;
}
}
return false;
};
Finally. There are native methods—e.g., <Array>.some()
—that you could have used instead of rolling your own. For example.
Using <Array>.some()
directly within the conditional expression:
<<if $NPC.some(npc => npc.name === 'James')>>
Test: Found James.
<</if>>
If you prefer using your cPrompt()
function, then using <Array>.some()
within your function:
window.cPrompt = function (name) {
return State.variables.NPC.some(npc => npc.name === name);
};