modifying mudlet.cpp to add and expose a function to lua

Post Reply
Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

modifying mudlet.cpp to add and expose a function to lua

Post by Lucky24 »

So, just wanted to make a simple change to the source to add the ability to silence playing sounds. However, the method crashes mudlet.

I added a method to mudlet.cpp and then modified TLuaInterpreter.cpp to add an intermediary method (what I assumed needed to happen to make the method available to lua in mudlet. Wondering if anyone can briefly explain how one would go about adding a C++ method/function and exposing that in lua in mudlet.

Here's what I tried:

In mudlet.cpp:
Code: [show] | [select all] lua
  void mudlet::stopSounds()
{
    mpMusicBox1->stop();
	mpMusicBox1->clear();
	mpMusicBox2->stop();
	mpMusicBox2->clear();
	mpMusicBox3->stop();
	mpMusicBox3->clear();
	mpMusicBox4->stop();
	mpMusicBox4->clear();
}

}
and the corresponding stub to mudlet.h, of course.

I then modified TLuaInterpreter.cpp to add a method to where I assumed would make the method available in lua:
Code: [show] | [select all] lua
int TLuaInterpreter::stopSounds( lua_State * L)
{
    mudlet::self()->stopSounds();
}
And finally added the method to be available to lua:
Code: [show] | [select all] lua
lua_register( pGlobalLua, "stopSounds", TLuaInterpreter::stopSounds );

However, when I compile and try to test, it crashes when the method is called. :(

Edit: I was looking through some Phonon documentation and I couldn't find the clear() method, so I tried stop() instead, both seem to crash.
Last edited by Lucky24 on Wed Mar 13, 2013 12:44 am, edited 1 time in total.

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: modifying mudlet.cpp to add and expose a function to lua

Post by chris »

I'm actually going to add this soon. I'll post my diff so you can do that/grab it from my github.


Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

Re: modifying mudlet.cpp to add and expose a function to lua

Post by Lucky24 »

Nice. Thank you.

Actually, I just finally managed to get mine to stop crashing. I had to check to see if the object was actually playing first, before calling clear(). Seems to work okay for me now; dunno why calling stop() or clear() on a non-playing object would cause it to crash for me and not for you.

Anyway, this seems to work in WIndows:
Code: [show] | [select all] lua
 void mudlet::stopSounds()
{
    if( mpMusicBox1->remainingTime() > 0 )
    {
        mpMusicBox1->clear();
    }
    if( mpMusicBox2->remainingTime() > 0 )
    {
        mpMusicBox2->clear();
    }
    if( mpMusicBox3->remainingTime() > 0 )
    {
        mpMusicBox3->clear();
    }
    if( mpMusicBox4->remainingTime() > 0 )
    {
        mpMusicBox4->clear();
    }
}

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: modifying mudlet.cpp to add and expose a function to lua

Post by chris »

That's odd. I use windows 7 (64 bit atm). I tested it out by doing a script that does playSoundFile(xxx) immediately followed by stopSounds(). Why yours crashed and mine didn't...Can you give some info on your setup (version, 32 vs 64, Qt version, anything else that may help?)?

Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

Re: modifying mudlet.cpp to add and expose a function to lua

Post by Lucky24 »

Well, lots of possible hangups and probably not worth researching. I compiled the 2.0 final, not 2.1 or your 2.0+ branch, and I'm using the old recipe for Windows' libs and QT and minGW versions.

Everything else seemed to work fine for my compile before I made any changes to the source; all my scripts worked, sounds worked, mapper worked, labels worked, etc. Thus, I assumed it was something stupid I was doing with the source and not the compile, else I wouldn't have made the thread.

Really this was just an exercise to see if I could make useful changes and compile and test them on windows.

So, just in case you are still wondering, running Windows 7 x64, compiling 2.0 final using the Recipe for Windows thread's QT 4.7.3 and the included mudlet_libs folder (with the addition of quazip as I posted in that thread yesterday).

I will try compiling 2.1 and on Ubuntu now that it seems to work on windows.

Edit: Also, I would attempt compiling for windows using your mingw instructions instead of the old thread's ones, but I'm a bit confused as to where you're getting your mudlet_libs from (just grabbing them from an existing windows install like I did for quazip maybe?)

P.S. Again, thanks for adding this :) I have been running on test4 for a year because I just recompiled it with only one musicbox player, and had issues recompiling newer versions on windows to test adding a proper stopSounds()

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: modifying mudlet.cpp to add and expose a function to lua

Post by chris »

I'll do a fresh go on a windows install sometime and document it. I build everything on my own using mingw and finally integrate with Qt Creator. Don't have time for a writeup atm but I'll get one together in the future.

Post Reply