Fish random swimming(SOLVED ty GarageGothic)

Started by proskiier, Thu 15/11/2007 18:25:48

Previous topic - Next topic

proskiier

Ok I have this map with a fountain with a statue in the middle, on it



I am going to make this fish objects... (one at a time(and i will separate/shrink them)) and I want them to swim around in the water randomly

when they move behind the statue it will be ok cause I can do walkbehinds in the room menu, but how do i get them to randomly move

I know i will have to do this with scripting object
  • .move but is there a way to do it randomly??

    and do I do this in the room script since I always want them moving(swimming) around or in that always changing script??

    Thanks a bunch for the help, and let me know if you need any leet drawings made for you, as you can see my skills are top notch. :)

BOYD1981

i'm no scripting expert so i'm not entirely sure if there is a way to randomly, although as is usually the case with AGS it probably is entirely possible.
but one way to script it without doing too much work would be to use the Character Control module here http://www.adventuregamestudio.co.uk/yabb/index.php?topic=28821.0 that way you can give multiple characters a simple set of destinations and have them repeat it endlessly (although the only thing i've ever used it for is trying to give the impression of a scrolling background through a window).

Limey Lizard, Waste Wizard!
01101101011000010110010001100101001000000111100101101111011101010010000001101100011011110110111101101011

GarageGothic

#2
Don't bother with the character control module, using that is overkill unless you also use it elsewhere in the game.

I would recommend first of all, to make the fish characters rather than objects. Then you can set different loops for them to avoid the fish swimming backwards. Also, make the water in the fountain a walkable area but make sure that it's blocked off from the surrounding walkable areas, so the player can't get into it. Also make it slightly smaller than the actual water surface, since character coordinates signify the center of the sprite, and you don't want half a fish poking through the edge of the fountain.

I assume here that you make them characters that are numbered in sequence (here 5 fish as characters 5-9). Even if you reuse the same art for multiple fish, they must be individual characters for it to work.

in the room script repeatedly_execute:

Code: ags
int fishnumber = 5; //the first fish in the sequence
while (fishnumber <= 9) { //this means that we run through all the fish before continuing the script
   if (character[fishnumber].Moving == false) { //don't bother if fish is already moving
       int destinationx;
       int destinationy;      
       while (GetWalkableAreaAt (destinationx, destinationy) != 3) { //we're gonna randomize some coordinates but must make sure they're in the water (replace 3 with number of walkable area)
          destinationx = 53 + Random(40); //Replace 53 with the leftmost x coordinate of the water area and 40 with the max width of the water area
          destinationy = 60 + Random(35); //Replace 60 with top y coordinate of water area and 35 with max height of water area
          }
      character[fishnumber].Walk(destinationx, destinationy); //make the fish swim to the randomized coordinate
      }
   fishnumber++; //move on to the next fishie
   }


Alternately, put in the room script repeatedly_execute_always, surrounded by:

Code: ags
if (IsGamePaused() == false) {
   //put the previous code here
   }


So that the fishies will swim even while blocking animations are executed, but not while the game is paused.

proskiier



thats the error i got... is it because It is not a square fountain and the fish cannot swim"walkable area" to the top and bottom corners of the square because there is grass??



here are screen shots of all my setups to make sure I did not goof something







Code:
// script for Room: Repeatedly execute
 
if (IsGamePaused() == false) {
int fishnumber = 2; //the first fish in the sequence
while (fishnumber <= 5) { //this means that we run through all the fish before continuing the script
   if (character[fishnumber].Moving == false) { //don't bother if fish is already moving
       int destinationx;
       int destinationy;     
       while (GetWalkableAreaAt (destinationx, destinationy) != 2) { //we're gonna randomize some coordinates but must make sure they're in the water (replace 3 with number of walkable area)
          destinationx = 123 + Random(84); //Replace 123 with the leftmost x coordinate of the water area and 84 with the max width of the water area
          destinationy = 102 + Random(57); //Replace 102 with top y coordinate of water area and 57 with max height of water area
          }
      character[fishnumber].Walk(destinationx, destinationy);
      }
   }
fishnumber++;
}
}


GarageGothic

#4
Which line is 19? I think the problem is that there's a bracket too many after the line:

Code: ags
   character[fishnumber].Walk(destinationx, destinationy);
   }
} //REMOVE THIS!
fishnumber++;


So that it never moves on to the next fish.

Edit: Incidentally, the whole point of redoing the randomizing until the spot is actually on the walkable area is exactly to get around the problem that the fountain isn't square. So we've got that covered. It's highly unlikely that it would ever go through 150001 iterations where the randomized coordinates are all outside the walkable area, but there's an infinitismally small chance of it happening. If you really want to be on the safe side, you could add a counter and supply a pre-set coordinate in that case, like this:

Code: ags
 int count;     
       while (GetWalkableAreaAt (destinationx, destinationy) != 2) { //we're gonna randomize some coordinates but must make sure they're in the water (replace 3 with number of walkable area)
          if (count > 100) { //if we've done 100 randomizations, screw it and just supply values that we know are inside the walkable area
             destinationx = 150;
             destinatyony = 130;
             }
          destinationx = 123 + Random(84); //Replace 123 with the leftmost x coordinate of the water area and 84 with the max width of the water area
          destinationy = 102 + Random(57); //Replace 102 with top y coordinate of water area and 57 with max height of water area
          count++;
          }
      character[fishnumber].Walk(destinationx, destinationy);
      }


