Jibble

Author Topic: It must be something I'm doing wrong, although I don't know what. [SOLVED].  (Read 1703 times)

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Peeps.

I have a problem, anyone who knows me would agree.(laugh)
This, however, is a Scripting problem.

In one room I have a number of creatures, on different levels, that walk from left to right and back again, repeatedly.
If any one of these creatures touches the 'Player Character' then a point is deducted from a 'Meter' until the meter is empty.
This is where the player runs away screaming, in theory.

I have created a 'Sanity Monitor' using a GUI and a 'Button'.
The buttons image is the meter and is set to 'Clip Image'

I have created a 'Global Variable' called 'number_of_hearts' an INT set to 218.
In 'Global Script ash' I have...
Code: Adventure Game Studio
  1. import function UpdateHeartGUI(int change);
At the very top of 'Global Script asc' I have...
Code: Adventure Game Studio
  1. function UpdateHeartGUI(int change) {
  2. number_of_hearts += change;
  3. if (number_of_hearts < 0) number_of_hearts = 0;
  4. if (number_of_hearts > 218) number_of_hearts = 218;
  5. btnHearts.Width = number_of_hearts * 1;
  6. return number_of_hearts;
  7. }
In my 'Room Script' I have...
Code: Adventure Game Studio
  1. // room script file
  2.  
  3. function room_FirstLoad() {
  4.   aWelcome.Play();
  5. }
  6.  
  7. function room_Load() {
  8.   // This disables MOUSE controls.
  9.   //------------------------------
  10.   mouse.Visible = false;
  11.   cDan.FaceLocation(0, 360);
  12.   aMystery.Play();
  13. }
  14.  
  15. function room_RepExec() {
  16.   // SCARE SCRIPT.
  17.   //---------------------------------------------------
  18.   // If the GHOSTS or other CREATURES collide with EGO,
  19.   // they SCARE him.
  20.  
  21.   if (AreThingsOverlapping(ZOMB, DAN)) {
  22.     aScream2.Play();
  23.     if (UpdateHeartGUI(-2) == 0){
  24.       QuitGame(0);
  25.     }
  26.   }
  27.  
  28.   if (AreThingsOverlapping(ZOMB2, DAN)) {
  29.     aScream2.Play();
  30.     if (UpdateHeartGUI(-2) == 0){
  31.       QuitGame(0);
  32.     }
  33.   }
  34.  
  35.   if (AreThingsOverlapping(ZOMB3, DAN)) {
  36.     aScream2.Play();
  37.     if (UpdateHeartGUI(-2) == 0){
  38.       QuitGame(0);
  39.     }
  40.   }
  41.  
  42.   if (AreThingsOverlapping(GHOST, DAN)) {
  43.     aGhostly1.Play();
  44.     if (UpdateHeartGUI(-1) == 0){
  45.       QuitGame(0);
  46.     }
  47.   }
  48.  
  49.   if (AreThingsOverlapping(GHOST2, DAN)) {
  50.     aGhostly1.Play();
  51.     if (UpdateHeartGUI(-1) == 0){
  52.       QuitGame(0);
  53.     }
  54.   }
  55.   // END OF SCARE SCRIPT.
  56.   //---------------------
  57.  
  58.   // This section makes GHOSTS and other CREATURES move.
  59.   //----------------------------------------------------
  60.   if (!cZomb.Moving) {
  61.     if (cZomb.x < 100) {
  62.       // If the Ghost is on the left hand side of the screen,
  63.       // start it moving towards the right.
  64.       cZomb.Walk(600, cZomb.y, eNoBlock, eAnywhere);
  65.       }
  66.       else {
  67.         // Otherwise, move it towards the left
  68.         cZomb.Walk(34, cZomb.y, eNoBlock, eAnywhere);
  69.       }
  70.   }
  71.  
  72.   if (!cZomb2.Moving) {
  73.     if (cZomb2.x < 100) {
  74.       // If the Ghost is on the left hand side of the screen,
  75.       // start it moving towards the right.
  76.       cZomb2.Walk(605, cZomb2.y, eNoBlock, eAnywhere);
  77.       }
  78.       else {
  79.         // Otherwise, move it towards the left.
  80.         cZomb2.Walk(34, cZomb2.y, eNoBlock, eAnywhere);
  81.       }
  82.   }
  83.  
  84.   if (!cZomb3.Moving) {
  85.     if (cZomb3.x < 100) {
  86.       // If the Ghost is on the left hand side of the screen,
  87.       // start it moving towards the right.
  88.       cZomb3.Walk(600, cZomb3.y, eNoBlock, eAnywhere);
  89.       }
  90.       else {
  91.         // Otherwise, move it towards the left.
  92.         cZomb3.Walk(34, cZomb3.y, eNoBlock, eAnywhere);
  93.       }
  94.   }
  95.  
  96.   if (!cGhost.Moving) {
  97.     if (cGhost.x < 100) {
  98.       // If the Ghost is on the left hand side of the screen,
  99.       // start it moving towards the right.
  100.       cGhost.Walk(582, cGhost.y, eNoBlock, eAnywhere);
  101.       }
  102.       else {
  103.         // Otherwise, move it towards the left
  104.         cGhost.Walk(34, cGhost.y, eNoBlock, eAnywhere);
  105.       }
  106.   }
  107.  
  108.   if (!cGhost2.Moving) {
  109.     if (cGhost2.x < 100) {
  110.       // If the Ghost is on the left hand side of the screen,
  111.       // start it moving towards the right.
  112.       cGhost2.Walk(600, cGhost2.y, eNoBlock, eAnywhere);
  113.       }
  114.       else {
  115.         // Otherwise, move it towards the left.
  116.         cGhost2.Walk(34, cGhost2.y, eNoBlock, eAnywhere);
  117.       }
  118.   }
  119. }
  120.   // END OF GHOST AND CREATURE MOVEMENTS SCRIPT.
  121.   //--------------------------------------------

