Changing an Object in a List[SOLVED]

Io
21 Mar 2018, 21:37

So let's say I have an Object with a stringlist as an attribute, called GenericStringList. I set GenericStringList to have the items ListObject1, ListObject2, and ListObject3.

Now, I know how to add things to lists, and I know how to call them. But I was looking through quest's List documentation and I couldn't see anything about changing an object already in a list. So, say, if I wanted to, during the game, replace "ListObject2" with "Fresh Apple", what is the syntax I would use for that?


Doctor Agon
21 Mar 2018, 21:55

Is the original Object2 still an apple? Either bad? bruised? or otherwise?
If so the simplest solution is to change the objects alias. Object2.alias = "A fresh apple."
The other alternative is List remove and List add, I don't think Quest has a List replace function.


Io
21 Mar 2018, 21:58

No, no actual objects. Poor wording on my part.

So Object.GenericStringList contains "ListString1"," ListString2", "ListString3". I want to change "ListString2" to "Fresh Apple" ingame.

Really, no list replace? That's a bummer. I mean, I can in theory do it with creative use of add and remove, but it'd be a hell of a headache.


mrangel
22 Mar 2018, 00:40

Hmm...

GenericStringList[1] = "Fresh Apple"

No idea if that works; I know that bracket notation for lists works in some situations, but haven't actually used it.
(i'm pretty sure it won't be as flexible as Perl, where you can do stuff equivalent to somelist[1,2,6] = anotherlist[3,2,1] ... but you might be able to use a list item as an lvalue)

If not, then make a function…

<function name="ListReplace" parameters="list, oldvalue, newvalue" type="list">
  newlist = NewList()
  foreach (item, list) {
    if (item = oldvalue) {
      list add (newlist, newvalue)
    }
    else {
      list add (newlist, item)
    }
  }
  return (newlist)
</function>

So then you could do: Object.GenericStringList = ListReplace (Object.GenericStringList, "ListString2", "A rotten apple")

Or if you want to edit the list in place (I think there's places that's necessary), the function would be a bit larger:

<function name="ListReplace" parameters="list, oldvalue, newvalue">
  newlist = NewList()
  foreach (item, list) {
    if (item = oldvalue) {
      list add (newlist, newvalue)
    }
    else {
      list add (newlist, item)
    }
  }
  while (ListCount(list) > 0) {
    list remove (list, list[0])
  }
  foreach (item, newlist) {
    list add (list, item)
  }
</function>

Io
22 Mar 2018, 02:07

Hmm, that seems a bit much, at least for what I'm doing; I'm entirely unfamiliar with setting parameters in functions, and I've just been using them as places to dump large amounts of code that I expect to call multiple times.

In any case, I /did/ find a solution for my specific problem with 3 more variables, but thanks anyway!