Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
dreg_master

UDMF - ACS for RANDOM Music Help

Recommended Posts

Hey guys,

I am trying to make a variable increase by 1 each time button is pressed thus making it move to the next song. Songs 1 thru 10 for now. I figured it needs to be a variable, see below code

I know its a mess and there is better way to do it, last i coded was qbasic back on a 286sx

I included a printf to show me the variable changing and the value every time i press the button increases by 3, but the song only changes once and that is it, keeps restarting the same song when i press the button and increasing the variable by another 3???.

#include "zcommon.acs"
int music = 1; //Music Start Variable 1

script 1 open //FOG - JUST IGNORE This is something else
{
Sector_SetFade (1,150,150,150);
Sector_SetFade (2,150,150,150);
Sector_SetFade (3,150,150,150);
Sector_SetFade (4,150,150,150);
Sector_SetFade (5,150,150,150);
Sector_SetFade (6,150,150,150);
Sector_SetFade (7,150,150,150);
Sector_SetFade (8,150,150,150);
}

Script 3 (void) //JukeBOX, by switch music
{
print(s:"music is " , d: music);
Delay (3); // Wait for next frame
    
    if (music == 1);
        {
        SetMusic("d_runnin");
        music=music+1;
        }
            
    if(music == 2);
        {
        SetMusic("d_stalks");
        music=music+1;
        }

    if (music == 3);
        {
        SetMusic("d_betwee");
        music=music+1;
        }
            
    if(music == 4);
        {
        SetMusic("d_doom");
        
}

    if (music == 5);
        {
        SetMusic("d_the_da");
        }
            
    if(music == 6);
        {
        SetMusic("d_shawn");
        }

    if (music == 7);
        {
        SetMusic("d_ddtblu");
        }
            
    if(music == 8);
        {
        SetMusic("d_in_cit");
        }

    if (music == 9);
        {
        SetMusic("d_runnin");
        }
            
    if(music == 10);
        {
        SetMusic("d_dead");
        }        
       
 }
I dont get compile errors. Just the button press the text changes increases by 3 but the music track is always the same.

Thanks for any help.

Share this post


Link to post

Obvious errors:
1. Don't put ";" after "if()", that will cause the code in curly brackets to be performed always, I believe (or not?).
2. You have "music=music+1;" inside some of the "if" conditions, but not inside other ones. In fact, you should put that line before and out of all of the "if" conditions.
3. You should also put "if(music>10) { music = 1; }"
4. Also beware, you've got music names mixed after "2".

So:

Script 3 (void) //JukeBOX, by switch music
{
  Delay (3); // Wait for next frame
  music++;                              // Change the value here!
  if(music>10) music = 1;               // This line!
  print(s:"music is " , d: music);      // Now printf the value, once it's changed!
  if(music == 1) SetMusic("d_runnin");  // It would be much better to use "switch/case" instead of "if".
  if(music == 2) SetMusic("d_stalks");
  if (music == 3) SetMusic("d_countd"); // D_COUNTD!!!
  if(music == 4) SetMusic("d_betwee");  // D_BETWEE!!!
  if (music == 5) SetMusic("d_doom");   // D_DOOM!!!
  if(music == 6) SetMusic("d_the_da");  // D_THE_DA!!!
  if (music == 7) SetMusic("d_shawn");  // D_SHAWN!!!
  if(music == 8) SetMusic("d_ddtblu");  // D_DDTBLU!!!
  if (music == 9) SetMusic("d_in_cit"); // D_IN_CIT!!!
  if(music == 10) SetMusic("d_dead");
}

Share this post


Link to post

Awesome, i jut cut and paste it worked first go :)
LOL Your awesome, thanks ive been up all night trying to figure this out. Now ill get some sleep.

Like u said there is better way by using choice... sorry switch/case
Im a bit old school and used to line numbers and Gotos lol

Really, helped me heaps.

EDIT: Also i am going to try make a switch now that changes the room colours Red Blue ect, one switch with 4-5 settings..

Edit 2: Actually Nah... Im going to bed.... :P

Share this post


Link to post

You're welcome, the problem was trivial. I've known C for years, but I'm currently studying it at university. We have compulsory homeworks every week. Damn, those are frustratingly hard and time consuming! :)

Share this post


Link to post

Well Thanks to you i actually getting less sleep cause i was gonna give up. LOL

But you gave me hope :)
I used what you taught me and got my sector changing 4 colors with one switch. So happy - Now ill go to sleep..

Script 4 (void)
{
sector++;
if(sector>4) sector = 1;
if(sector == 1) Sector_SetFade (11,255,255,0); //yellow
if(sector == 2) Sector_SetFade (11,255,0,255); //Purple
if(sector == 3) Sector_SetFade (11,0,255,255); //Blue
if(sector == 4) Sector_SetFade (11,0,0,0); //Normal
}

Share this post


Link to post

How about For and Next loops? Do that have similar in ACS?

Script 5 (void)
{
for a=1 to 255;
Sector_SetFade (11,a,255,0);
Delay (3)
next a
}

No Good, So i try this

