1. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

Winning Sound ?!

Discussion in 'Modding Help' started by Ni, May 12, 2017.

  1. Ni

    Ni Battle Angel Global Moderator Forum Moderator Mapping Moderator Donator Tester Official Server Admin
    1. Active Forum Users

    Messages:
    433
    Hey C:
    How can i make team 1 and 2 have there own winning and losing sounds ?
    I found this, but its for the winners in general, i want team 1 and 2 have their own sound :3
    Code:
    const string tagname = "played fanfare";
    
    void onInit(CRules@ this)
    {
        onRestart(this);
    }
    
    void onRestart(CRules@ this)
    {
        this.set_bool(tagname, false);
    }
    
    void onTick(CRules@ this)
    {
        if (this.isGameOver() && this.getTeamWon() >= 0 && !this.get_bool(tagname))
        {
            // only play for winners
            CPlayer@ localplayer = getLocalPlayer();
            if (localplayer !is null)
            {
                CBlob@ playerBlob = getLocalPlayerBlob();
                int teamNum = playerBlob !is null ? playerBlob.getTeamNum() : localplayer.getTeamNum() ; // bug fix (cause in singelplayer player team is 255)
                if (teamNum == this.getTeamWon())
                {
                    Sound::Play("/FanfareWin.ogg");
                }
                else
                {
                    Sound::Play("/FanfareLose.ogg");
                }
            }
    
            this.set_bool(tagname, true);
            // no sound played on spectator or tie
        }
    }
    
    Thanks <3
     
    Biurza likes this.
  2. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    teamNum == 1 ? Sound::Play("/RedTeamWin.ogg") : Sound::Play("/BlueTeamWin.ogg"); and the same again for the lose sound
     
    Asu likes this.
  3. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    I'm not sure that'd work, the cleaner way is
    Code:
    Sound::Play(teamNum == 0 ? "/BlueTeamWin.ogg" : "/RedTeamWin.ogg");
    If you want to call different functions you'd use an if block, ternary operators are kind of inappropriate else.

    also damn you ninja
     
  4. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    oh yeah thought it looked wrong :rollseyes:
    --- Double Post Merged, May 12, 2017, Original Post Date: May 12, 2017 ---
    If you have more than 2 teams you could make a couple arrays. @AsuMagic back me up here, I'm almost always wrong.
    Code:
    void onTick(CRules@ this)
    {
        if (this.isGameOver() && this.getTeamWon() >= 0 && !this.get_bool(tagname))
        {
            // only play for winners
            CPlayer@ localplayer = getLocalPlayer();
            if (localplayer !is null)
            {
                CBlob@ playerBlob = getLocalPlayerBlob();
                int teamNum = playerBlob !is null ? playerBlob.getTeamNum() : localplayer.getTeamNum() ; // bug fix (cause in singelplayer player team is 255)
                if (teamNum == this.getTeamWon())
                {
                    string winsound = getWinSound(teamNum);
                    Sound::Play(winsound +".ogg");
                }
                else
                {
                    string losesound = getLoseSound(teamNum);
                    Sound::Play(losesound +".ogg");
                }
            }
    
            this.set_bool(tagname, true);
            // no sound played on spectator or tie
        }
    }
    
    string getWinSound(u8 team)
    {   
        string winsound = "";
        string[] teamSounds = { "/blueteamwin",
                                "/redteamwin",
                                "/greenteamwin",
                                "/purpleteamwin",
                                "/orangeteamwin",
                                "/aquateamwin",
                                "/tealteamwin",
                             };
    
         for (uint i = 0; i < teamSounds.length; i++)
        {
            if (i == team)
            {
                winsound = teamSounds[i];
            }
        }
        return winsound;
    }
    
    string getLoseSound(u8 team)
    {   
        string losesound = "";
        string[] teamSounds = { "/blueteamlose",
                                "/redteamlose",
                                "/greenteamlose",
                                "/purpleteamlose",
                                "/orangeteamlose",
                                "/aquateamlose",
                                "/tealteamlose",
                             };
    
         for (uint i = 0; i < teamSounds.length; i++)
        {
            if (i == team)
            {
                losesound = teamSounds[i];
            }
        }
        return losesound;
    }
     
  5. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    Your for loop is quite useless, you simply can access the string at index team. If you're worrying about accessing a string out of bounds you can just check the size of the array. (It won't be necessary to check for the index to be negative because team is unsigned and the subscript operator likely takes an unsigned anyways)
    The winsound variable you have is unnecessary and will perform useless extra copies. You can return directly the string at the index you want and return "" or "/blueteamwin" as a default.
    I don't think length is a public field, it is a function that has to be called (i.e. length()). I typically use size(), which does the same thing, but is apparently more conventional in the KAG code base (it's the member function you'd have in C++).

    Generally I'd have done this :
    Code:
    void onTick(CRules@ this)
    {
        if (this.isGameOver() && this.getTeamWon() >= 0 && !this.get_bool(tagname))
        {
            // only play for winners
            CPlayer@ localplayer = getLocalPlayer();
            if (localplayer !is null)
            {
                CBlob@ playerBlob = getLocalPlayerBlob();
                int teamNum = playerBlob !is null ? playerBlob.getTeamNum() : localplayer.getTeamNum() ; // bug fix (cause in singelplayer player team is 255)
                Sound::Play("/" + getTeamName(teamNum) + "team" + (teamNum == this.getTeamWon() ? "win" : "lose") + ".ogg");
            }
    
            this.set_bool(tagname, true);
            // no sound played on spectator or tie
        }
    }
    
    string getTeamName(u8 team)
    { 
        string[] teamNames =
        {
            "blue",
            "red",
            "green",
            "purple",
            "orange",
            "aqua",
            "teal",
        };
    
        if (team < teamSounds.size())
            return teamNames[team];
        else
            return "default";
    }
    This way, it would use blueteamwin.ogg, redteamwin.ogg, greenteamlose.ogg, etc.
    The CTeam class also can let you use its getName() method, but it returns something like Blue Team or Red Team. The advantage would be an extra function removed, but it needs a bit of string manipulation - lower out the string and replace " " by "" (or by "_").
    See:
    Code:
    [09:33:41] print(string("hello world hOW aRe yOu doing").replace(" ", "").toLower());
    [09:33:41] helloworldhowareyoudoing
    This gives the final code :
    Code:
    void onTick(CRules@ this)
    {
        if (this.isGameOver() && this.getTeamWon() >= 0 && !this.get_bool(tagname))
        {
            // only play for winners
            CPlayer@ localplayer = getLocalPlayer();
            if (localplayer !is null)
            {
                CBlob@ playerBlob = getLocalPlayerBlob();
                int teamNum = playerBlob !is null ? playerBlob.getTeamNum() : localplayer.getTeamNum() ; // bug fix (cause in singelplayer player team is 255)
                CTeam@ team = this.getTeam(teamNum);
                string team_name = team.getName().replace(" ", "").toLower();
                Sound::Play("/" + team_name + (teamNum == this.getTeamWon() ? "win" : "lose") + ".ogg");
            }
    
            this.set_bool(tagname, true);
            // no sound played on spectator or tie
        }
    }
    Also, team names depends on the configuration. So if you have the green team named "Orc Team", the filename will be "orcteamwin.ogg" for the winning sound. If it's just "Orcs", "orcswin.ogg", etc.
     
    Monkey_Feats likes this.
  6. Monkey_Feats

    Monkey_Feats Bison Rider Tester

    Messages:
    214
    Nice one. :)
     
    Asu likes this.