[SOLVED] Function affecting player and any other character

Started by Héctor Bometón, Mon 31/01/2022 11:06:23

Previous topic - Next topic

Héctor Bometón

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
 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
 if (Character.x >= cBird.x - 20 && Character.x <= cBird.x + 20) { //HERE GOES THE ANIMATION CODE AND WHATNOT } 


Thank you very much!

AndreasBlack

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)

Héctor Bometón

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.

AndreasBlack

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? 

eri0o

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

Héctor Bometón


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.  :~( :~( :~(

Héctor Bometón



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

Laura Hunt

#7
Maybe something like:

Code: ags
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
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
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 } 
}

Héctor Bometón


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

AndreasBlack

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!

SMF spam blocked by CleanTalk