• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

What's wrong with this code? [SOLVED]

G

Gary Alexander Stott

Guest
Hey guys,

I'm making a Pong game and I'm trying to get my music to change as the score of the game changes. There are six tracks in total, one for scores of 0,1,2,3,4 & 5 respectively. When the game starts at 0-0, the score 0 music should play, with the music changing to score 1 when one or more players scores 1. If a player catches up, for example, going from 3-2 to 3-3, then the score 3 music wouldn't need to change. It could just keep on playing. As soon as a number is reached for the first time, the song corresponding to that score should start looping. That's the idea, and here's how I'm trying to achieve it:


if (global.paddle1_score = 1) or (global.paddle2_score = 1) and !audio_is_playing(snd_1v)
{
audio_stop_sound(snd_0v);
audio_play_sound(snd_1v,0,1);
}

if (global.paddle1_score = 2) or (global.paddle2_score = 2) and !audio_is_playing(snd_2v)
{
audio_stop_sound(snd_1v);
audio_play_sound(snd_2v,0,1);
}

if (global.paddle1_score = 3) or (global.paddle2_score = 3) and !audio_is_playing(snd_3v)
{
audio_stop_sound(snd_2v);
audio_play_sound(snd_3v,0,1);
}

if (global.paddle1_score = 4) or (global.paddle2_score = 4) and !audio_is_playing(snd_4v)
{
audio_stop_sound(snd_3v);
audio_play_sound(snd_4v,0,1);
}

if (global.paddle1_score = 5) or (global.paddle2_score = 5) and !audio_is_playing(snd_5v)
{
audio_stop_sound(snd_4v);
audio_play_sound(snd_5v,0,1);
}

Problem is, I keep getting glitchy audio, like it's turning off and on every frame. I've tried expressing this a bunch of different ways in code, any suggestions?

Thank you.
 

Binsk

Member
It's an issue with your placement of parintheses, or lack thereof.

You are writing your if statement as such:
If A or B and C

This will get interpreted as:
If A or (B and C)

Meaning the statement will return true in two cases:
  1. A is true (regardless of B and C)
  2. B is true and C is true (regardless of A)
Make sure to use proper parintheses. Proper parintheses in this case would be:
If (A or B) and C

This would change the success cases to these:
  1. A is true and C is true (regardless of B)
  2. B is true and C is true (regardless of A)

I hope that helps.
 
Last edited:
L

Laurent57

Guest
I thought I had given the solution with max(score1,score2) with comparing the old and the new values... :confused:
 

Binsk

Member
I thought I had given the solution with max(score1,score2) with comparing the old and the new values... :confused:
Why make the solution convoluted? It can easily be done with a simple if statement so long as proper parintheses are used.

Using max doesn't even shorten the statement and, should the programmer not know the intricacies of GameMaker (which seems the case, no shame in it) such as how it treats booleans as integers, it just comes off as confusing.

Edit: Unless you are referring to a more elegant solution in general, which I now realise you may be.
 
G

Gary Alexander Stott

Guest
Thank you both for your input!

Apologies Laurent, I'll concede to not fully understanding your answer yesterday, and so I failed to implement it properly. Could you show me the exact way of phrasing it, using my code above?

I if I have an 'oldscore' variable in my scorekeeper which = max(global.paddle1_score,global.paddle2_score), then I'm not sure what to do with regard to creating a 'newscore' variable - your answer yesterday indicated that newscore should = max(global.paddle1_score,global.paddle2_score) too, but that doesn't make sense to me, since that would give it the same value as 'oldscore' wouldn't it? Moreover, how could I get this variable to play a specific song at a specific score if it's only keeping track of which score is higher?

And Binsk, thank you for clarifying. I tried it multiple ways to see whether that was the case, but couldn't get it working in any format.
 
Last edited by a moderator:
G

Gary Alexander Stott

Guest
This is my code as it currently stands. It's still bugging out at certain scores, 4-4 in particular, and I don't understand why.

winningscore = max(global.paddle1_score,global.paddle2_score);
losingscore = min(global.paddle1_score,global.paddle2_score);

