Lua Interface in my Win32 C++ application
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Lua Interface in my Win32 C++ application

Started by cberg76, 01 November, 2004, 06:51:24

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

cberg76

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);
-------------------------------------------------------------------


Corayzon

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

cberg76

#2
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.

SMF spam blocked by CleanTalk