Incorrect Object Type Processing

homeeman
03 Apr 2013, 00:36
I made a variant of ShowMenu that works with objects:


outputsection = StartNewOutputSection()
msg (caption)
count = 0
game.menuoptionskeys = NewStringList()
foreach (option, options) {
list add (game.menuoptionskeys, option)
count = count + 1
target = ObjectDictionaryItem(options, option)
optionText = target.alias
if (target.cover) {
OutputTextNoBr (count + ": <a class=\"cmdlink\" style=\"" + GetCurrentLinkTextFormat() + "\" onclick=\"ASLEvent('ShowMenuResponse','" + option + "')\">" + optionText + "</a>")
SetForegroundColour ("Red")
msg (" (behind cover)")
SetForegroundColour ("Black")
}
else if (target.defend) {
OutputTextNoBr (count + ": <a class=\"cmdlink\" style=\"" + GetCurrentLinkTextFormat() + "\" onclick=\"ASLEvent('ShowMenuResponse','" + option + "')\">" + optionText + "</a>")
SetForegroundColour ("Red")
msg (" (defending)")
SetForegroundColour ("Black")
}
else {
OutputTextNoBr (count + ": <a class=\"cmdlink\" style=\"" + GetCurrentLinkTextFormat() + "\" onclick=\"ASLEvent('ShowMenuResponse','" + option + "')\">" + optionText + "</a>")
}
}
EndOutputSection (outputsection)
game.menuoptions = options
game.menuallowcancel = allowCancel
game.menucallback = callback
game.menuoutputsection = outputsection


As you can see, it has also been adapted to add text next to an option when it is behind cover or defending. When it tests the flags for these states (if (target.cover)) it appears to find the correct state (if I make the cover boolean true, it prints "(behind cover)" next to the option; if I make the defend boolean true, it also responds correctly), however, just after printing the output correctly, it gives me an error message saying that it's trying to read target.cover as an object, and that it can't read it like a boolean. (Error running script: Error compiling expression 'target.cover': RootExpressionElement: Cannot convert type 'Object' to expression result of 'Boolean)
I'm confused, because clearly it's processing the boolean statements--all the way to their conclusion, as I can make every case possible print correctly. But after it finished processing the statements, it doubles back and says, "Oh wait, I can't do that, target.cover is an object." I'm not sure how to proceed in this situation. I appear to be right and wrong at the same time.

As a side note: I'd like to avoid posting my code file at the moment, but if this merits further investigation, I can do that.

jaynabonne
03 Apr 2013, 08:37
You will get that message if "target.cover" does not exist as an attribute. Can you change:

if (target.cover) {

to

if (GetBoolean(target, "cover") {

I don't know what's in your object array, so it's hard to tell exactly what's going on. If this persists, a small complete sample test case would be handy.

homeeman
03 Apr 2013, 15:16
I must have turned my brain off. I made a dummy object so that I could include "Go back" as an option, and I didn't give it any attributes.
And I never would have found it if I hadn't started putting together a test file in my head. Thanks again, jaynabonne!