How to control many characters at once?

Started by RootBound, Sun 09/07/2023 22:15:47

Previous topic - Next topic

RootBound

Hey all,

I searched the forums and couldn't find an answer to this.

Let's say, for example, that I have a zombie game with 50 zombies in a room, and when the player makes a noise, all the zombies simultaneously move toward the player.

Is there any way to do this more efficiently than using cZombie.Move 50 times, once for each zombie?

I feel like if I could access the character.ID integer array, I could do it easily with a "for" statement, but there doesn't seem to be a way to do that anymore since the script now uses name pointers instead of ID numbers.

I could put all the character name pointers into an array and assign them all a number that way, but that seems superfluous if the character.ID integer array already exists.

What are my options here?

Thanks very much. :)
J. They/them. Here are my most recent games:

Crimson Wizard

#1
QuoteI feel like if I could access the character.ID integer array, I could do it easily with a "for" statement, but there doesn't seem to be a way to do that anymore since the script now uses name pointers instead of ID numbers.

This is a misconception, the script does not use "name pointers", it uses pointers, which may be stored in named variables (either generated by the editor, or declared by you), as well as in arrays.

There's a global array for each precreated object type in AGS. For characters this is "character[]" array. But you may create your own, if you need to only store particular objects there.

Some random examples:
Code: ags
for (int i = 0; i < 10; i++)
    character[i].Move(...)

Character* someCharacter = character[5];
Character* anotherCharacter = cEgo;

Character* enemies[10];
enemies[0] = cZombie1;
enemies[1] = cZombie2;
enemies[2] = cZombie3;
...
for (int i = 0; i < 10; i++)
    enemies[i].Move(...)

In the context of using placeholder characters as enemies, I may mention a ObjectPool module which I wrote for keeping track of used and free objects: https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/module-objectpool-keep-record-of-reusable-objects/

RootBound

This is a fantastically helpful answer! Thank you. ;-D 

From reading the manual all I could find was character.ID, and the only example given there was interactions with deprecated script functions, which made it sound like the number array was obsolete.

I'm glad that's not the case, as this makes several things I'm trying to do a whole lot simpler!

I'll have to take a look at your ObjectPool module too. That sounds like it has a lot of possible uses.

Huge thank you!

J. They/them. Here are my most recent games:

Crimson Wizard

#3
Quote from: RootBound on Mon 10/07/2023 03:29:32From reading the manual all I could find was character.ID, and the only example given there was interactions with deprecated script functions, which made it sound like the number array was obsolete.

What do you call a "number array" though?
"character[]" is not a array of numbers, it's array of Character* pointers.

The manual mentions these with example here:
https://adventuregamestudio.github.io/ags-manual/GlobalArrays.html

RootBound

Yes, sorry, I'm not as good with the technical language at this level. Perhaps I should have said the "number pointers"? I mean each character's ID number as generated by the editor.
J. They/them. Here are my most recent games:

Snarky

The ID property is the array index of the character[] array. Meaning that if cSnarky.ID == 5, then a pointer to cSnarky is stored in position 5 in the character[] array, so that it can also be accessed as character[5].

SMF spam blocked by CleanTalk