if (winningscore = 1) and (!audio_is_playing(snd_1v)) and (winningscore >= losingscore)
{
audio_stop_all();
audio_play_sound(snd_1v,0,1);
}

if (winningscore = 2) and (!audio_is_playing(snd_2v)) and (winningscore >= losingscore)
{
audio_stop_all();
audio_play_sound(snd_2v,0,1);
}

if (winningscore = 3) and (!audio_is_playing(snd_3v)) and (winningscore >= losingscore)
{
audio_stop_all();
audio_play_sound(snd_3v,0,1);
}

if (winningscore = 4) and (!audio_is_playing(snd_4v)) and (winningscore >= losingscore)
{
audio_stop_all();
audio_play_sound(snd_4v,0,1);
}

if (winningscore = 5) and (!audio_is_playing(snd_5v)) and (winningscore >= losingscore)
{
audio_stop_all();
audio_play_sound(snd_5v,0,1);
}
 
Last edited by a moderator:
G

Gary Alexander Stott

Guest
I thought I had given the solution with max(score1,score2) with comparing the old and the new values... :confused:
Replying directly to hopefully alert your attention. Sorry if I appear desperate. It's because I am! :p Spending way too much time trying to get this music system working and not enough time on the rest of the game.
 
L

Laurent57

Guest
I think you have to put that in the code which counts points,
ONLY when you add a point (whatever the player)

**** you add a point *****
new_winningscore=max(global.paddle1_score,global.paddle2_score);
if new_winningscore>old_winningscore then global.winningscore+=1;
old_winningscore=new_winningscore;

.... forget this part when you play music :

winningscore = max(global.paddle1_score,global.paddle2_score);
losingscore = min(global.paddle1_score,global.paddle2_score);

and

and (winningscore >= losingscore) ...........in your "if statement"

but replace winningscore with global.winningscore
 
L

Laurent57

Guest
in the script which adds point and only when you add point :

Code:
**** you add a point *****
new_winningscore=max(global.paddle1_score,global.paddle2_score);
if new_winningscore>old_winningscore then global.winningscore+=1;
old_winningscore=new_winningscore;
in the script which plays music :
Code:
if (global.winningscore = 1) and (!audio_is_playing(snd_1v))
{
audio_stop_all();
audio_play_sound(snd_1v,0,1);
}

if (global.winningscore = 2) and (!audio_is_playing(snd_2v))
{
audio_stop_all();
audio_play_sound(snd_2v,0,1);
}

if (global.winningscore = 3) and (!audio_is_playing(snd_3v))
{
audio_stop_all();
audio_play_sound(snd_3v,0,1);
}

if (global.winningscore = 4) and (!audio_is_playing(snd_4v))
{
audio_stop_all();
audio_play_sound(snd_4v,0,1);
}

if (global.winningscore = 5) and (!audio_is_playing(snd_5v))
{
audio_stop_all();
audio_play_sound(snd_5v,0,1);
}
 
G

Gary Alexander Stott

Guest
Thank you! I'll give this a try tomorrow and let you know how I get on with it. I appreciate the help.
 
G

Gary Alexander Stott

Guest
Implemented the code this morning and seem to have everything working as intended, at last! ^_^ Had a few problems to begin with as my ball is actually the object responsible for adding points whenever its x value ventures off-screen. Had to think about where to put everything, but it seems to be working now. Due to the fact I'm using audio_stop_all(), I had problems with my score/miss sound effects too, but I coded a workaround that plays the appropriate sound effect just before the ball goes off-screen, but has passed the paddle and can no longer be redirected.

Thanks so much for your help. I've only been doing this for a week or so, and reactive music is the first thing I've tried to code from scratch. All the other code I've needed so far has been widely available on the internet, but I couldn't find any other music systems like this one and so I had to think about how to express it myself. I think I had a partial solution, but it was convoluted and didn't cover everything it needed to cover compared to this much more elegant approach. Moving forward, I can now reference this code whenever I need to compare scores and use the value of them to initiate actions.

I can now continue to work on the rest of my game! Thanks again, Laurent57! I hope good things happen to you today!
 
Top