Author Topic: .setMusic/.setSound being a bitch - Solved.  (Read 2545 times)

I've got this
Code: [Select]
function onHourChange(%hour) {
//Package it!
if(%hour == 6) {
if(!isObject($AmbienceBrick)) { warn("Ambience brick doesn't exist!"); return; }
$AmbienceBrick.setSound(musicData_Morning);

} else if(%hour == 13) {
if(!isObject($AmbienceBrick)) { warn("Ambience brick doesn't exist!"); return; }
$AmbienceBrick.setSound(musicData_Day);
} else if(%hour == 22) {
if(!isObject($AmbienceBrick)) { warn("Ambience brick doesn't exist!"); return; }
$AmbienceBrick.setSound(musicData_Night);
}
}

I am sure that musicData_blah exists via console and I can set it manually but the script doesn't do anything and I am 100% certain it is being executed because if I forcefully set the hour to those mentioned before, the music brick will set its song to "None".


I looked at Server_MusicVoting and it has the same mechanic, yet it works there but not here. I am stumped guys.
« Last Edit: February 05, 2016, 08:35:37 PM by Pastrey Crust »

Your code is all messed up. Try something like this
Code: [Select]
function onHourChange(%hour)
{
if(isObject($AmbienceBrick)) && %hour == 6)
$AmbienceBrick.setSound(musicData_Morning);
else if((isObject($AmbienceBrick)) && %hour == 13)
$AmbienceBrick.setSound(musicData_Day);
else if((isObject($AmbienceBrick)) && %hour == 22)
$AmbienceBrick.setSound(musicData_Night);
else return warn("Ambience brick doesn't exist!");
}

I don't see how it's messed up.

Your code is all messed up. Try something like this

That isn't really better. I would format it this way:

Code: server.cs (15 lines)
function onHourChange(%hour)
{
    if(!isObject($AmbienceBrick))
    {
        warn("Ambience brick doesn't exist!");
        return;
    }

    switch(%hour)
    {
        case  6: $AmbienceBrick.setSound(musicData_Morning);
        case 13: $AmbienceBrick.setSound(musicData_Day);
        case 22: $AmbienceBrick.setSound(musicData_Night);
    }
}


Other than that, make sure the datablock exists, uiName is set to something and it uses a looping profile.

Yes, those checks are made when loading the server and the brick's datablock is checked when it is set.

Post the code that creates the datablock and calls this function

This is at the beginning of the script, right before any variable declaration or any kind of function declaration.
Code: [Select]
if(!isObject(musicData_Day)) warn("Day music datablock missing!");
if(!isObject(musicData_Night)) warn("Night music datablock missing!");
if(!isObject(musicData_Morning)) warn("Morning music datablock missing!");


Code: [Select]
package AmbienceSoundPackage {

function fxDTSbrick::onPlant(%this) {
Parent::onPlant(%this);
if(%this.getName() $= "_Ambience_Brick" && %this.getDatablock().uiName $= "Music (Server-Wide)") {
$AmbienceBrick = %this;
}
}

function fxDTSbrick::onLoad(%this) {
Parent::onLoad(%this);
if(%this.getName() $= "_Ambience_Brick" && %this.getDatablock().uiName $= "Music (Server-Wide)") {
$AmbienceBrick = %this;
}
}

function fxDTSbrick::setName(%this,%anme) {
Parent::setName(%this,%anme);
if(%this.getName() $= "_Ambience_Brick" && %this.getDatablock().uiName $= "Music (Server-Wide)") {
$AmbienceBrick = %this;
}
}
};
activatePackage(AmbienceSoundPackage);

And now that I think about it, the onPlant() is useless because you can't plant an already-named brick, lol.

That isn't really better. I would format it this way:

Code: server.cs (15 lines)
function onHourChange(%hour)
{
    if(!isObject($AmbienceBrick))
    {
        warn("Ambience brick doesn't exist!");
        return;
    }

    switch(%hour)
    {
        case  6: $AmbienceBrick.setSound(musicData_Morning);
        case 13: $AmbienceBrick.setSound(musicData_Day);
        case 22: $AmbienceBrick.setSound(musicData_Night);
    }
}

It's not like you have to go and over-do it, Zeb. There are a few ways you could format the code. Another, even simpler way would be
Code: [Select]
function onHourChange(%hour)
{
if(!isObject($AmbienceBrick))
return warn("Ambience brick doesn't exist!");
else if(%hour == 6
$AmbienceBrick.setSound(musicData_Morning);
else if(%hour == 13)
$AmbienceBrick.setSound(musicData_Day);
else if(%hour == 22)
$AmbienceBrick.setSound(musicData_Night);
}
Normally, I'd use a case when doing like 8 or more if statements

um guys, sorry to interrupt but I couldn't care less about the formatting. I'd like to know why this doesn't work while Server_MusicVoting uses the same syntax and it works.

Have you tried debugging it? eg echo $AmbienceBrick, add echos to see if the changes are reached etc.

This is at the beginning of the script, right before any variable declaration or any kind of function declaration.

Post the code that creates the datablocks


It's not like you have to go and over-do it, Zeb. There are a few ways you could format the code. Another, even simpler way would be

Compacting code doesn't really make it better, it'll just be less readable. You'll also get a warning message because you're returning the result of a void call.
« Last Edit: February 05, 2016, 12:20:38 PM by Zeblote »

I assume this is for http://forum.blockland.us/index.php?topic=292415.15

You had it working? Anyways, the easiest way to do what he wants is to use a regular server-wide music brick, and then use the day-cycle events to change the music onDawn, onNoon, etc.

He says in his OP that he'd like it to be for different environments too, not server wide. I would like this :3

Hm. I guess with VCE and Environment Events, paired with DayCycle Events this could be achievable
-
It would still be nice to have this without needing 3 different add-ons.

Hm. I guess with VCE and Environment Events, paired with DayCycle Events this could be achievable
Cannot be achieved.

Have you tried debugging it? eg echo $AmbienceBrick, add echos to see if the changes are reached etc.
It exists because there are no errors and using dump() shows that it's an audio profile.


I did. That's it. The brick itself is another add-on.