Note that there are some explanatory texts on larger screens.

plurals
  1. POLua 5.2::Create a different environment for each script
    text
    copied!<p>This is a simplified version of the problem that Im having:</p> <p>[ bot0.lua ]</p> <pre><code>function OnHit(bot) // Where the bot structure and vec3 // generated using SWIG and registered to a // "global" lua_State that I maintain. bot.color = vec3( 1.0, 0.0, 0.0) end; </code></pre> <p>[bot1.lua]</p> <pre><code>function OnHit(bot) bot.color = vec3( 0.0, 1.0, 0.0) end; </code></pre> <p>At loading time, for each script I first do the following (pseudo code):</p> <pre><code>bot-&gt;script-&gt;L = lua_newthread( &lt;Global lua_State where vec3 etc... are registered&gt; ) luaL_loadbuffer( bot-&gt;script-&gt;L, botXXX.lua ... lua_call( bot-&gt;script-&gt;L, ... </code></pre> <p>Then at runtime from my program when a bot get hit I call:</p> <pre><code>luaL_dostring( bot-&gt;script-&gt;L, "OnHit(GetBot(\"&lt;the bot name&gt;\")" ); </code></pre> <p>(Where GetBot is also a function registered in the global lua_State).</p> <p>The problem is that the bot always become green since bot1.lua OnHit is in the last bot file loaded. I thought by using lua_newthread I will be able to have a new definition of OnHit for each "thread" created that will assign the proper color to the bot since Im using a different lua_State when using luaL_dostring. But in fact the lastest OnHit loaded will overwrite the previous one in the the global lua_State environement.</p> <p>My question is: How can I create a different environment for each script so that the correct OnHit will be called for the correct bot->script->L and where I can still have access to all the functions (such as my vec3,GetBot etc... functions) that are registered inside the "global" lua_State environment that I create?</p> <p>[edit]</p> <p>After more research I think what I need is to be able to "sandbox" in C. Am I right?</p> <p>[edit]</p> <p>Actually not really sandboxing, since I want to that if the look up fail in the script environment, the lookup is made to the global environment. Is it even possible?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload