ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 492 forks source link

How can I use usertype with metatable to set the global table? #1547

Open GasimGasimzada opened 8 months ago

GasimGasimzada commented 8 months ago

I need a way to somehow make it so that all tables and functions, except for one global variable is a noop operation. I have created a usertype with metatable that does nothing but returns itself for index and call metafunctions:

struct NoopMetatable {
  NoopMetatable call() { return {}; }

  NoopMetatable index() { return {}; }

  static void create(sol::state_view state) {
    state.new_usertype<NoopMetatable>(
        "Noop",
        // Call
        sol::meta_function::call_function, &NoopMetatable::call,

        // Index
        sol::meta_function::index, &NoopMetatable::index);
  }
};

This works well when I manually disable all global variables one-by-one:

sol::state state;
NoopMetatable::create(state);
state["entity"] = NoopMetatable{};
state["entity_query"] = NoopMetatable{};
state["entity_spawner"] = NoopMetatable{};
state["logger"] = NoopMetatable{};
state["ui"] = NoopMetatable{};
state["table"] = NoopMetatable{};

But this is not a scalable solution for me as I keep adding more and more global objects into the system. I want to somehow make the global "_G" table to be a metatable that returns NoopMetatable for everything. I tried it with `environment but it did not work:

sol::table globals = state.create_table();
globals[sol::metatable_key] = NoopMetatable{};
sol::environment env(state, sol::create, globals);
state.safe_script_file("test.lua", env);

I also tried to set it manually via table but it did not work either:

globals[sol::meta_function::index] = []() { return NoopMetatable{}; };
globals[sol::meta_function::call_function] = []() { return NoopMetatable{}; };

What am I doing wrong here?

Rochet2 commented 8 months ago

It would be nice to know what you are actually trying with this. There might be alternate existing solutions to achieve what you want.

Some of the problems you are hitting may be the following:

_G A global variable (not a function) that holds the global environment (see §2.2). Lua itself does not use this variable; changing its value does not affect any environment, nor vice versa.

__index: The indexing access operation table[key]. This event happens when table is not a table or when key is not present in table.

Additionally, you are trying to use an object as a metatable, which may not be supported. So maybe avoid using the usertype completely. Simply use normal tables for everything. For example, probably want to change globals[sol::metatable_key] = NoopMetatable{}; to actually refer to a table. Could you maybe tell what kind of errors and issues you are getting? Maybe some examples of how you try to invoke things in the global environment?

GasimGasimzada commented 8 months ago

It would be nice to know what you are actually trying with this. There might be alternate existing solutions to achieve what you want.

I have a method that I inject to my script:

local my_var = input_vars.register("My variable", input_vars.types.String);

During actual execution of script, this method will read the "My variable" from some place in my game engine and provide it to the script. However, I have another step that is before the execution of the script -- in my game engine's editor. The idea is that, if I load this script into my editor, my editor evaluates this script with a different logic for input_vars.register -- instead of reading the runtime data for the script, it will automatically create UI controls in the editor that allows the user of the editor to store variables for the script. For example, the following script:

local value = input_vars.register('name', input_vars.types.AssetPrefab)

Creates an input that allows drag and dropping an asset that is available from the script:

image

This part works great with no issues. But imagine a scenario where a script uses a lot of APIs:

local value = input_vars.register('name', input_vars.types.AssetPrefab)

entity.animator:trigger("RunAnimation")
logger.debug("Debug logs")
game.on_update:connect(function(dt)
  print('Update called')
end)

local command = entity.input:get_command("Jump")
local value = entity.input:get_value_boolean(command)

existing_entity = entity_query:get_first_entity_by_name("My entity name")
new_entity =  entity_spawner:spawn_empty()

The editor only needs to evaluate input_vars.register('name', input_vars.types.AssetPrefab) and ignore everything else. This is where I want to make it so that anything other than input_vars global variable returns an empty table where every function and value returns a recursive table that does nothing. This way, whatever the developer provides will not do anything. To achieve this, this is what I am currently doing:

sol::state state;

// Create usertypes
NoopMetatable::create(state);
InputVars::create(state);

state["entity"] = NoopMetatable{};
state["entity_query"] = NoopMetatable{};
state["entity_spawner"] = NoopMetatable{};
state["logger"] = NoopMetatable{};
state["ui"] = NoopMetatable{};
state["table"] = NoopMetatable{};
state["game"] = NoopMetatable{};
state["input_vars"] = InputVars{};

I want to automate this part in a way that any field that is not input_vars is always noop.


Could you maybe tell what kind of errors and issues you are getting? Maybe some examples of how you try to invoke things in the global environment?

I am getting no errors. It is just the environment data that I set just do not get applied.