Chord Progression Script Attempt. Don't understand how to make it work.

Hi guys. Im trying to make a script where by the user presses buttons which are assigned Chords. A Major D Major and E Major.

The user presses these buttons in any order and then by pressing the play button the chords play through in the order the user pressed them.

I just dont understand how I am going to get this to work!

Ive attempted it.

//#pragma strict

public var AMajor : AudioClip;
public var DMajor : AudioClip;
public var EMajor : AudioClip;

private var button : boolean = false;

public var Chord1;
public var Chord2;
public var Chord3;
public var Chord4;

function OnGui ()
{
    if (GUI.Button(Rect(10, 300, 100, 50), "A Major")){
        var AMajorButton;
    }
    if (GUI.Button(Rect(110, 300, 100, 50), "D Major")){
        var DMajorButton;
    }
    if (GUI.Button(Rect(210, 300, 100, 50), "E Major")){
        var EMajorButton;
    }
    if (GUI.Button(Rect(10, 300, 100, 50), "Start")){
        var StartButton;
    }
}

function Update ()
{
	if (AMajorButton) 
    {   
        Chord1 = AMajor;
    }
    else if (DMajorButton) 
    {   
        Chord1 = DMajor;
	}
    else if (EMajorButton) 
    {   
        Chord1 = EMajor;
    }
    else (StartButton) 
    {
        TheYielder ();
    }
}








function TheYielder ()
{
    audio.clip = Chord1;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord2;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord3;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord4;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);



}

Help is really needed please!

All comments are appreciated!

Well, first of all your Button variables are declared in the OnGUI() function and you are checking if they’re pressed in the Update() function. You should look into Scope, secondly you don’t specify a value for your variables.

New Code I have tried out. Now I can get the start button to work but It only plays the last chord button pressed.

//#pragma strict

public var AMajor : AudioClip;
public var DMajor : AudioClip;
public var EMajor : AudioClip;

private var button : boolean = false;

public var Chord1;
public var Chord2;
public var Chord3;
public var Chord4;

function OnGUI ()
{
    var AMajorButton = GUI.Button(Rect(10, 250, 100, 50), "A Major");
    
    var DMajorButton = GUI.Button(Rect(110, 250, 100, 50), "D Major");

    var EMajorButton = GUI.Button(Rect(210, 250, 100, 50), "E Major");
    
    var StartButton = GUI.Button(Rect(310, 250, 100, 50), "Start");
    
    
    // Chord 1
    if (AMajorButton)
    {
        Chord1 = AMajor;
    }
    else if (DMajorButton)
    {
        Chord1 = DMajor;
    }
    else if (EMajorButton)
    {
        Chord1 = EMajor;
    }
    else if (StartButton)
    // Chord 2
    if (AMajorButton)
    {
        Chord2 = AMajor;
    }
    else if (DMajorButton)
    {
        Chord2 = DMajor;
    }
    else if (EMajorButton)
    {
        Chord2 = EMajor;
    }
    else if (StartButton)
        // Chord 3
    if (AMajorButton)
    {
        Chord3 = AMajor;
    }
    else if (DMajorButton)
    {
        Chord3 = DMajor;
    }
    else if (EMajorButton)
    {
        Chord3 = EMajor;
    }
    else if (StartButton)
        // Chord 4
    if (AMajorButton)
    {
        Chord4 = AMajor;
    }
    else if (DMajorButton)
    {
        Chord4 = DMajor;
    }
    else if (EMajorButton)
    {
        Chord4 = EMajor;
    }
    else if (StartButton)
   
    {
        TheYielder ();
    }
}








function TheYielder ()
{
    audio.clip = Chord1;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord2;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord3;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    audio.clip = Chord4;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);



}

bump D:

You seem to have a fundamental misunderstanding about how programming works, especially in relation to an engine like this.

I’d recommend a lot more tutorials on programming in general, and look into what actually happens i OnGUI or even why Update is called.

Your entire code is written just one after the other. Your code there is called 60 times per second at least. So when you hit a button it does all your if statements at once.

But it doesn’t even do that, you hit AMajorButton, then it assigns chord1, it would also assign all the other chords at once to AMajor if you didn’t have the strange if(StartButton). That aborts the entire sequence and jumps to the end.

I can’t really beging to explain to you what you need to do without you having more understanding of how the engine loops and how programming languages work, but I’ll give you the basics to start researching.

Basically you will need a way to remember what happened in the past. Everything in OnGUI happens 60 times each second, and every time it happens there is no previous memory of the past times it ran.

So you would have an if statement looking for a button, if you hit AChord, you would immediately store it in an array or list. So your code should only look at what was hit, then store it for later in a list. Finally, the same code, always looking for each button, if it sees start, then you set a flag saying, “stop showing inputs and start playing the chords”. The chord playing code should also be a part of this loop you have, with another if else statement deciding if you are in button or play mode.

That’s the gist, but you will need to learn a lot more before you can build something like that.

Thanks for the help!

Yeh I looked over my code and have done some reading and what ive done is a load of poo! :stuck_out_tongue:

I have also asked my lecturer about it and he came up with the same sort of concept as you did so I’m asking for some help from him! Thanks guys! :slight_smile: