• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Global variables don't change how they should [Cyko help^^]

Summ

(\/)(;,,;)(\/) Y not?
Staff member
Global Moderator
Joined
Oct 15, 2008
Messages
4,136
Solutions
12
Reaction score
1,115
Location
Germany :O
Hey guys

I have a problem with a global variable.
I created a new file 200-globals.lua in libs. If I declare a variable like:
LUA:
storage = 50
and change the variable to 60 on login or other creaturescript, there is a problem.
If I now call the variable in actions script it is still 50.

Regards
Summ
 
I didn't find a way to fix this:
Code:
In static member function `static lua_State* LuaInterface::getSharerState()': 
cannot call member function `bool LuaInterface::loadFile(const std::string&, Npc*)' without object

It is this line:
[cpp] if(loadFile(std::string(datadir + "global.lua")) == -1)[/cpp]
 
I wanted to try if the shared lua states work and so I update the code: http://otland.net/f35/shared-lua-states-55726/#post564976
or you can show me another way to make the variables global for all sections?

[cpp]lua_State* LuaInterface::getSharerState()
{
static lua_State* sharer;
if(!sharer)
{
sharer = luaL_newstate();
#ifndef __USE_LUALIBRARIES__
//Here you load only the "safe" libraries
luaopen_base(sharer);
luaopen_table(sharer);
luaopen_os(sharer);
luaopen_string(sharer);
luaopen_math(sharer);
// Unsafe, but immensely useful
luaopen_debug(sharer);
#else
//And here you load both "safe" and "unsafe" libraries
luaL_openlibs(sharer);
#endif
std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);

if(loadFile(std::string(datadir + "200-pets.lua")) == -1)
{
std::cout << "Warning: [LuaScriptInterface::initSharedState] Can not load " << datadir << "200-pets.lua." << std::endl;
}
}
return sharer;
}[/cpp]
 
Last edited:
so my guess is getSharerState is static member function, and that means you can't use any member functions unless theyre static too, so make loadFile static also you will run into more problems because that way is shitty, you could just make the "lua_State* m_luaState" to "static lua_State* m_luaState" then declare it in the .cpp file
 
I removed the static before "lua_State* getSharerState();" and I could compile it but after all it crashes now when loading script system.
Can't you code something so it makes it work xD? It very important
 
i told you to remove it and replace "lua_State* m_luaState" in luascript.h to "static lua_State* m_luaState" then in luascript.cpp at the beginning of the file or anywhere put "lua_State* LuaInterface::m_luaState" then in constructor "LuaInterface::LuaInterface" remove "m_luaState = NULL;"then in member function "LuaInterface::initState()" replace this:
Code:
m_luaState = luaL_newstate();
with this:
Code:
if (!m_luaState) { m_luaState = luaL_newstate(); }
and ur done
 
Where I have to put: lua_State* LuaInterface::m_luaState ?

Another question:
Can't I just edit "bool LuaInterface::initState()" somehow to get the same effect?
 
Last edited:
Ok ty now the server starts but it has many errors:

Like this:
Code:
[14:12:30.198] [Error - GlobalEvent Interface]
[14:12:30.199] data/globalevents/scripts/ctf.lua:onThink
[14:12:30.200] Description:
[14:12:30.201] attempt to call a nil value
[14:12:30.203] stack traceback:
[14:12:30.204] [Error - GlobalEvents::think] Couldn't execute event:

Wouldn't it be possible to make the normal lua state the same for all scripts?
 
Doesn't work since every function (onSay, onThink, onUse etc.) is not declared (nil value)
I just made these 3 changes or so you told me.
 
[cpp]bool LuaInterface::initState()
{
if (!m_luaState) { m_luaState = luaL_newstate(); }
if(!m_luaState)
return false;

luaL_openlibs(m_luaState);
#ifdef __LUAJIT__
luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
#endif

registerFunctions();
if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), NULL))
std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;

lua_newtable(m_luaState);
lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
m_runningEvent = EVENT_ID_USER;
return true;
}[/cpp]
 
Code:
bool LuaInterface::initState()
{
	if (!m_luaState) { m_luaState = luaL_newstate(); }
	luaL_openlibs(m_luaState);
#ifdef __LUAJIT__
	luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
#endif
 
	registerFunctions();
	if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), NULL))
		std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;
 
	lua_newtable(m_luaState);
	lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
	m_runningEvent = EVENT_ID_USER;
	return true;
}
try this
 
Nah still every event is nil:

Code:
[15:46:55.164] [Error - TalkAction Interface]
[15:46:55.164] data/talkactions/scripts/teleporttown.lua:onSay
[15:46:55.164] Description:
[15:46:55.165] attempt to call a nil value
[15:46:55.165] stack traceback:

[15:46:55.272] [Error - NpcScript Interface]
[15:46:55.272] data/npc/scripts/oldrak.lua:onThink
[15:46:55.273] Description:
[15:46:55.273] attempt to call a nil value
[15:46:55.273] stack traceback:
 
Back
Top