dynamic object list

Started by zeta_san, Fri 14/10/2022 08:09:25

Previous topic - Next topic

zeta_san

hi boys

I would like to do one thing, I need a dynamic object list

when the object is visible (true) it appears creating a list

example

if only 1 and 2 are true
the list will be

1
2

if only 5 and 7 are true
the list will be

5
7

I hope I was clear
thank you

Khris

Depending on the use case you can use an array or a set for this. A Set is easier because it already has methods like .Contains(), however you can only add Strings to it so you would need to convert to a string to store a number and convert it back after reading it from the set.

Here's extender functions that allow storing numbers instead:
Code: ags
bool AddNumber(this Set*, int number) {
  return this.Add(String.Format("%d", number));
}

bool ContainsNumber(this Set*, int number) {
  return this.Contains(String.Format("%d", number));  
}

bool RemoveNumber(this Set*, int number) {
  return this.Remove(String.Format("%d", number));  
}

zeta_san

hi khris

in this case?



Code: ags
if (destinationArtena == true) {
  oTartena.Visible = true;
} else {
  oTartena.Visible = false;
}

if (destinationMilano == true) {
  oTmilano.Visible = true;
} else {
  oTmilano.Visible = false;
}

if (destinationRoma == true) {
  oTroma.Visible = true;
} else {
  oTroma.Visible = false;
}

ecc..

Khris

#3
You can shorten that code considerably:
Code: ags
  oTartena.Visible = destinationArtena;
  oTmilano.Visible = destinationMilano;
  // etc.

Not sure if you need a set then, but I'm still not sure what you're trying to do exactly.
What do you mean by "creating a list"? Do you want the objects to appear below each other?

zeta_san


Khris

Ok, so one way is this:
Code: ags
function UpdateDestinations() {
  // topmost position
  int y = 123;
  Object* o;
  // object IDs 0 - 7
  for (int i = 0; i < 8; i++) {
    o = object[i];
    if (o.Visible) {
      o.Y = y;
      y += Game.SpriteHeight[o.Graphic];
    }
  }
}

zeta_san

only this? nothing else ?

Khris

I'm sorry but this is getting really frustrating. Can you please explain properly what you're trying to do? I'm just guessing wildly here, trying to assemble a puzzle where half the pieces are missing.
You're talking about a list and you seem to have a bunch of objects which represent destinations but there's no context or useful code whatsoever.

The function I posted above will arrange the visible objects of indices 0 to 7 in a stack at the given y coordinate, so if that's what/all you need, then yes, only this. But I have no way to tell.

zeta_san


SMF spam blocked by CleanTalk