It is currently April 25th, 2024, 1:03 pm

A little help with bang

Discuss the use of Lua in Script measures.
Skymil
Posts: 24
Joined: July 19th, 2011, 9:44 am
Location: France

A little help with bang

Post by Skymil »

Hello,

I have a few issues with Lua when I want to send a bang to my skin. I'm a beginner so I think I have not understood correctly the language.

I made this script to block adverts in a webradio.

This is the code :

Code: Select all

PROPERTIES =
{

}

function Initialize()

   Artist = SKIN:GetMeasure("MeasureArtist")
   ShutUp = 0

end

function Update()

   Title = Artist:GetStringValue()

    if ShutUp == 0 and Title == "Advert:Radionomy"
    then
     SKIN:Bang("!PluginBang "mPlayer SetVolume 0"")
     ShutUp = 1
    elseif ShutUp == 1 and not Title == "Advert:Radionomy"
    then
     SKIN:Bang("!PluginBang "mPlayer SetVolume 100"")
     ShutUp = 0
    end

   return Title

end
The error I get is : 19: ')' expected near 'mPlayer'.
When I remove the quotes between mPlayer SetVolume 0 and mPlayer SetVolume 100, I have no error but the bang doesn't work (and I have a notice in the log, something like "Undefined command").

Can you help me?

Thanks in advance.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: A little help with bang

Post by Kaelri »

You can't use nested quotes in Lua unless you use escape characters, like this:

Code: Select all

SKIN:Bang("!PluginBang \"mPlayer SetVolume 0\"")
This tells Lua that the quotes around "mPlayer SetVolume 0" are meant to be part of the parameter, rather than closing the first parameter and opening a new one.

Alternatively, since Lua allows you to use double quotes and single quotes interchangeably, you can also do it like this:

Code: Select all

SKIN:Bang('!PluginBang "mPlayer SetVolume 0"')
In this case, since you open the parameter with a single quote, it can only be closed with a single quote; the double quotes in your !PluginBang syntax won't interfere. :)
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: A little help with bang

Post by smurfier »

1) !PluginBang was depreciated. Please use !CommandMeasure.
2) You need to mix " and ' in lua or escape the additional quotes with \.
3) You can use ~= for not equal.

Code: Select all

PROPERTIES =
{

}

function Initialize()

   Artist = SKIN:GetMeasure("MeasureArtist")
   ShutUp = 0

end

function Update()

   Title = Artist:GetStringValue()

    if ShutUp == 0 and Title == "Advert:Radionomy" then
     SKIN:Bang('!CommandMeasure "mPlayer" "SetVolume 0"')
     ShutUp = 1
    elseif ShutUp == 1 and Title ~= "Advert:Radionomy" then
     SKIN:Bang('!CommandMeasure "mPlayer" "SetVolume 100"')
     ShutUp = 0
    end

   return Title

end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Skymil
Posts: 24
Joined: July 19th, 2011, 9:44 am
Location: France

Re: A little help with bang

Post by Skymil »

Oh thank you! I feel a little stupid now...

EDIT:
@smurfier: Thank you as well! I take your advices. Is there a real difference between "and not ==" and "and ~=" ? Or it's just for a better look?
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: A little help with bang

Post by smurfier »

There really isn't a difference, I just find it simpler. If you're going to compare something then inverse it, why not just use the inverse comparison?
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: A little help with bang

Post by Kaelri »

Skymil wrote:Is there a real difference between "and not ==" and "and ~=" ? Or it's just for a better look?
I'm not 100% sure, but I think and not [Variable] == 'Value' would work even when [Variable] hasn't been declared, whereas and [Variable] ~= 'Value' would break the script in that case. I'm I'm right, then and not would actually be a slightly more stable way to formulate the condition. Which makes me think that I'm probably wrong. ;)
smurfier wrote:1) !PluginBang was depreciated. Please use !CommandMeasure.
I'm not sure of this, either. I think !CommandMeasure is meant for commanding Script measures, but I don't have any recollection of its replacing !PluginBang in other cases.
Skymil
Posts: 24
Joined: July 19th, 2011, 9:44 am
Location: France

Re: A little help with bang

Post by Skymil »

There really isn't a difference, I just find it simpler. If you're going to compare something then inverse it, why not just use the inverse comparison?
Ok. You're right. But, heh, maybe I prefer to have a tortured mind :P Thank you again for your quick answers.
I'm not 100% sure, but I think and not [Variable] == 'Value' would work even when [Variable] hasn't been declared, whereas and [Variable] ~= 'Value' would break the script in that case. I'm I'm right, then and not would actually be a slightly more stable way to formulate the condition. Which makes me think that I'm probably wrong. ;)
Oh if it's the case, it's very strange.
Last edited by Skymil on August 29th, 2011, 5:11 pm, edited 1 time in total.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: A little help with bang

Post by smurfier »

From the change log:
Added !CommandMeasure bang.
This replaces !PluginBang, which is being depreciated and should not be used in new skins. The reason for this change is that !CommandMeasure can be used to both send a bang to a plugin as !PluginBang did before, but can also be used to send a bang to any measure that can accept it. As of now, only the Measure=Script (Lua) measures can make use of this, but this may change over time.
The syntax is slightly different from !PluginBang as it is:
!CommandMeasure "MeasureName" "Arguments"
instead of:
!PluginBang "MeasureName Arguments'
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: A little help with bang

Post by Kaelri »

I stand corrected. :)
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: A little help with bang

Post by jsmorley »

This was partially my fault. We have indeed "depreciated" !PluginBang, but other than the release notes, this was not well documented. I have changed the entry for !PluginBang in the manual to reflect this, and have changed every manual entry and example skin for all plugins to reflect the new approach.