Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Héctor Bometón on Mon 31/01/2022 11:06:23

Title: [SOLVED] Function affecting player and any other character
Post by: Héctor Bometón on Mon 31/01/2022 11:06:23
After years of using AGS I'm still managing to come up with stupid questions so... here it goes:

Imagine I have a script for some birds in the background to start flying if you get near them (for example, 20px near), something like this:

Code (ags) Select
if (player.x >= cBird.x - 20 && player.x <= cBird.x + 20) { //HERE GOES THE ANIMATION CODE AND WHATNOT }

This is already working, but... What about if I want them to react to ANY character getting near? because there are some of them walking in the background.

I tried something like this, and also with the "*", as in "Character*", just because I've seen that before but the truth is I don't know what it means when you have the "*" next to something.
Code (ags) Select
if (Character.x >= cBird.x - 20 && Character.x <= cBird.x + 20) { //HERE GOES THE ANIMATION CODE AND WHATNOT }

Thank you very much!
Title: Re: Function affecting player and any other character
Post by: AndreasBlack on Mon 31/01/2022 11:10:31
Welcome to the stupid club. Can't you just add the character in the if statement? like cHector || cMax? I think it should work in theory, so it applies to whatever characters you have available. (roll)
Title: Re: Function affecting player and any other character
Post by: Héctor Bometón on Mon 31/01/2022 11:13:20
Quote from: AndreasBlack on Mon 31/01/2022 11:10:31
Welcome to the stupid club. Can't you just add the character in the if statement? like cHector || cMax? I think it should work in theory, so it applies to whatever characters you have available. (roll)


Well... Birds are going to be used in several rooms, and I'm adding more and more characters and randomizing their movements so... I wanted to make it work for any character.
Title: Re: Function affecting player and any other character
Post by: AndreasBlack on Mon 31/01/2022 11:22:39
Quote from: Héctor Bometón on Mon 31/01/2022 11:13:20
Quote from: AndreasBlack on Mon 31/01/2022 11:10:31
Welcome to the stupid club. Can't you just add the character in the if statement? like cHector || cMax? I think it should work in theory, so it applies to whatever characters you have available. (roll)


Well... Birds are going to be used in several rooms, and I'm adding more and more characters and randomizing their movements so... I wanted to make it work for any character.

Dumb question perhaps but do you put the code into a region "Walk onto region"? Perhaps that would solve it if you chose the "playable character" code. Or should NPC's be able to walk there aswell? 
Title: Re: Function affecting player and any other character
Post by: eri0o on Mon 31/01/2022 11:25:34
You could leverage the global arrays:

https://adventuregamestudio.github.io/ags-manual/GlobalArrays.html

Use character array with an i index in a for following what's mentioned above to iterate through all characters. If you have a range of character IDs you think are the interesting ones to react, select only the range. Also, make sure to ignore the bird characters.

More info on pointers (the * char) here:

https://adventuregamestudio.github.io/ags-manual/Pointers.html
Title: Re: Function affecting player and any other character
Post by: Héctor Bometón on Mon 31/01/2022 11:36:52

Quote from: eri0o on Mon 31/01/2022 11:25:34
Use character array with an i index in a for following what's mentioned above to iterate through all characters. If you have a range of character IDs you think are the interesting ones to react, select only the range. Also, make sure to ignore the bird characters.

Thank you for the answer. I can make a list of those character's IDs, but they are not consecutive and I've never used arrays before so I don't know how that would look like in the script.  :~( :~( :~(
Title: Re: Function affecting player and any other character
Post by: Héctor Bometón on Mon 31/01/2022 11:43:05


Here is a little clip on the birds. I simplyfied the function A LOT, but it's a pretty complex script and I was really hoping I only had to replace "player" for another word  :-D
Title: Re: Function affecting player and any other character
Post by: Laura Hunt on Mon 31/01/2022 12:17:19
Maybe something like:

Code (ags) Select
Character* birdscarers[20]; //or whatever the number of characters that can interact with birds ends up being

birdscarers[0] = cHector;
birdscarers[1] = cPedro;
birdscarers[2] = cMaria;
//etc etc etc


And then you would iterate over this array in your function, something like:

Code (ags) Select
for (int i = 0; i<20; i++) {
    if (birdscarers[i].x >= cBird.x - 20 && birdscarers[i].x <= cBird.x + 20) { //HERE GOES THE ANIMATION CODE AND WHATNOT }
}


I guess there could be issues if several characters start interfering with one another, but this could be a start. (Maybe create a state bool like "arebirdsflying" and prevent the function from triggering if it's true.)


EDIT:

Another way to do this could be with custom properties: simply create a custom boolean property for characters called "scaresbirds" for example, give it a default value of false, and set it to true only for the characters that you want to be able to interact with birds. Then the code would look like this:

Code (ags) Select
for (int i = 0; i < Game.CharacterCount; i++) {
    if (character[i].x >= cBird.x - 20 && character[i].x <= cBird.x + 20 && character[i].GetProperty("scaresbirds") == true) { //HERE GOES THE ANIMATION CODE AND WHATNOT }
}
Title: Re: Function affecting player and any other character
Post by: Héctor Bometón on Mon 31/01/2022 17:47:49

Thanks, Laura! Always helpful. Thanks everybody. I think I got it.
Title: Re: Function affecting player and any other character
Post by: AndreasBlack on Mon 31/01/2022 20:10:14
Quote from: Héctor Bometón on Mon 31/01/2022 17:47:49

Thanks, Laura! Always helpful. Thanks everybody. I think I got it.


Looks great btw! Keep it up!