Home › Forums › General › Programming › LUA and string matching
- This topic has 9 replies, 3 voices, and was last updated 9 years ago by audiosync.
-
AuthorPosts
-
November 27, 2011 at 9:09 pm #501
Ok so today’s request for help is related to dealing with strings in LUA. What I’m trying to do is have a method react differently depending on the modulator which calls the method. I’ve worked out that I can get the modulator name, checked the API docs for String and read through the linked page on the JUCE API site. But I still just can’t get it to work!
Here’s my first stab at some code:
[code:2f2lmgt1]
function myTestButton2 (modulator)
modLuaName=modulator:getLuaName()if (modLuaName:endsWith("sustain-on")) then
console("Received the requested modulator")
endend[/code:2f2lmgt1]
And the button’s modulator name is OP1-sustain-on, but this throws an "attempt to call method ‘endsWithChar’ (a nil value)" exception.
I’m sure this is just a silly noob mistake but I’m stuck, any ideas?
David
November 28, 2011 at 10:57 am #3522endsWithChar is a different method than endsWith so witch one you are using ?
November 28, 2011 at 8:43 pm #3523Hi atom,
Aha yes sorry I must have made a mistake, I want to use the endsWith method and match a whole substring to the modulator’s name string.December 2, 2011 at 3:28 pm #3524I don’t want to be impatient, but is there anyone who has any idea how to handle this in Ctrlr’s LUA? ” title=”Smile” />
December 2, 2011 at 3:52 pm #3525It’s a bit complicated but doable, i think i messed this up by not registering a converter for the String class, you can do this now like so
[code:25izf3m3]
String name = modulator:getLuaName()
if (name:endsWith(String("sustain-on"))) then
console ("match")
end
[/code:25izf3m3]this is because LUA expects it’s string parameters to by of type String and luabind uses std::string and it can’t convert them, i’ll try to work around this somehow.
December 2, 2011 at 9:55 pm #3526Also you don’t really NEED to rely on the JUCE api there is a whole lot of LUA functions that are faster and simpler for stuff like MATH and STRING for example:
[code:3f7xz4gp]
m = panel:getModulatorByName("modulator-3")
name = m:getLuaName()
ret = string.find (name, "-3")
console ("ret="..ret)
— will print 10 as the substring -3 is at the 10th position
[/code:3f7xz4gp]Here is the whole string library docs: http://lua-users.org/wiki/StringLibraryTutorial
LUA is a powerful programming engine, use it.
March 28, 2015 at 5:27 pm #42048It’s a bit complicated but doable, i think i messed this up by not registering a converter for the String class, you can do this now like so
[code:25izf3m3]
String name = modulator:getLuaName()
if (name:endsWith(String(“sustain-on”))) then
console (“match”)
end
[/code:25izf3m3]Hi, I just stumbled over the same issue.
When I try the above console shows error for the string declaration:ERROR: [string “change_form”]:8: ‘=’ expected near ‘name’
change_form = function(mod, value) String name = mod:getLuaName() if (name == "v1_op1_form") then console ("match") end
- This reply was modified 9 years, 1 month ago by audiosync.
March 31, 2015 at 1:14 pm #42322This is not c++ or c.
you do
name = mod:getLuaName()
you never do
String name = mod:getLuaName()this is a syntax error for lua
if you want to declare an empty String you do
myString = String()
variables in Lua have no types, you never specify them.
March 31, 2015 at 8:31 pm #42355Ok, thanks for the info!
So how do I get the calling modulator’s name into a string?
When I tried this:change_form = function(mod, value) name = mod:getLuaName() v = string.sub (name, 2, 2) console ("Voice: "..v) end
…I always ran into the error:
Error message: [string “change_form”]:12: bad argument #1 to ‘sub’ (string expected, got userdata)April 6, 2015 at 8:27 am #43061How silly! I tried to read getLuaName() into string because this didn’t work:
vop = (panel:getModulatorByName(“modulator1”):getComponent():getProperty("name"))
but I just had to do this:
name = (panel:getModulatorByName(modulator1):getProperty("name"))
sorry for disturbing…
- This reply was modified 9 years ago by audiosync.
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.