This works just fine... However.
The audio files that I have within my room script play over and over again.
Obviously this is because they are within the 'Repeatedly Execute' section.
I don't know how to cure this.

I have tried many ways of getting the sound files to play outside of 'Repeatedly Execute'.
My game Compiles but no sound is played when the character collides with an NPC.

If anyone can see my error or even a better way to do what I am trying to do then please help.
I have been trying to do this for the past week now, without help, and have got nowhere.

Yours.
Jay.   
« Last Edit: 26 May 2014, 03:08 by RetroJay »
  Master Decorator

Ghost

  • AGS Baker
  • Posts: 1,726
  • Nerf on!
    • Ghost worked on a game that won an AGS Award!
    •  
    • Ghost worked on a game that was nominated for an AGS Award!
The problem is in the "IsThingsOverlapping". As long as they are, the sound is started over and over again.

The most basic solution involves the new audio system. I suggest you create an audio channel, and test against it using IsSoundPlaying. That way, you can play the sound only if its channel is currently not playing.

This can surely be fine-tuned even more but the basic idea is sound.

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Ghost.

Thanks.
I will try that, if I can work out what you mean, Tomorrow or Today, as it is now.
Please forgive me. My Brain hurts.???

When I'm firing on all eight cylinders I will let you know how things went.

But the rest of what I have done looks ok, Yes?
Or can I accomplish what I am after a better way?

Yours.
Jay.
  Master Decorator

Slasher

  • Posts: 2,756
  • slasher
    • I can help with AGS tutoring
    • Lifetime Achievement Award Winner
    • I can help with scripting
    • I can help with story design
    • Slasher worked on a game that was nominated for an AGS Award!
Hi,

Do you mean:

If the player gets too close to a creature a point is deducted, a sound plays and he runs away?

I had a similar thing and I did this:

1: I used the PPCollision Module for if player collides with something.
2; Made a global bool (in global pane) and set to false and add it to your PPCollision script (in room rep exec).
3: When collision happens set bool to true and use if bool is true
4: Add events after touching.
5: set bool back to false
6: If sound does not stop make it stop after running away with the Stop() command.

