Codea 2.3: Lua 5.3

The use of environments has also changed. For me, this is one of the bigger changes. The functions setfenv and getfenv don’t exist any more, and loadstring is replaced by load.

(I don’t know if @toadkick is still around; if so, you may be interested to know that I’ve adapted cmodule to Lua 5.3. Not sure how well I’ve done it, but it’s functional.)

@LoopSpace do you have a link to cmodule and how to use it?

@LoopSpace - does this affect that neat program for running multiple programs in different tabs? If so, I’d like to use it to fix a number of tutorial demos thar are going to break.

Here’s another way to get the result of 25.86:

local var = 25.86789
print(var-(var%0.01))

This will print the first 5 digits of pi.

local pi = math.pi
print(pi-(pi%.1^4))

@Ignatz - fair point. I misunderstood that the original goal was to round the number, not necessarily to output it.

(i have to say though that the example with integer division by @Simeon - that’s a bit extreme :slight_smile: )

@Simeon bitwise operators aren’t flagged as errors in 2.3(44).

Don’t forget the modf function.

function setup()
    var=25.86789
    print(math.modf(var*100+.5)/100)
end

Sorry @Simeon, but the post was a little confusing, which is now the correct way to use varargs?

@TheSolderKing use select, the arg variable doesn’t exist any more unless you explicitly create it by doing arg = {...}

Ah, thanks Simeon. So

function foo(...)
arg={...}
print(arg[1])
end

would work? Sorry for being a pain, just didn’t understand varargs much in the first place until recently :P.

Yes, that would work, though you’d probably want local arg = {...}

You could also use select instead (more efficient if you only want access to a specific argument)

function foo(...)
    local x = select(1, ...)
    print( x )
end

Here’s the manual entry for select

select (index, ···)

If index is a number, returns all arguments after argument number index; a negative number indexes from the end (-1 is the last argument). Otherwise, index must be the string "#", and select returns the total number of extra arguments it received.

@TheSolderKing Here’s some examples for arg and select.


function setup()
    useArg(1,3,5,7,9)
    print()
    useSelect(2,4,6,8,0)    
end

function useArg(...)
    arg={...}
    print("size of arg  ",#arg)
    for z=1,#arg do
        print("values of arg  ",arg[z])
    end   
end

function useSelect(...)
    a=select("#",...)
    print("size of ...  ",a)
    a=select(2,...)
    print("value of 2nd pos  ",a)
    a,b=select(3,...)
    print("values at pos 3 and 4  ",a,b)
end

@Ignatz Yes, I believe it does since those used setfenv. I think it is possible to emulate setfenv using the debug library, or if we make the invocation just a little more detailed. I’ll need to dig up the code and examine it.

@LoopSpace - that would be great if you have the time, thanks

@Ignatz just found it. The key question is whether you would sanction the use of the debug library or not. If not, then I think that the invocation has to be a little more wordy. Something like:

_ENV = localise()

instead of just

localise()

Thanks

Just been playing with your lighting step-by-step program. The following seems to work:

New localise function:

function localise(n,d)
    if d then tabDesc[n]=d end
    local t= {}
    setmetatable(t,{__index = _G})
    tabs[n] = t
    return t
end

Then change the invocation to:

if localise then _ENV = localise(1,"Ambient") end

and similar for the other tabs

brilliant, that saved me some time, thanks! =D>

@Simeon sorry to sound obsessed :stuck_out_tongue: but does this update include sockets, as they are included in Lua 5.3? The answer is probably no, just thought I’d ask anyways :smiley:

@TheSolderKing yes it does actually, codea 2.3 has the LuaSocket library (http://w3.impa.br/~diego/software/luasocket/introduction.html)