proskiier

when i take out the bottom one it gives me those errors with the # sign


// room script file

#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for Room: Repeatedly execute
 
if (IsGamePaused() == false) {
int fishnumber = 2; //the first fish in the sequence
while (fishnumber <= 5) { //this means that we run through all the fish before continuing the script
   if (character[fishnumber].Moving == false) { //don't bother if fish is already moving
       int destinationx;
       int destinationy;     
       while (GetWalkableAreaAt (destinationx, destinationy) != 2) { //we're gonna randomize some coordinates but must make sure they're in the water (replace 3 with number of walkable area)
          destinationx = 123 + Random(84); //Replace 123 with the leftmost x coordinate of the water area and 84 with the max width of the water area
          destinationy = 102 + Random(57); //Replace 102 with top y coordinate of water area and 57 with max height of water area
          }
      character[fishnumber].Walk(destinationx, destinationy);
    }
    }
fishnumber++;
}
}
#sectionend room_a  // DO NOT EDIT OR REMOVE THIS LINE

GarageGothic

#6
Ah, sorry, my mistake. There should be one bracket more at the very end, and one less before "fishnumber++". Like so:

Code: ags
          character[fishnumber].Walk(destinationx, destinationy);
          }
      fishnumber++;
      }
   }
}
#sectionend room_a  // DO NOT EDIT OR REMOVE THIS LINE

proskiier

// room script file

#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for Room: Repeatedly execute
 
if (IsGamePaused() == false) {
int fishnumber = 2; //the first fish in the sequence
while (fishnumber <= 5) { //this means that we run through all the fish before continuing the script
   if (character[fishnumber].Moving == false) { //don't bother if fish is already moving
       int destinationx;
       int destinationy;     
       while (GetWalkableAreaAt (destinationx, destinationy) != 2) { //we're gonna randomize some coordinates but must make sure they're in the water (replace 3 with number of walkable area)
          destinationx = 123 + Random(84); //Replace 123 with the leftmost x coordinate of the water area and 84 with the max width of the water area
          destinationy = 102 + Random(57); //Replace 102 with top y coordinate of water area and 57 with max height of water area
          }
      character[fishnumber].Walk(destinationx, destinationy);
    }
fishnumber++;
}
}
}
}
#sectionend room_a  // DO NOT EDIT OR REMOVE THIS LINE


still getting the error

should i take one away at the way end????

GarageGothic

Yes, see my example that I put in the edit above.

proskiier

#9
doh I'm an idiot... i thought I did it perfectly like yours :X

my fish are swimming on the grass a bit... so I'm gonna hafta tweak the numbers a bit... but thank you so much for all of your help...


now I just have one last question, they look kinda dumb when they swim backwards lol (tail first)

anyway to flip them over when they switch directions???

prolly not right cause they are random ints??

GarageGothic

I'm glad you got it working. The fish swimming on the grass is probably because the character coordinates are in fact the bottom center of the character sprite, so you want to decrease the width of the walkable area a little and remove a fish's height of the top part of it (and adjust numbers accordingly, as you say).

proskiier

now I just have one last question, they look kinda dumb when they swim backwards lol (tail first)

anyway to flip them over when they switch directions???

prolly not right cause they are random ints??

GarageGothic

#12
Well, if you refer to my very first post in this thread, you'll see that I suggested using characters instead of objects in the first place, because it would make it easier to make them face the right direction. Since the code uses the regular walk routines that's also used for the player character, all you need is to set up the animation loops for the fish characters like you would set up a normal walkcycle.

Just go to your "Views"-page in the editor, select the fish view and add some more loops to it (for all four directions, but you just want to set them all to the same sprite unless you also draw front/back frames). Then, to make them face the other direction, you click the word NORMAL beneath the sprite and it changes to FLIPPED and you will have a loop of the fish swimming in another direction.

There's still a slight problem when the fish is swimming straight up/down, if you find it to cause a lot of trouble, I can supply you with some code to fix it by changing loops manually. But now that the code is finally working, it would probably be easiest to use the other method.

proskiier

#13
I did use characters instead of objects... so if i just add a second frame of them flipped, it will do all the work for me??


EDIT: itried to add a second flipped frame but now they just change frames when they stop moving (so they flip)

and when they move again they flip back... they dont flip depedning on what direction they are swiming....

ohh i think i know how to solve it

GarageGothic

Yup, that's what I was saying. Uncheck the "Diagonal Loops" box for the fish characters though, so you don't have to add so many loops.

Loop 1 is left, Loop 2 is right. You decide yourself how you want to set up Loop 0 and 3 (up/down), whether you just want to put in the left/right sprites or draw new ones.

proskiier

haha they look awesome now... i cant thank you enough :)

GarageGothic

Great, I'm looking forward till your game comes out so I can see it in action :)

SMF spam blocked by CleanTalk