Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


[LUA] setenv is nill?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 gabrielauditore   Members   -  Reputation: 100

Like
0Likes
Like

Posted 29 December 2011 - 07:18 PM

Hi guys, i'm trying to make some easy lua encapsulation code, so far i have a base class
-- Auto generate begin
BaseClass = {}
function BaseClass:new(object)
object = object or {}
setmetatable(object, self)
self.__index = self
return object
end
setmetatable(_G, {
__newindex = function(t, k, v)
  BaseClass[k] = v
end,
__index = function(t, k)
  return BaseClass[k]
end
})
-- Auto generate end
function PrintX(self)
setenv(1, self)
print (x)
end
-- Auto generate begin
setmetatable(_G, nil)
-- Auto generate end

and then a test file that uses it
variable1 = BaseClass:new()
variable2 = BaseClass:new()
variable1.x = 10
variable1:PrintX()
variable2:PrintX()

Basically anything within the --autogenerate comments would be written by a pre-processor macro, so end users could write code, and anything within a file would automatically be added to a class.

if you take a look inside the printX function i'm trying to set the current global environment to self, that is while the function is executing x should reference self.x (The setenv function would be added by a pre-processor as well).

However when i run this piece of code i get the following output attempt to call global 'setenv' (a nil value)
I think this is because instead of calling _G.setenv my code is actually calling BaseClass.setenv, but rawget(_G, "setenv") return nil

Any ideas on how to solve this issue?

EDIT, 2 things
First, i had a typo. It's setfenv not setenv.
Second the following code fails (I'm using the command line interpreter from the lua site)
function test()
local newgt = {}
setmetatable(newgt, {__index = _G})
setfenv(1, newgt)
end


Sponsor:

#2 gabrielauditore   Members   -  Reputation: 100

Like
0Likes
Like

Posted 29 December 2011 - 08:24 PM

Lua 5.2 no longer supports setfenv. It isntead has a _ENV variable




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS