Click here to Skip to main content
15,877,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have difficulty to explain this problem, but I'll do my best.
I'm trying to refactor my code, moving away from classes with only static members, to regular functions in a certain namespace. One of such is my "Script" class, which handles Lua scripting for the game I'm trying to write. I use an external library called 'Sol' for the binding between C++ and Lua. The Script class has a private sol::state. It was some template functions along with some other code which all work just fine, such as "RunFile()" to run some script file.

The problem I get, is that the template function to Register a function to Lua, gets called but somehow doesnt work. I'm using the Sol library, in which you can use: m_lua[functionName] = function; to register a function to Lua. If I type this hardcoded m_lua[PushEvent] = PushEvent; it still works.

//Script.h
class Script
{

private:
	static sol::state m_lua;

...

template <typename T>
	static void RegisterFunction(std::string functionName, T function); //registers c++ functions to Lua
};

template <typename T>
void Script::RegisterFunction(std::string functionName, T function) //registers c++ functions to Lua
{
	m_lua[functionName] = function;
}

//Script.cpp
#include "Script.h"
sol::state Script::m_lua;


It works as I want, I can register a function to Lua using:
Script::RegisterFunction("PushEvent", Events::PushEvent);


For some reason, when I remove the class and put everything into a namespace Script instead of the class, and put the m_lua variable into an anonymous namespace, it no longer works:

<pre>
//Script.h
namespace Script
{
namespace { //anonymous namespace
	static sol::state m_lua;
}
...

template <typename T>
void RegisterFunction(std::string functionName, T function) //registers c++ functions to Lua
{
	m_lua[functionName] = function;
}

}


//Script.cpp
#include "Script.h"
sol::state Script::m_lua;



I don't understand this. I tought using regular functions rather then a class shouldn't make much of a difference.

The program compiles and links just fine, but the functions do not get registered to Lua, so I get a runtime error that it cannot find the function name (PushEvent for example). It really boils down to this RegisterFunction template, and I can not get my head around it why it shouldn't just work.
During debugging all the functions get called (m_lua[functionName] = function; gets executed when I walk over the code).

If anyone knows an answer it would be highly appreciated!

What I have tried:

Changing back to a class, which works fine.
Posted
Updated 3-Feb-23 9:15am
Comments
Richard MacCutchan 3-Feb-23 12:13pm    
Without seeing the code that actually gets executed it's difficult to guess why that is failing. Do you use the full namespace::functionname in the calls to RegisterFunction?
robvleugel 3-Feb-23 12:47pm    
Yes namespace::RegisterFunction(SomeFunc, SomeFunc).

In the execution part, I haven't changed anything. Except the Script:: part is now from the namespace, instead of the class name. You can call a function by Namepace::func() or a static member function by Classname::func() likewise.

There is not so much I changed in the code, which is why I don't understand this won't work when I put the code in a namespace instead of a class with static member functions that I never instantiate.
Richard MacCutchan 3-Feb-23 13:39pm    
It's still difficult to see why this does not work. And without the full code and the library itself there is no way to gather more information. I'm afraid you will need to do some deeper debugging.

1 solution

My mind was getting foggy after trying to figure this problem out for hours, couldn't think straight anymore. That in part explains my vague question here, sorry about that.

I solved it by moving the variable from the header to the source file, this was the culprit.
For the templates I defined I added a Get() function to access the variable.

Thanks for your help nonetheless Richard!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900