Unable to set new global environments

In lua engines like pico8 you can swap between the global environment and any user defined ones. A basic setup in pico8 is:

_G = _ENV; _G.__index = _G --header

player={x=30};setmetatable(player,_G)
do
	local _ENV=player
	function update()
		x+=1
	end
	function draw()
		circ(x,63,8)
	end
end

function _update() player:update() end
function _draw()cls(1) player:draw() end --displays circle moving right

I’m not having any luck translating this into solar2d. It’s not accepting the header line: _G = _ENV; _G.__index = _G
In solar2d it seems _G exists as the default environment name.
Whereas pico8 it’s _ENV. Swapping the names in the code above will move it past the header but the read of the code won’t work, it’ll recognize x as a nonexistant global.

It is not a common practice to reference the environments in Solar2D.

I’m not sure what you are exactly trying to achieve, but I think that what you are looking for is getfenv() and setfenv(), try:

_G = getfenv(); _G.__index = _G --header

Pico8 uses its own lua version, while Solar2D uses Lua 5.1, so you can look at any docs referencing the standard Lua 5.1 and it should work in Solar2D.

The outstanding problem is this line local _ENV=player. In the OP for pico8 it is the necessary component to getting most of this to work.

The usage+examples for pico8 are well out-lined here: Using _𝘦𝘯𝘷 in PICO-8
This is the core example I need to get working. Examples like below are not valid in solar2d even if I change env to _G:

player={}

do
  local _𝘦𝘯𝘷=player
  x=10
  y=20
  lives=3
  sp=1
end --player={x=10,y=20...,sp=1}

The best I could get was the below two in solar2d. The second is the best but it will not allow me to store anything in obj like obj={x=xx,y=yy} and then call them like x , y Because that local _ENV=obj line doesn’t work…that’s the line that lets u do it in pico8.

--ENV EXAMPLE #1
CAT={__index = _G}
player={x=30,y=40}; setmetatable(player,CAT)
-- local x=1000 --main worry is this...if we have the same variable name as a local with overlappipng scope
function update()
	x=x+100
end; setfenv(update,player)
player.update() print(player.x) --130


--ENV EXAMPLE #2
CAT={__index = _G}
local h=100
local bullets={}
local function make_bullet(xx,yy) --parameters must be unique
	local obj={}; setmetatable(obj,CAT)
--local _ENV=obj --the line that doesn't work but would allow u in pico8 to write obj={x=xx} above
	x=xx or -1 y=yy or -2 r=3 --init var must not overlap with scope of a local var of same name
	function update()
		x=x+ h
	end
	table.insert(bullets,obj)
end
make_bullet(1,2) bullets[1].update() print(bullets[1].x) --1+100=101

The local _ENV=obj line in example#2 above can be substituted with setfenv(make_bullet,obj), so that can work similar to pico8 now in that format. The problem is setfenv only applies to functions, so I’m still stuck.

--ENV EXAMPLE #2
CAT={__index = _G}
local h=100
local bullets={}
local function make_bullet(xx,yy) --parameters must be unique
	local obj={x=xx,dmg=5}; setmetatable(obj,CAT); setfenv(make_bullet,obj)
	hp=900
	function update() x=x+ 1 end
	table.insert(bullets,obj)
end
make_bullet(-10,2)

--local tik=0; Runtime:addEventListener("enterFrame", function(event) tik=event.frame
	for i=1,#bullets do
		bullets[i].update()
		print(bullets[i].x) --x=-10; x = -10+1 = -9
		print(bullets[i].dmg) --5
		print(bullets[i].hp) --900
	end
-- end)