Something along those lines is an alternative method.

Edit:

I also have the dreaded Zombies, copycat (laugh)

I used AddWaypoint (they ignore Walkable areas though) to have Zombies patrol back and forth:

Code: Adventure Game Studio
  1. function repeatedly_execute_always()
  2. {
  3.   if (cZombie1.walking == false && cZombie1.View==111){
  4.   cZombie1.AddWaypoint(488, 1892); //First place to walk to x y
  5.   cZombie1.AddWaypoint(600, 1892); //Second place to walk to x y
  6.  //This will keep repeating back and forth non blocking with game.
  7. }
  8. }

I don't use if characters collide in my game but if I did it may be like this:

Code: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   if (PPColliding.CWithC(cCharles, cZombie1)){ // The 2 characters that if collide.
  4.   aScream.Play(); // Sound to play whilst running away
  5.   cCharles.SayBackground("Aghhhhhhhhhhhhhhhhhhhhhhh"); // SayBackground so non blocking.
  6.   cCharles.Walk(cCharles.x-200, cCharles.y, eBlock, eWalkableAreas); Runs away to x y position blocking.
  7.   cCharles.SayBackground(""); // Deletes the previous SayBackground.
  8.   aScream.Stop(); // Stops sound if required.
  9.   // Of course you may also subtract a point after collision.
  10. }  
  11. }
  12.  

Anyhow, this may help.....



« Last Edit: 23 May 2014, 20:29 by slasher »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Ghost.

Sorry, but after hours of tinkering with Audio Channels the sounds still plays over and over.
Although it must be said that I don't really understand what you mean. :-[

Thanks for your help, Though.

Yours.
Jay.

  Master Decorator

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Slasher.

