0

I have dialog in a javascript object separated into objects and arrays (similar to JSON format).

I'd like to access these objects and arrays by passing in their respective variable names...

For example, how would I access the "parent_object_name" object and child array "array_name"? I've tried creating objects and arrays, then passing them in, as such:

var parent_object_name = npc_dialog.people[NPC_id].dialogs.answers;
var child_array_name = npc_dialog.people[NPC_id].dialogs.answers.AnswerOne;
cycleDialog(0, parent_object_name, child_array_name);

But that didn't work...

So then I tried passing the names in as strings:

cycleDialog(0, "parent_object_name", "child_array_name");

This also didn't work.

cycleDialog takes those variables and substitutes them in as such:

function cycleDialog(NPC_id, TYPE, SUBTYPE) {
    NPCs_ARRAY[NPC_id].children[1].text = npc_dialog.people[NPC_id].dialogs.TYPE.SUBTYPE[dialog_id];
}

Thank you


EDIT:

Data looks like:

"dialog" : 
{   
    "dialog_name" :
    [ 
              "dialog",
              "more text..."
    ]
},
"answers" : 
{   
    "answer_name" :
    [ 
              "answer text here",
              "more text..."
    ]
}
1
  • 1
    Is this any different than your other question? Commented Aug 26, 2013 at 3:14

3 Answers 3

2

You probably want this:

function cycleDialog(NPC_id, TYPE, SUBTYPE) {
    NPCs_ARRAY[NPC_id].children[1].text = npc_dialog.people[NPC_id].dialogs[TYPE][SUBTYPE][dialog_id];
}

along with:

cycleDialog(0, parent_object_name, child_array_name);

The difference between .x and [x] is that .x looks up the key 'x', whereas [x] looks up the key that the variable x evaluates to:

> var array = {x: 100, foobar: 200}
> var x = 'foobar';
> array.x
100
> array[x]
200
Sign up to request clarification or add additional context in comments.

4 Comments

So now when I pass in cycleDialog(0, "answers", "Mandarin");, dialogs should be able to look up the keys that match the strings "answers" and "Mandarin", right? It says Cannot read property 'answers' of undefined though
@Growler: That means that npc_dialog.people[NPC_id].dialogs is undefined instead of what you expect.
Okay, thank you. That difference (.x and [x]) you mentioned above, in terms of accessing the key value vs accessing the key that the variable evaluates to... is that just used for objects, or can you have a similar structure with arrays to?
@Growler: that's the same for arrays also, although x.1 is invalid syntax so you can't do it that way for numbers
0

You can't pass variables to a function by passing their names. The only way to do that would be to pass their address or value.

Comments

0

Your function cycleDialog gets the data from the object npc_dialog.people[NPC_id].dialogs. If I understand correctly, all you need to do is pass it the names of the objects within that object that you want it to look up, and follow Claudiu's answer for altering the cycleDialog function.

e.g. cycleDialog(0, 'answers', 'answer_name');

would return [ "answer text here", "more text..." ]

using the example data you give (and assuming this data is found within npc_dialog.people[0].dialogs).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.