Script 5 (void)
{
INT a=0;
until (a=255);
Sector_SetFade (11,a,255,0);
a++;
Delay (3);
}

still no good.

Script 5 (void)
{
INT color=0;
while (color<=255);
Sector_SetFade (11,color,255,0);
color++;
Delay (3);
}

This one Compiled ok, but i get runaway script error ingame

Share this post


Link to post
scifista42 said:

Obvious errors:
1. Don't put ";" after "if()", that will cause the code in curly brackets to be performed always, I believe (or not?).


Yes, by itself ';' is a null statement. Semantically, it is equivalent to empty brackets: {}

That is to say, the following is legal code:

int x = 1;
{};
;
{}
x *= 2;
Here we have six instructions. There's two obvious instructions (int x = 1 and x *= 2) and in the middle we have four "empty" instructions. They will not do anything, but they exist.

So when you have something like this:
if (blah);
{
      dosomething();
}
what you really have is this:
if (blah)
{
}
{
      dosomething();
}
The instruction block gets disconnected from the if, and therefore is run all the time. (It's perfectly valid, even if kinda useless, to make braced blocks not connected to any if/while/switch/whatever.)

A good way to detect that is to have an else.
if (blah);
{
      dosomething();
}
else
{
      dosomethingelse();
}
This will not compile, the compiler will complain about the else not having a matching if.


Note that null statements are sometimes used deliberately.
Here's an example:
if (whatever)
;
else
{
      dosomething();
}
Here it's basically an equivalent of
if (!whatever)
{
      dosomething();
}
In rare and kinda contrived cases it might be more legible to do things this way, depending on the conditional.

A more frequent use of empty statement is in for loops:
int x = getvalue();
bla blah code
func(x);
more code
for (; x < somevalue; ++x)
{
      dosomething();
}
Here the iterator is already initialized before, so the for loop's initialization is an null statement.

dreg_master said:

Script 5 (void)
{
INT color=0;
while (color<=255);
Sector_SetFade (11,color,255,0);
color++;
Delay (3);
}

This one Compiled ok, but i get runaway script error ingame


Again, this is a case of using a null statement. What you want to have as code is this:

Script 5 (void)
{
      int color=0;
      while (color<=255)
      {
            Sector_SetFade (11,color,255,0);
            color++;
            Delay (3);
      }
}
The curly braces are very important!

Share this post


Link to post

The code in the 1st post shows a good example of the sometimes subtle purpose of "else if" blocks.

if (music == 1)
{
SetMusic("d_runnin");
music=music+1;
}

if(music == 2)
{
SetMusic("d_stalks");
music=music+1;
}

if (music == 3)..... etc

See, each if condition is based on the value of music and each condition raises the value of music. So say music starts at 1. The first if block executes, setting d_runnin and making music==2. But now since music is 2, the 2nd if block executes right after (which makes music ==3, etc every if block will execute, since you add 1 in each block, so the music integer and track will be instantly set to the last if block. But if you make all the ifs after the 1st say "else if" instead, only the true condition of the 10 blocks will execute. But doesn't matter since scifista42 changed the code (his works without elses because the music int isn't changed inside any of his ifs. (you could put music++ at the end of the ifs if desired so the first track will be music's starting value)

You could try this with an array (untested, tracksi stands for "tracks index" which was a clearer variable name than music in this case, and goes 0 to 9 since indexes start at 0). Arrays have to be global, a weird ACS quirk that makes functions you WANT to return an array instead have to edit a global array intended as that functions "output".

int tracks[10]={"d_runnin","d_stalks","d_countd","d_betwee","d_doom","d_the_da","d_shawn","d_ddtblu","d_in_cit","d_dead"};
int tracksi=0;

Script 3 (void)
{
  print(s:"music is " , d: tracksi);
  SetMusic(tracks[tracksi]); 
  Delay (3);
  tracksi+=1;
  if(tracksi>=10) tracksi = 0;
}

Share this post


Link to post

Yep, array is a much cleaner approach.

Share this post


Link to post
dreg_master said:

How about For and Next loops? Do that have similar in ACS?

Script 5 (void)
{
for a=1 to 255;
Sector_SetFade (11,a,255,0);
Delay (3)
next a
}

No Good, So i try this

Script 5 (void)
{
INT a=0;
until (a=255);
Sector_SetFade (11,a,255,0);
a++;
Delay (3);
}

still no good.

Script 5 (void)
{
int color = 0;
while (color<=255);
Sector_SetFade (11,color,255,0);
color++;
Delay (3);
}

This one Compiled ok, but i get runaway script error ingame

Proper usage of brackets and semicolons is important! Try this:

Script 5 (void)
{
  int color=0;
  while (color<=255) {
    Sector_SetFade (11,color,255,0);
    color++;
    Delay (3);
  }
}
Also, here is a ZDoomwiki page for ACS, a very useful site with all information you need. You might also search for a C tutorial, to get into the basics of its proper syntax.

Also, learn to use [code] tags on these forums, to display your codes like I did. :)


EDIT: I didn't see that Gez already posted the same code that I suggested. :P

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×