After looking at the Script you showed, it got me thinking.
Messing around with your script I finally ended up with this...
Code: Adventure Game Studio
  1. function room_RepExec() {
  2.  
  3.   // SCARE SCRIPT.
  4.   //---------------------------------------------------
  5.   // If the GHOSTS or other CREATURES collide with EGO,
  6.   // they SCARE him.
  7.   if (AreThingsOverlapping(ZOMB, DAN)) {
  8.     UpdateHeartGUI(-40);
  9.     aScream.Play();
  10.     cDan.StopMoving();
  11.     Wait(40);
  12.     aScream.Stop();
  13.    
  14.     if (UpdateHeartGUI(-0) == 0){
  15.       QuitGame(0); // More stuff happens before Quit
  16.     }
  17.   }
  18.  
  19.   if (AreThingsOverlapping(ZOMB2, DAN)) {
  20.     UpdateHeartGUI(-40);
  21.     aScream.Play();
  22.     cDan.StopMoving();
  23.     Wait(40);
  24.     aScream.Stop();
  25.    
  26.     if (UpdateHeartGUI(-0) == 0){
  27.       QuitGame(0); // More stuff happens before Quit
  28.     }
  29.   }
  30.  
  31.   if (AreThingsOverlapping(ZOMB3, DAN)) {
  32.     UpdateHeartGUI(-40);
  33.     aScream.Play();
  34.     cDan.StopMoving();
  35.     Wait(40);
  36.     aScream.Stop();
  37.    
  38.     if (UpdateHeartGUI(-0) == 0){
  39.       QuitGame(0); // More stuff happens before Quit
  40.     }
  41.   }
  42.  
  43.   if (AreThingsOverlapping(GHOST, DAN)) {
  44.     UpdateHeartGUI(-20);
  45.     aGhostly.Play();
  46.     cDan.StopMoving();
  47.     Wait(40);
  48.     aScream.Stop();
  49.    
  50.     if (UpdateHeartGUI(-0) == 0){
  51.       QuitGame(0); // More stuff happens before Quit
  52.     }
  53.   }
  54.  
  55.   if (AreThingsOverlapping(GHOST2, DAN)) {
  56.     UpdateHeartGUI(-20);
  57.     aGhostly.Play();
  58.     cDan.StopMoving();
  59.     Wait(40);
  60.     aScream.Stop();
  61.    
  62.     if (UpdateHeartGUI(-0) == 0){
  63.       QuitGame(0); // More stuff happens before Quit
  64.     }
  65.   }
  66. }
  67.   // END OF SCARE SCRIPT.
  68.   //---------------------
  69.  
  70. function repeatedly_execute_always() {
  71.     // This section makes GHOSTS and other CREATURES move.
  72.   //----------------------------------------------------
  73.   if (!cZomb.Moving) {
  74.     if (cZomb.x < 100) {
  75.       // If the Ghost is on the left hand side of the screen,
  76.       // start it moving towards the right.
  77.       cZomb.Walk(600, cZomb.y, eNoBlock, eAnywhere);
  78.       }
  79.       else {
  80.         // Otherwise, move it towards the left
  81.         cZomb.Walk(34, cZomb.y, eNoBlock, eAnywhere);
  82.       }
  83.   }
  84.  
  85.   if (!cZomb2.Moving) {
  86.     if (cZomb2.x < 100) {
  87.       // If the Ghost is on the left hand side of the screen,
  88.       // start it moving towards the right.
  89.       cZomb2.Walk(605, cZomb2.y, eNoBlock, eAnywhere);
  90.       }
  91.       else {
  92.         // Otherwise, move it towards the left.
  93.         cZomb2.Walk(34, cZomb2.y, eNoBlock, eAnywhere);
  94.       }
  95.   }
  96.  
  97.   if (!cZomb3.Moving) {
  98.     if (cZomb3.x < 100) {
  99.       // If the Ghost is on the left hand side of the screen,
  100.       // start it moving towards the right.
  101.       cZomb3.Walk(600, cZomb3.y, eNoBlock, eAnywhere);
  102.       }
  103.       else {
  104.         // Otherwise, move it towards the left.
  105.         cZomb3.Walk(34, cZomb3.y, eNoBlock, eAnywhere);
  106.       }
  107.   }
  108.  
  109.   if (!cGhost.Moving) {
  110.     if (cGhost.x < 100) {
  111.       // If the Ghost is on the left hand side of the screen,
  112.       // start it moving towards the right.
  113.       cGhost.Walk(582, cGhost.y, eNoBlock, eAnywhere);
  114.       }
  115.       else {
  116.         // Otherwise, move it towards the left
  117.         cGhost.Walk(34, cGhost.y, eNoBlock, eAnywhere);
  118.       }
  119.   }
  120.  
  121.   if (!cGhost2.Moving) {
  122.     if (cGhost2.x < 100) {
  123.       // If the Ghost is on the left hand side of the screen,
  124.       // start it moving towards the right.
  125.       cGhost2.Walk(600, cGhost2.y, eNoBlock, eAnywhere);
  126.       }
  127.       else {
  128.         // Otherwise, move it towards the left.
  129.         cGhost2.Walk(34, cGhost2.y, eNoBlock, eAnywhere);
  130.       }
  131.   }
  132. }
  133.   // END OF GHOST AND CREATURE MOVEMENTS SCRIPT.
  134.   //--------------------------------------------

Now when a Ghost or other such nasty beastie collides with character he stops and screams, Only once, the meter goes down and then you continue along your merry way.
This may not be the prettiest way or even the correct way but it seems to work, to a fashion.

If anyone knows of a better way then please enlighten me.:)

Thank you Slasher for giving me the Idea, much appreciated.

Yours.
Jay.
  Master Decorator

Ghost

  • AGS Baker
  • Posts: 1,726
  • Nerf on!
    • Ghost worked on a game that won an AGS Award!
    •  
    • Ghost worked on a game that was nominated for an AGS Award!
I don't know any details about your game, but the multiple Wait(40) commands sound like a bad idea. You're blocking the whole game for a second there, and that can seriously upset user input if a collision repeats or if there are many characters on screen. I think nobody would like to play a sequence that relies on "speedy control" and have the control taken away regularly. Imagine playing Super Mario, and each time a coin jingle play, the game freezes. ;)

