List Based on Integer Value

I am having an issue trying to do something that seems like it should be simple, and I suspect I'm probably making it too difficult.

I've created a Function that should randomly pull an object from all the children of one directory based on a certain criteria. However, I'm having trouble creating the list I want. Specifically, I'd like to create a list of all items in a directory with a Price attribute Integer greater than a certain value.

My code right now is as follows:

StuffList = GetAllChildObjects (Repositories)
TreasureList = FilterByNotAttribute (StuffList, "Price")
YourTreasure = PickOneObject (TreasureList)
RealTreasure = CloneObjectAndMoveHere (YourTreasure)
RealTreasure.parent = game.pov
msg ("You just dug up a " + YourTreasure.name + ". With a little cleaning, it should still be worth something.")

Right now, the code should be only creating TreasureList with items that have the "Price" attribute, but I'm getting some strange results, and it's not really what I'm looking to do anyway.

What I'd like to do is create TreasureList with only objects with a Price of 25 or higher. Like I said, I'm sure it's easy, just couldn't figure out the syntax.

Thank you in advance!


The built-in filter functions only work if you want to check for an exact value; for something like that, you'd probably want to to do your own loop checking the values. Something like:

TreasureList = NewObjectList()
foreach (object, GetAllChildObjects (Repositories)) {
  if (HasInt (object, "Price")) {
    if (object.Price >= 25) {
      list add (TreasureList, object)
    }
  }
}
RealTreasure = CloneObjectAndMove (PickOneObject (TreasureList), game.pov)
msg ("You just dug up " + GetDisplayName (RealTreasure) + ". With a little cleaning, it should still be worth something.")

Off the top of my head, think that's right. Can you follow how that code works?


Here are the various errors that code is throwing. As always, I really appreciate your help with this.

Error running script: Error compiling expression 'newobject': RootExpressionElement: Cannot convert type 'Object' to expression result of 'Element'
Error running script: Error evaluating expression 'Contains(game.pov, object)': Contains function expected object parameter but was passed 'null'
Error running script: Error evaluating expression 'HasString(obj, "alias")': HasString function expected object parameter but was passed 'null'
Error running script: Error evaluating expression 'not GetBoolean(obj, "usedefaultprefix")': GetBoolean function expected object parameter but was passed 'null'
You just dug up . With a little cleaning, it should still be worth something.

OK, that's weird… it looks like it's getting a null from somewhere.

The only way I can imagine that happening is if the list is empty.
Try this version:

TreasureList = NewObjectList()
foreach (object, GetAllChildObjects (Repositories)) {
  if (HasInt (object, "Price")) {
    if (object.Price >= 25) {
      list add (TreasureList, object)
    }
  }
}
if (ListCount (TreasureList) = 0) {
  msg ("There are no possible treasure items.")
}
else {
  RealTreasure = CloneObjectAndMove (PickOneObject (TreasureList), game.pov)
  msg ("You just dug up " + GetDisplayName (RealTreasure) + ". With a little cleaning, it should still be worth something.")
}

(note: I'm assuming that your Price attribute is an int (whole number). If it's a double, change HasInt to HasDouble)


Of course your code was correct, mrangel. Turns out my "Price" variable was actually not capitalized, so I was indeed building a list with nothing in it. Thank you as always for you indispensable help. This is what happens when I shelve a project for way too long. I'm rusty and making easy mistakes. Thanks again!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums