How to address the alias attribute
MerchantToo
11 Feb 2012, 16:26In my game people can get security clearances to allow access to different parts of the spaceship. When the security device is examined, I want it to list the clearances that have been added to the device. So I wrote the following function to print out which clearances are present. There is an boolean attribute SecurityClearance on such clearance objects, and if it is set to true then the player has that security clearance. But here's my problem, the code shown below works fine, making a list of the name attributes of all present clearances. However, if I swap .name for .alias which is actually what I want to display, I get a error such as:
Error running script: Error compiling expression 'Printed + ObjectListItem(DisplayList,loop).alias + ", "': Unknown object or variable 'alias'
Any ideas? Thanks in advance.
Error running script: Error compiling expression 'Printed + ObjectListItem(DisplayList,loop).alias + ", "': Unknown object or variable 'alias'
Any ideas? Thanks in advance.
<function name="ListSecurityClearances">
Printed = "The following security clearances are currently uploaded to this device: "
NumClearance = 0
DisplayList = NewObjectList()
foreach (obj, game.SecurityClearanceList) {
if (obj.SecurityClearance) {
NumClearance = NumClearance + 1
list add (DisplayList, obj)
}
}
if (NumClearance = 0) {
Printed = Printed + "None."
}
else if (NumClearance = 1) {
Printed = Printed + ObjectListItem(DisplayList,0).name + "."
}
else {
for (loop, 0, NumClearance - 2, 1) {
Printed = Printed + ObjectListItem(DisplayList,loop).name + ", "
}
Printed = Left(Printed, LengthOf(Printed) - 2)
Printed = Printed + " and " + ObjectListItem(DisplayList,NumClearance - 1).name + "."
}
msg (Printed)
</function>
Alex
11 Feb 2012, 16:35Maybe one of your objects doesn't have an alias?
You could use the GetDisplayAlias function as a safe alternative - it will return the alias if it exists, and the name if it doesn't.
You could use the GetDisplayAlias function as a safe alternative - it will return the alias if it exists, and the name if it doesn't.
MerchantToo
11 Feb 2012, 16:44Thanks that did the trick. I only have 2 security objects defined for my test, and they both had aliases. Strange that. 

MerchantToo
12 Feb 2012, 10:34I found another work-around. Maybe the interpreter has problems when you access an attribute from a object from a list? But anyhow the following also works:
<function name="ListSecurityClearances">
Printed = "The following security clearance are currently uploaded to this device: "
NumClearance = 0
DisplayList = NewObjectList()
foreach (obj, game.SecurityClearanceList) {
if (obj.SecurityClearance) {
NumClearance = NumClearance + 1
list add (DisplayList, obj)
}
}
if (NumClearance = 0) {
Printed = Printed + "None."
}
else if (NumClearance = 1) {
item = ObjectListItem(DisplayList,0)
Printed = Printed + item.alias+ "."
}
else {
for (loop, 0, NumClearance - 2, 1) {
item = ObjectListItem(DisplayList,loop)
Printed = Printed + item.alias+ ", "
}
Printed = Left(Printed, LengthOf(Printed) - 2)
item = ObjectListItem(DisplayList,NumClearance-1)
Printed = Printed + " and " + item.alias + "."
}
msg (Printed)
</function>