evaluating which attribute is highest out of a group
ilta
29 Nov 2014, 09:57Hi all!
I've got a sticky programming question.
In my game, there's a scene where the player is in a car and can go in any of the four compass directions. Theoretically they could travel 15 times (either in one direction, or in some combination), though it's unlikely they will actually bother to go more than 3 before getting bored and doing something else.
Each time they travel in one direction, an attribute in the player object ticks up from 0:
drove_n
drove_e
drove_s
drove_w
Later on, I'd like to be able to report back to them which direction they traveled the most (ie which attribute is highest of the four). If they tie, that's fine, it could either report the tie ("you drove mostly north and south") or it could report "you didn't travel in any one particular direction".
Anyone have any ideas how to go about doing this? Is there a "compare values" function that could work for more than one variable at a time? I'm a little lost.
Thanks everyone!
- Isaiah
I've got a sticky programming question.
In my game, there's a scene where the player is in a car and can go in any of the four compass directions. Theoretically they could travel 15 times (either in one direction, or in some combination), though it's unlikely they will actually bother to go more than 3 before getting bored and doing something else.
Each time they travel in one direction, an attribute in the player object ticks up from 0:
drove_n
drove_e
drove_s
drove_w
Later on, I'd like to be able to report back to them which direction they traveled the most (ie which attribute is highest of the four). If they tie, that's fine, it could either report the tie ("you drove mostly north and south") or it could report "you didn't travel in any one particular direction".
Anyone have any ideas how to go about doing this? Is there a "compare values" function that could work for more than one variable at a time? I'm a little lost.
Thanks everyone!
- Isaiah

jaynabonne
29 Nov 2014, 10:47The way I would do it is to use four separate objects, each with a common attribute, rather than four attributes on a single object. For example, instead of:
I would use:
The reason for that is twofold:
1) You need to associate the direction with the value during the sort, so that when it's all sorted, you know which value corresponds to which object. So you'd need to combine the value and direction into some sort of structure anyway.
2) There is a function in Quest called ObjectListSort (or ObjectListSortDescending), which sorts a list of objects based on the value of an attribute.
[Note that this is an internal use of objects. These objects won't appear in any room or be accessible by the player in any way. They should live outside your rooms.]
Assuming you have the objects set up with their values tallied, you could have code like:
The attached game shows this in action. The counter object value attributes have hard-coded values so you can see the sort results without having to move around in a set of rooms. (North = 0, South = 10, West = 5, East = 3) It not only dumps out the highest but shows the sorted counters list as well.
I hope that helps!
<object name="player">
<northcounter type="int">0</northcounter>
<southcounter type="int">0</southcounter>
<westcounter type="int">0</westcounter>
<eastcounter type="int">0</eastcounter>
</object>
I would use:
<object name="northcounter">
<value type="int">0</value>
<object>
<object name="southcounter">
<value type="int">0</value>
<object>
<object name="westcounter">
<value type="int">0</value>
<object>
<object name="eastcounter">
<value type="int">0</value>
<object>
The reason for that is twofold:
1) You need to associate the direction with the value during the sort, so that when it's all sorted, you know which value corresponds to which object. So you'd need to combine the value and direction into some sort of structure anyway.
2) There is a function in Quest called ObjectListSort (or ObjectListSortDescending), which sorts a list of objects based on the value of an attribute.
[Note that this is an internal use of objects. These objects won't appear in any room or be accessible by the player in any way. They should live outside your rooms.]
Assuming you have the objects set up with their values tallied, you could have code like:
// Create a list of the counters objects.
counters = NewObjectList()
list add(counters, northcounter)
list add(counters, southcounter)
list add(counters, westcounter)
list add(counters, eastcounter)
// Sort the list
sortedcounters = ObjectListSortDescending(counters, "value")
// Grab the highest counters
switch (sortedcounters[0]) {
case (northcounter) {
msg("The north direction was the most.")
}
case (southcounter) {
msg("The south direction was the most.")
}
case (westcounter) {
msg("The west directxion was the most.")
}
case (eastcounter) {
msg("The east direction was the most.")
}
}
The attached game shows this in action. The counter object value attributes have hard-coded values so you can see the sort results without having to move around in a set of rooms. (North = 0, South = 10, West = 5, East = 3) It not only dumps out the highest but shows the sorted counters list as well.
I hope that helps!
ilta
29 Nov 2014, 11:26that is amazing. Thank you so much! I'll give it a try.
ilta
29 Nov 2014, 11:28related question:
if I want to output the number as well, what does that command look like? I assume it's similar to "msg("something something is equal to" object.value ".")" but I can't figure out the syntax quite right. Sorry I'm sure this is a noob question.
if I want to output the number as well, what does that command look like? I assume it's similar to "msg("something something is equal to" object.value ".")" but I can't figure out the syntax quite right. Sorry I'm sure this is a noob question.

jaynabonne
29 Nov 2014, 12:03Something like:
msg("You went north " + northcounter.value + " times.")
ilta
29 Nov 2014, 17:05thanks again!