pro-mos

[Help] Custom script loader [Lua environment]

Recommended Posts

I've edited bonsai's script wrapper and using it, it looks like this:

local env = {}
local _createObject = createObject
local _setTimer = setTimer
local _addEventHandler = addEventHandler

outputChatBox = function() end
triggerServerEvent = function() end
createObject = function(...)
	local object = _createObject(...)
	setElementDimension(object, dimension)
	table.insert(wrapper.objects, object)
	return object
end

function newClientEnvironment()
	env = table.copy(_G, true)

	local metatable = {
		__index = function(self, index)
			return rawget(_G, index)
		end,

		__newindex = function(_G, index, value)
			rawset(_G, index, value)
		end
	}

	setmetatable(env, metatable)
  _outputChatBox('created new Environment')
end

and this is how a script is loaded:

function loadScript(scriptData)	
  local loaded = loadstring(scriptData)
  setfenv(loaded, env)
  local ex = pcall(loaded)
end

The problem is the functions re-defined above interfere with my main script that is using this wrapper. i can just use this wrapper as a separate script but then i'll have to reload two scripts rather than 1, and i dont want that.

any ideas? and thank you for reading

Edited by pro-mos

Share this post


Link to post

What do you mean with "reload two scripts"?

If you make it a seperate resource that only handles script loading there shouldn't be any problems, and I assume its safer too.

Also, that script back then was pretty messy. :(

And very important, don't use that for server side scripts.

Share this post


Link to post
16 minutes ago, Bonsai said:

What do you mean with "reload two scripts"?

If you make it a seperate resource that only handles script loading there shouldn't be any problems, and I assume its safer too.

Also, that script back then was pretty messy. :(

And very important, don't use that for server side scripts.

Hi :D

i would like it to be on the same resource, so when im testing, i /restart only 1 resource, i tried to insert the modified functions into 'env' like

env.addEventHandler = ...

env.outputChatbox = ...

everytime the  environment is created. but that doesn't work :( 

Share this post


Link to post

If you do something like this in your main resource:

<include resource="other_resource" />    

then it will start and stop along with the main resource.

I wouldn't do it in one big resource.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.