__
A very hacky idea. Use an attack animation. Start playing that once a collision happens. Assign the attack sound to a frame in the animation (in the view editor). You will still need to take care that the animation is not started over and over again, but you avoid the whole sound issue.
« Last Edit: 24 May 2014, 00:10 by Ghost »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Ghost.

Believe me, I'm not happy about the whole thing.;)
This was only supposed to be a simple game to take my mind off of my main project.
And now it's developed into a large pain in my butt.(laugh)

Mind you. That Idea you have had, regarding the attack sound on a frame, might just work.
I will continue tinkering and let you know how it goes.

Thank you for your help.
Jay. 
  Master Decorator

Khris

  • Posts: 10,676
  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on a game that was nominated for an AGS Award!
There are two ways to approach this:

1. only play a sound when there's currently no sound playing (will keep playing the sound during overlap, but in sequence)
2. only play the next sound after the player has not overlapped another character in the meantime (will only play the sound once)


Code: Adventure Game Studio
  1. bool CheckEnemy(int id, int hploss) {
  2.   bool r = AreThingsOverlapping(id, DAN);
  3.   if (r && UpdateHeartGUI(hploss) == 0) QuitGame(0);
  4.   return r;
  5. }
  6.  
  7. bool was_colliding;
  8. AudioClip *collision_sound;
  9. AudioChannel *collision;
  10.  
  11. function room_RepExec() {
  12.  
  13.   bool is_colliding = false;
  14.  
  15.   if (CheckEnemy(ZOMB, -2) || CheckEnemy(ZOMB2, -2) || CheckEnemy(ZOMB3, -2)) {
  16.     collision_sound = aScream2;
  17.     is_colliding = true;
  18.   }
  19.   if (CheckEnemy(GHOST, -1) || CheckEnemy(GHOST2, -1)) {
  20.     collision_sound = aGhostly1;
  21.     is_colliding = true;
  22.   }
  23.  
  24.   // method 1
  25.   if (is_colliding && (collision == null || !collision.IsPlaying))
  26.     collision = collision_sound.Play();
  27.  
  28.   // method 2
  29.   if (is_colliding && !was_colliding) collision_sound.Play();
  30.  
  31.   was_colliding = is_colliding;
  32. }

Edit: code fixed
« Last Edit: 25 May 2014, 22:36 by Khris »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Khris.

Long time, no speak.
It's great to hear from my mentor.;-D

This looks like it maybe exactly what I'm looking for.
I have printed out your script so I can take it to work with me and try it over this Bank Holiday.
Unfortunately I won't be able to tell you how things go until Tuesday Evening.:(

Thank you ever so much, I really appreciate you taking the time to look at this for me.

Yours.
Jay.
  Master Decorator

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Khris.

I completely forgot about the megabytes, I still have left on my phone.
So here I am sitting at work typing this message to let you know that your script works beautifuly.:)

I had to use Method 2, which is a shame, because Method 1 wouldn't work.
I receive this message...
Error running function
'Room_RepExec':
Error Null pointer referenced.

When I am home I will post the complete script here.
It maybe useful to other people.

Thank you for your help.
As always it is much appreciated.

Yours.
Jay.
  Master Decorator

Khris

  • Posts: 10,676
  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on a game that was nominated for an AGS Award!
Hi Jay,

stupid mistake on my part; method 1 will try to play the sound as long as the channel is free but doesn't check whether there even was an actual collision...
Code: Adventure Game Studio
  1.   // method 1
  2.   if (is_colliding && (collision == null || !collision.IsPlaying))
  3.     collision = collision_sound.Play();

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Khris.

Yep, that's cured it.;-D
Actually, I think that Method 1 works better for what I need.

Thank you for your help, Khris.
It's much appreciated.

Yours.
Jay.
« Last Edit: 26 May 2014, 03:02 by RetroJay »
  Master Decorator