Hello,
I am new to this forum. I have a few questions about building a custom Lua Interface within my C++ Win32 application. To assist me, I bought the book "Programming in Lua".
For now, I am using Lua to read a script and initialize specific variables in my application. I managed to make the following functions to read in float data types, but I am having problems reading in a string data type.
If I am not doing something correct, please point it out so that I can learn a better solution. But for now, I am most concerned with reading in a string from the Lua script and setting my string variable in C++ without getting bad pointers or compile errors.
--Thank You.
--------------------------------------------------------------------
First my script looks like this:
--Lua Script
--Initialization File for Win32 Application in C++
--Test Float value
pi = 3.1487501
--Test String value
stringvar = hello
-----------------------------------------------------------------
Here is an example of the functions that works to read and store a float data type:
void getDataFloat( char *var, float *pointer )
{
//get variable name from Lua script file
lua_getglobal(L, var);
//set float value to variable
*pointer = (float)lua_tonumber(L, -1);
}
Here is how I am calling this function in my program:
float testFloat;
getDataFloat("pi", &testFloat);
----------------------------------------------------------------
Now, here is the getDataString function that I cannot get to work:
void getDataString( char *var, char *pointer )
{
//get variable name from Lua script file
lua_getglobal(L, var);
//set integer to a pointer
pointer = (char*)lua_tostring(L, -1);
}
Here is how I am calling my bad function:
char inputString;
getDataString("stringvar", &inputString);
-------------------------------------------------------------------
hey cberg76,
--Lua Script
--Initialization File for Win32 Application in C++
--Test Float value
pi = 3.1487501
--Test String value
stringvar = hello
stringvar = hello should be stringvar = "hello"
u should know that without the quotations it tries to call
a function or get a variable setting ;)
let me know if thats it dude
noza
Well... I happened to open the book and noticed I wasn't creating the strings correctly as you noticed as well. It was pretty late when I gave up on it last night :)
I also had to change the function as I was creating a single char and not a pointer to char. I will post the working code, below. If anybody has a problem with the way I am doing things for this Win32 app. your suggestions would be much appreciated.
Here is the new working function:
void CLuaEmbed::getDataString( char *var, char **pointer )
{
//get variable name from Lua script file
lua_getglobal(L, var);
//set integer to a pointer
*pointer = (char*)lua_tostring(L, -1);
}
Here is how I am calling the function:
char *inputString;
getDataString("stringvar", &inputString);
-------------------------------------------------------------
Here is the new test script:
--Lua Script
--Initialization File for The Hog Engine
--Display Window
width = 1280
height = 800
--Test Float value
pi = 3.1487501
--Test String value
stringvar = "hello"
This implementation was much easier than writing my own text parser. I was wondering about the getglobal function and whether that would slow my program much. If anybody has a better method I would really appreciate your opinions and suggestions.
Thank You.