I'm making a point and click adventure/dating sim like website. I am currently making Dialogue Options. Options that come up at a certain point in conversation that allows the user to select their response, or chose some sort of prompt option.
My entire system seems to work fine except one step where I'm populating the Dialogue Options onto the prompt.
//Run through up to 10 dialogue options and add to dialogue option window.
for(var i = 1; i<3; i++)
{
var Option = eval(O['o'+i]);
if(typeof(Option) !== 'undefined')
{
$("#CAM_OptionsFrame").append("<p id='DialogueO"+i+"' class='clickable'>"+Option[0]+"</p>");
var funcString = Option[1];
var func = window[funcString];
$('#DialogueO'+i).on("click",function() {
$( this ).off("click");
endDialogueOption(time, O.noHide, function(){func();});
});
}
}
I load any number of Dialogue Options and their partner functions from an argument. Then loop through them listing the name in the HTML of the prompt and giving them a unique ID.
Then I select the element by the unique ID and give it an onclick listener which refers to the function passed in by the specific option.
However, ALL the elements run the last options function when you click them. I suspect the listener points to the "func" variable and updates every time I change it in the loop.
How do I make the listener refer to the instance of the variable so that every new listener I make refers to it's own "func" variable?