Wintermute Engine Forum
Wintermute Engine => Technical forum => Topic started by: greg on October 29, 2006, 03:35:15 PM
-
I'm trying to track what dialogue lines a character has said (and how many times he has said them).
To do this, my first thought was to use a hash table, a la:
// stringId is the ID of the dialogue line in the string table (in the format X_##_##_##, which is [ActorID]_[RoomID]_[ObjectID]_[LineId])
if (actor.dialogue[stringID]) {
actor.dialogue[stringID] = actor.dialogue[stringID] + 1;
} else {
actor.dialogue[stringID] = 1;
}
Since WME doesn't have hash tables but instead approximates them with the variable.subvariable format, my next thought was to do something like:
var lineState = actor.dialogue;
if (lineState.stringId) {
lineState.stringId = lineState.stringId + 1;
} else {
lineState.stringId = 1;
}
This wouldn't work though, because lineState.stringId would be treated as "lineState.stringId" rather than being resolved to lineState.X_##_##_##.
I suppose I could create a hash function which turns the stringId (X_##_##_##) into an integer (######) and then use that integer to index into an array of length maximumPossibleNumberOfLinesGivenStringIdFormat (######, which would be, egad, 1,000,000).
Is there a better--or more memory efficient--way of tracking which lines have been said, though?
Thanks for your help!
Greg
-
Main question:
For global variable x and w = x.y.z (or w = x.y[z]), how can I edit w so that future references to x.y.z (or x.y[z]) (from different scripts, etc) will reflect the updated w?
Explanation:
As a corrolary to the previous post, I'm now trying this method:
// if actor.dialogue[stringId] is true, then the actor has said the line corresponding to stringId.
// in the following code, "this" refers to the actor who's speaking
var string_state = this.dialogue; // because wme doesn't like this.dialogue[stringId]
method TalkOnce(stringId) {
var string_state = this.dialogue;
if (!string_state[stringId]) {
string_state[stringId] = true;
this.Talk(stringId);
}
}
However, even though I'm setting actor.dialogue[stringId] to true, the next time I call the method, actor.dialogue[stringId] has reverted to its previous null value. I suspect this is a pass-by-reference/pass-by-value problem. Namely, when I update string_state, I'm updating a copy of actor.dialogue (pass by value) rather than the actual actor.dialogue (pass by reference). Is this true? If so...
For global variable x and w = x.y.z (or w = x.y[z]), how can I edit w so that future references to x.y.z (or x.y[z]) (from different scripts, etc) will reflect the updated w?
Thanks,
Greg
-
You'll need to assign the value back after modifying it.
var string_state = this.dialogue;
string_state[stringId] = true;
this.dialogue = string_state;