Member
[WoW][3.0.9] A way to get return values from Lua Functions
Well, it is a pretty simple way of getting return values, and there certainly are better ways.
But however, this is a quick way to do it.
As you should know, using Lua_DoString the return values are popped off the stack before you can parse them, and this is due to a setting that wow uses when calling lua_pcall.
However, if you call it inside a registered function, the return values aren't popped off the stack until outside the function.
So, you can do is register your own Lua_CFunction and execute your Lua functions in it and then pass the return values to a vector. (Don't forget to patch the invalid pointer check.)
Something like this:
Code:
//wherever you want
std::vector<const char*> vResults;
//from lua sdk
typedef int ( __cdecl * Lua_CFunction )( void * pState );
//our own function
int OurFunction( void * L )
{
//clear the vector if its not empty
if ( !vResults.empty() )
vResults.clear();
//get count of returns on stack
int n = lua_gettop(L);
//loop to retreive these
for (int i = 1;i<=n; i++)
{
//using lua_tostring to get the result
const char * pszResult = Lua_tostring(L, i, NULL);
//make sure its valid
if (pszResult && pszResult[0])
{
//add result to vector
vResults.push_back( pszResult );
}
}
//returning 0 args
return 0;
}
//register our function
Lua_Register( "OurFunction", OurFunction );
//get return values like this:
Lua_DoString("OurFunction(GetTotalAchievementPoints())","OurFunction(GetTotalAchievementPoints())", NULL);
//converting from const char * to int correctly( quick c style )
int iAchievementPoints = atoi(vResults[0]);
I just put in some quick comments! I don't think they matter too much, it is all simple enough.
I hope this helps someone out, if you have any problems feel free to ask.
Thanks to Bobbysing for helping me out countless times!
:wave:
These ads disappear when you log in.