• Advertisement
Sign in to follow this  

Passing a lua array to a C++ function using luabind?

This topic is 3230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to pass a lua array to a C++ function that has been bound with luabind. Before I was able to just read from the table when I was using just lua but I don't know how to get an array in to my function now that I'm using luabind? Here is the C++ function:

void lua_SetVariable(UINT material, const char* variable, UINT count, ?? data ) // So data is suppose to be an array but what type works with luabind?
{
	((E_MATERIAL*)material)->Effect->GetVariableByName( variable )->SetRawValue( data, 0, count );
}








And here is how the function would be used in lua:
SetVariable( Mat02, "Diffuse", 4, {3.0, 3.0, 3.0, 1.0} )






Basically it's a function to set DirectX 10 shader variables. [Edited by - CodaKiller on April 16, 2009 3:55:31 PM]

Share this post


Link to post
Share on other sites
Advertisement
The documentations are here:
http://www.rasterbar.com/products/luabind/docs.html
Search for the following example:

void my_function(object const& table)
{
if (type(table) == LUA_TTABLE)
{
table["time"] = std::clock();
table["name"] = std::rand() < 500 ? "unusual" : "usual";

std::cout << object_cast<std::string>(table[5]) << "\n";
}
}




The last line shoes what you want:
object_cast<some_type>(table[index])

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
The documentations are here:
http://www.rasterbar.com/products/luabind/docs.html
Search for the following example:
*** Source Snippet Removed ***

The last line shoes what you want:
object_cast<some_type>(table[index])


I thought I understood but I can't get my function to work, here is my new code:


void lua_SetVariable(UINT material, const char* variable, UINT count, luabind::object const& data )
{
std::vector<float> floats;
for( UINT i = 0; i < count; i++ )
{
floats.push_back(luabind::object_cast<float>(data));// Fails on this line?
}
((E_MATERIAL*)material)->Effect->GetVariableByName( variable )->SetRawValue( &floats[0], 0, count );
}




Error message:

Quote:

First-chance exception at 0x7700f328 in Ether.exe: Microsoft C++ exception: luabind::cast_failed at memory location 0x0017f780..


[Edited by - CodaKiller on April 16, 2009 7:16:51 PM]

Share this post


Link to post
Share on other sites
Isn't the first (default) index of an lua array 1? I think data[0] is nil and the cast to float fails. try
for( UINT i = 1; i <= count; i++ )

Also it should be possible to get the "count" using lua's getn function.

Share this post


Link to post
Share on other sites
*facepalm* I don't know why I didn't think of that! So it's working now but would anyone know the exact command used to get the size of the table from the luabind::object?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement