For the backgrounmd, see this thread:
viewtopic.php?f=18&t=3789What I am trying to do is set up a spell book; player types "CAST UNLOCK", and a spell from the spell book is cast. The problem is that spells in the spellbook are not in a convential scope, so I am trying to match text instead of an object.
<command name="Cast">
<pattern>cast #text#</pattern>
<script>
// stuff here
</script>
</command>
Jay pointed me to ResolveNameInternal, and that got me going, but when you get to disambiguation, it all goes weird. The problem is that the command completes without waiting for the menu to get a response.
But wait, that is just what "on ready" is for, right?
So here it is:
<command name="Cast">
<pattern>cast #text#</pattern>
<script><![CDATA[
value = LCase (text)
scope = GetDirectChildren (spellbook)
fullmatches = NewObjectList()
partialmatches = NewObjectList()
value = Trim(value)
foreach (obj, scope) {
name = LCase(GetDisplayAlias(obj))
CompareNames (name, value, obj, fullmatches, partialmatches)
if (obj.alt <> null) {
foreach (altname, obj.alt) {
CompareNames (LCase(altname), value, obj, fullmatches, partialmatches)
}
}
}
if (game.lastobjects <> null) {
foreach (obj, game.lastobjects) {
CompareNames (LCase(obj.article), value, obj, fullmatches, partialmatches)
CompareNames (LCase(obj.gender), value, obj, fullmatches, partialmatches)
}
}
if (ListCount(fullmatches) = 1) {
spellbook.spellpending = ListItem(fullmatches, 0)
}
else if (ListCount(fullmatches) = 0 and ListCount(partialmatches) = 1) {
spellbook.spellpending = ListItem(partialmatches, 0)
}
else if (ListCount(fullmatches) + ListCount(partialmatches) = 0) {
spellbook.spellpending = null
}
else {
menu = NewStringDictionary()
GenerateMenuChoices (menu, fullmatches)
GenerateMenuChoices (menu, partialmatches)
ShowMenu (DynamicTemplate("DisambiguateMenu", value), menu, true) {
if (result <> null) {
spellbook.spellpending = GetObject(result)
}
else {
spellbook.spellpending = null
}
}
}
on ready {
if (spellbook.spellpending = null) {
msg ("No such spell in your book")
}
else if (not HasScript (spellbook.spellpending, "castspell")) {
msg ("That's not a spell; it should not even be in your book")
}
else {
do (spellbook.spellpending, "castspell")
}
}
]]></script>
</command>
But it fails. The "on ready" bit does not wait for the menu to complete. Why is that?
Code here:
You will also need Jay's library file from the above thread.