简体   繁体   中英

lual_dofile(); wont load script with C++ and Lua

Hello i'm trying to get this code to work

extern "C"
{
#include <lua.h>
#include "lualib.h"
#include "lauxlib.h"
}

#include "glew.h"
#define GLEW_STATIC
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>




////////////////////////
////////////////////////
test.h
void Test(void)

{
    int status;

**//The Lua Interpreter**
lua_State  *L = lua_open();


**//Open Lua Libarys**
luaL_openlibs(L);



**//Run Lua Script**
status = luaL_loadfile(L,"Test.lua");

printf( "actually getting to this point!");
getchar();
//Close Lua
lua_close(L);



}

this is named test.lua this is my lua file

print"Whats your name?"

function sleep(n)
end

this doesn't work :(

lual_dofile(L,"Test.lua");

the hole program compiles but then doesn't execute the script or show any visual feedback of the lua script running has anyone encountered this problem before? and has any knowledge of why ?**

You seem to not understand a few things.

luaL_loadfile loads the script from a file, compiles it, but it does not run it. It just pushes it onto the Lua stack, so that you can run it with whatever parameters you see fit.

luaL_dofile loads the script from a file, compiles it, and runs it, thus popping the script off the stack.

In the case of errors, luaL_loadfile will return an error code explaining what kind of error it is. It also pushes an error message onto the Lua stack if there is an error. If no errors happen, it returns 0.

Similarly, luaL_dofile will return 1 if an error happens, and the error message will be on the Lua stack.

You should always be checking the return value of these functions to see if an error has happened, and acting accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM