Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Jonius7 on April 04, 2012, 02:27:10 am

Title: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 04, 2012, 02:27:10 am
Hi everyone,
I haven't been active much on the forums in the past few days (in comparison with the past few months), but I have created a simple Lua program while experimenting on oclua. I have transferred the code into a Lua file on the computer and here it is!
Basically you start with a value of 0 and you can choose to Attack or Defend to try and increase your value. right now I am experimenting with the points system as Attack carries no more risk than Defence and both are as equally likely to win as to lose.

Here is also the source code. Feel free to look through it, comment on my program, how my coding is, any improvements on optimising it or making it easier for another programmer to read, and stuff. The more feedback I get the more I can use the feedback to broaden my understanding of Lua!
Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--
v=0
t=0
r1=0
r2=2
r3=0
r4=2
function on.paint(gc)
gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
gc:drawString(v,10,30,"top")
if r2==1 then
gc:drawString("Attack was successful. +" .. r1,10,50,"top")
end
if r2==0 then
gc:drawString("Attack was unsuccessful. -" .. r1,10,50,"top")
end
if r4==1 then
gc:drawString("Defence was successful. +" .. r3,10,50,"top")
end
if r4==0 then
gc:drawString("Defence was unsuccessful. -" .. r3,10,50,"top")
end
gc:drawString("Turn " .. t,200,10,"top")
if v~=0 or t~=0 then
gc:drawString("Average " .. v/t,200,30,"top")
end
gc:setFont("sansserif","r",8)
gc:drawString("Numstrat - Jason Ho",10,200,"top")
end
function on.charIn(ch)
cha=ch
if cha=="a" or cha=="d" then
if cha=="a" then
r4=2
r1=math.random(4,5)
r2=math.random(0,1)
if r2==1 then
v=v+r1
end
if r2==0 then
v=v-r1
end
end
if cha=="d" then
r2=2
r3=math.random(1,3)
r4=math.random(0,1)
if r4==1 then
v=v+r3
end
if r4==0 then
v=v-r3
end
end
t=t+1
end
if cha=="r" then
v=0
t=0
r1=0
r2=2
r3=0
r4=2
end
platform.window:invalidate()
end

With my current knowledge, I think a Lua version of my TI-nspire Basic Game, Jason's TI-nspire Hold'em is achievable. I just need to get working on it, persistently ;)

Thanks everyone!
Title: Re: Numstrat - Stumbling into Lua
Post by: Nick on April 04, 2012, 02:50:13 am
looks good. I've tried it and it runs fine. But there are some things i would change:
- make the average value round (floor or ceil whatever, but it does not now so it dissapears partly from screen because it's too long)
- why do you do "cha = ch" ? since you could just be using the ch from your argument, couldn't you?
- i would make a on.create() which contains all the r1=0, r2=2 etc so that when you press "r" you can just call on.create() and you don't have to type it twice.
- maybe make a table with those value instead of 5 different variables. Like r1 = table[1] etc. but then you have to know what value is at what place

These are just thought, it's already great as is :)

edit: and why do you do "if cha=="a" or cha=="d" then if cha=="a" then.." ? that's just twice the same statement that follows, this only takes time and space :)
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 04, 2012, 02:59:18 am
Thanks Nick! That's a great summary of things to fix.
Most of that stuff I was partly aware of already, but just hadn't put in yet.
- So how about if I wanted to display the average up to 2 decimal places? i.e. Fix2?
- cha=ch probably because I changed it from a getchar program I got from jimbauwens and just left it there
- Yes on.create() was what I was looking for thanks
- For a table is that different to a matrix/array/list?
- "if cha=="a" or cha=="d" then if cha=="a" then.." I think I did that because there is t=t+1 at the end. But it would make more sense (and save more space), to put t=t+1 inside the if cha=="a" and if cha=="d" I think then.

Thanks for the help!
I used http://wiki.inspired-lua.org/ for some help.
Title: Re: Numstrat - Stumbling into Lua
Post by: Nick on April 04, 2012, 06:04:50 am
to mak eit only display 2 decimals after the comma, you can do math.floor(average*10)/10. I don't know if there's another way to do this, but that's my way :) And i don't think there's an easy way to make it only have to decimals at all...

and i think it's faster to put twice t = t+1 than if since if is a statement, the other one just an addition, which is faster

a table is te same as a list in lua, you don't have two dimensional things. just do e.g.
Code: [Select]
function on.create()
     vars = {0, 0, 0, 2, 0, 2}
end

--this must in the if ch=="a" thingie

if vars[3] = 0 then
     vars[1] = vars[1]+1
end
[code]

this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :)
 
[/code]
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 04, 2012, 06:37:07 am
to mak eit only display 2 decimals after the comma, you can do math.floor(average*10)/10. I don't know if there's another way to do this, but that's my way :) And i don't think there's an easy way to make it only have to decimals at all...

and i think it's faster to put twice t = t+1 than if since if is a statement, the other one just an addition, which is faster

a table is te same as a list in lua, you don't have two dimensional things. just do e.g.
Code: [Select]
function on.create()
     vars = {0, 0, 0, 2, 0, 2}
end

--this must in the if ch=="a" thingie

if vars[3] = 0 then
     vars[1] = vars[1]+1
end
[code]

this might be a bit more timeconsuming to type, but it think it's more beautiful to see, rather than 5 vars :)
 
[/code]

Thanks Nick, I shall update this if I have time.
From what I gather table is equivalent to list
and an array is equivalent to matrix
Thanks!
Hopefully this will just be the beginning of my Journey in Lua :D
Title: Re: Numstrat - Stumbling into Lua
Post by: Jim Bauwens on April 04, 2012, 07:21:01 am
Lua doesn't have list, arrays or matrices.
The only thing similar are tables, and tables blow all of the other things away.
You can easily combine tables to get a matrix:

Code: [Select]
matrix= {
    {1,2,3},
    {4,5,6},
    {7,8,9}
}

print(matrix[2][3]) -- prints 6

You can put any lua object in a table, and you can use any object as a key.
Lua tables can also be manipulated by metatables and metamethods, making them extremely cool. But that is a bit more advanced :)
Title: Re: Numstrat - Stumbling into Lua
Post by: someone on April 04, 2012, 08:52:28 pm
I redefined some variables, I think is more legible this way:

Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--

chance = { [-1] = "",
            [0] = "unsuccessful . -",
            [1] = "successful . +" }

action = { nothing="", attack="Attack", defense="Defense"}

function initialize_variables()
    v = 0
    t = 0
    r1 = 0
    r2 = -1
    state = action.nothing
end

--function on.create()
initialize_variables()
--end

function on.paint(gc)
    gc:setFont("sansserif","r",11)
    gc:setColorRGB(0,0,0)
   
    gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
    gc:drawString(v,10,30,"top")

    if state == action.attack then
        gc:drawString("Attack was "  .. chance[r2] .. r1, 10, 50, "top")
    elseif state == action.defense then
        gc:drawString("Defense was " .. chance[r2] .. r1, 10, 50, "top")
    end

    gc:drawString("Turn " .. t,200,10,"top")
    if v~=0 or t~=0 then
        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")
    end
    gc:setFont("sansserif","r",8)
    gc:drawString("Numstrat - Jason Ho",10,200,"top")
end

function on.charIn(ch)
    if ch=="a" then
        state = action.attack
        r1=math.random(4,5)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1
    end

    elseif ch=="d" then
        state = action.defense
        r1=math.random(1,3)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        state=action.defense
        t=t+1
    end

    elseif ch=="r" then
        initialize_variables()
    end

    platform.window:invalidate()
end

function round(value, digits)
    return string.format("%." .. digits .. "f", value)
end
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 07, 2012, 07:34:18 pm
Thanks someone, though I'm don't completely understand sections of it.
So in your version "state" plays an important part here. and I also see "chance" defined as a table?
Title: Re: Numstrat - Stumbling into Lua
Post by: someone on April 08, 2012, 02:20:22 pm
"action" was used to classify which actions can be performed when you press a key. I'm not sure if you intend to add more command actions (like flee, heal, steal, etc.), but if you do, you can now easily add them on the table.

"state", behaves as a filter event. It keeps track of the action executed. Then you can control in your program what to do when something happened.

"chance" was created to reduce the amount of lines. Since attack & defense behave alike, you can group them. I added the blank one just in case you want to create something like a "miss" or so. This is an ordered table, so when the variable r2 = 1, chance[r2] is the same as "successful . +"


This link might be helpful for understanding more about how to use tables
http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables (http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables)
Title: Re: Numstrat - Stumbling into Lua
Post by: linuxgeek96 on April 08, 2012, 04:56:47 pm
any easy way to rotate a table 90 degrees?
Title: Re: Numstrat - Stumbling into Lua
Post by: cyanophycean314 on April 08, 2012, 05:58:45 pm
any easy way to rotate a table 90 degrees?

What exactly do you mean by that?

You can transpose matrices I guess.
Title: Re: Numstrat - Stumbling into Lua
Post by: DJ Omnimaga on April 08, 2012, 06:03:29 pm
From what I remember this was possible in TI-83+ BASIC, although it was rather slow and would only let you rotate the matrix in one direction (meanign rotating the opposite way took 3 times more time, as you had to use the transpose command 3 times), so I wouldn't be surprised if it was possible on the Nspire too.
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 08, 2012, 08:23:00 pm
"action" was used to classify which actions can be performed when you press a key. I'm not sure if you intend to add more command actions (like flee, heal, steal, etc.), but if you do, you can now easily add them on the table.

"state", behaves as a filter event. It keeps track of the action executed. Then you can control in your program what to do when something happened.

"chance" was created to reduce the amount of lines. Since attack & defense behave alike, you can group them. I added the blank one just in case you want to create something like a "miss" or so. This is an ordered table, so when the variable r2 = 1, chance[r2] is the same as "successful . +"


This link might be helpful for understanding more about how to use tables
http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables (http://nixstaller.sourceforge.net/manual/0.2/nixstaller_9.html#Tables)

Thanks someone, it seems you know a great deal about Lua. Have you got or are planning any Lua projects yourself?

Also it would probably be possible to transpose matrices, you would somehow do a for .... do command to transpose x and y coordinates of each value in the matrix over to the new coordinates.
Title: Re: Numstrat - Stumbling into Lua
Post by: someone on April 09, 2012, 04:05:48 pm
Well, I've been reading some documentation regarding LUA & Nspire LUA, so I know it more or less. I would say like at intermediate level...

As for projects, I recently remade a Nonogram program, that you can check on its topic:
http://ourl.ca/15731
Title: Re: Numstrat - Stumbling into Lua
Post by: cyanophycean314 on April 11, 2012, 02:18:20 am
I think he's talking about your own work. I know you've been doing some edits, optimizations, and remakes, but you could try making your own project.  ;)

As a suggestion, you could try making a Lua sudoku. It'd be easy enough and it'd be pretty nice.  :D
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 11, 2012, 07:34:12 am
I think he's talking about your own work. I know you've been doing some edits, optimizations, and remakes, but you could try making your own project.  ;)

As a suggestion, you could try making a Lua sudoku. It'd be easy enough and it'd be pretty nice.  :D

Well kind of cyano. I was talking about any project anywhere that someone started and even though it is from someone else's project I'd still count it. Very nice..

And good idea for the Lua Sudoku.
I'm possibly leaning towards making some card games in Lua including Texas Hold 'em Poker (porting my Basic version), Hearts Solitare etc.
Title: Re: Numstrat - Stumbling into Lua
Post by: Nick on April 11, 2012, 04:08:36 pm
For card games you might learn a lot from chockosta. He made the freecell clone, which is really nice and runs perfect. dragable cards, checking for values and freee places etc.
Title: Re: Numstrat - Stumbling into Lua
Post by: Adriweb on April 11, 2012, 05:19:32 pm
Levak made an excellent Lua sudoku btw (quite advanced/complex Nspire Lua though) available here :
http://levak.free.fr/ftp/nspire/Sudoku/ (http://levak.free.fr/ftp/nspire/Sudoku/)
Title: Re: Numstrat - Stumbling into Lua
Post by: cyanophycean314 on April 12, 2012, 04:04:24 am
Where do all these Lua programs pop out of?  ??? There always seems to be unreleased programs hiding everywhere...
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 14, 2012, 09:47:05 pm
For card games you might learn a lot from chockosta. He made the freecell clone, which is really nice and runs perfect. dragable cards, checking for values and freee places etc.

Yeah I've learnt a bit from him already. He has lots of nice Lua programs
Cyano too :D
And it does take an effort to release programs, so there would be many development/unreleased programs lying around.

Anyway the code by someone:
Spoiler For Code by someone:
I redefined some variables, I think is more legible this way:

Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--

chance = { [-1] = "",
            [0] = "unsuccessful . -",
            [1] = "successful . +" }

action = { nothing="", attack="Attack", defense="Defense"}

function initialize_variables()
    v = 0
    t = 0
    r1 = 0
    r2 = -1
    state = action.nothing
end

--function on.create()
initialize_variables()
--end

function on.paint(gc)
    gc:setFont("sansserif","r",11)
    gc:setColorRGB(0,0,0)
   
    gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
    gc:drawString(v,10,30,"top")

    if state == action.attack then
        gc:drawString("Attack was "  .. chance[r2] .. r1, 10, 50, "top")
    elseif state == action.defense then
        gc:drawString("Defense was " .. chance[r2] .. r1, 10, 50, "top")
    end

    gc:drawString("Turn " .. t,200,10,"top")
    if v~=0 or t~=0 then
        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")
    end
    gc:setFont("sansserif","r",8)
    gc:drawString("Numstrat - Jason Ho",10,200,"top")
end

function on.charIn(ch)
    if ch=="a" then
        state = action.attack
        r1=math.random(4,5)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1
    end

    elseif ch=="d" then
        state = action.defense
        r1=math.random(1,3)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        state=action.defense
        t=t+1
    end

    elseif ch=="r" then
        initialize_variables()
    end

    platform.window:invalidate()
end

function round(value, digits)
    return string.format("%." .. digits .. "f", value)
end


Is a bit advanced for me. So I'll create things my own way and see if I can implement those things in as I go.
Title: Re: Numstrat - Stumbling into Lua
Post by: someone on April 16, 2012, 11:16:18 am
Well, I agree that the first part was a little abstract, so I removed it.

If you have any question on this code, be free to ask.

Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--

-- Call this function whenever you want to set the variables to their original values
function initialize_variables()
    v = 0
    t = 0
    r1 = 0
    r2 = -1
    state = ""
end

--function on.create()
initialize_variables()
--end

function on.paint(gc)
    gc:setFont("sansserif","r",11)
    gc:setColorRGB(0,0,0)
   
    gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
    gc:drawString(v,10,30,"top")

    if state == "attack" then
        if r2 == 1 then
            gc:drawString("Attack was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
        end
    elseif state == "defense" then
        if r2 == 1 then
            gc:drawString("Defense was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
        end
    end

    gc:drawString("Turn " .. t,200,10,"top")
    if v~=0 or t~=0 then
        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")
    end
    gc:setFont("sansserif","r",8)
    gc:drawString("Numstrat - Jason Ho",10,200,"top")
end

function on.charIn(ch)
    if ch=="a" then
        state = "attack"
        r1=math.random(4,5)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1

    elseif ch=="d" then
        state = "defense"
        r1=math.random(1,3)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1

    elseif ch=="r" then
        initialize_variables()
    end

    platform.window:invalidate()
end

--This function rounds the value passed depending on the number of digits needed
function round(value, digits)
    return string.format("%." .. digits .. "f", value)
end
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on April 19, 2012, 01:45:27 am
I noticed that the number of lines has increased a bit from 67 to 84. But I guess your modification does allow for expansion if I wish.
Thanks for all your help someone.
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on September 10, 2012, 01:49:56 am
I'm back. I looked back at a copy I had a few days ago of the code that someone put in above and I get what it's saying.
I mostly get the first version here with the use of tables (which I'll have to familiarise myself with)
http://ourl.ca/15758/295401
Title: Re: Numstrat - Stumbling into Lua
Post by: Twilight Sparkle on September 18, 2012, 11:34:33 am
Hi, just wanted to note that you can change
Code: [Select]
    if state == "attack" then
        if r2 == 1 then
            gc:drawString("Attack was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
        end
    elseif state == "defense" then
        if r2 == 1 then
            gc:drawString("Defense was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
        end
    end

to

Code: [Select]
if state ~= "" then
gc:drawString((state=="attack" and "Attack" or "Defense") .. " was " .. (r2==0 and "unsuccessful. -" or "successful. +") .. r1, 10, 50, "top")
end

Makes it a bit smaler :D
Title: Re: Numstrat - Stumbling into Lua
Post by: Jim Bauwens on September 18, 2012, 11:42:54 am
Hmm, optimization's like that are good indeed.
I would also recommend you to *always* use at least a simple screen manager, it makes life easier for the developer, and it's easier for him to make the game look better too :)
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on September 22, 2012, 09:29:34 pm
Hmm I like that optimisation. I am still working on Lua programming, just a bit slowly.
And Jim Bauwens, what do you mean by a screen manager?
Title: Re: Numstrat - Stumbling into Lua
Post by: Twilight Sparkle on September 23, 2012, 07:08:17 am
A screen manager is something that allows you to devide your Lua applications in several pieces.
Basically, you will have a paint handler for every scene in your program, without needing to put all your stuff in on.paint.

Here is a simple screen manager, so you can understand how it works a bit:
Code: [Select]

------ Screen Manager -------

-- In this table we will contain all 'screen' objects.
Screens = {}

-- This function is used to push a screen into the above table (Screens)
function pushScreen(scrn)
    table.insert(Screens, scrn)
    platform.window:invalidate()
end

-- This function is used to remove the last item in the 'Screens' table
function pullScreen()
    table.remove(Screens)
    platform.window:invalidate()
end

-- Return the last item of 'Screens', the current screen
function currentScreen()
    return Screens[#Screens]
end

-- This function will loop through all screens in the 'Screens' table and draw them
function paintScreens(gc)
    for _, screen in ipairs(Screens) do
        screen:paint(gc)
    end
end

-- Our screen object class
Screen = class()

function Screen:arrowKey() end
function Screen:paint()        end
function Screen:charIn()      end
function Screen:enterKey()  end

-- Link the Screen manager to 'on'

function on.paint(gc)              paintScreens(gc)                           end
function on.arrowKey(arrow) currentScreen():arrowKey(arrow) end
function on.charIn(ch)            currentScreen():charIn(ch)           end
function on.enterKey()            currentScreen():enterKey()          end

--- User code ----
popup = Screen()

function popup:paint(gc)
    gc:setColorRGB(255,255,255)
    gc:fillRect(50,50,200,100)
    gc:setColorRGB(0,0,0)
    gc:drawRect(50,50,200,100)
    gc:drawString("Hey!", 52, 50, "top")
    gc:drawString("Press enter to hide this popup", 52, 80, "top")
end

function popup:enterKey()
    pullScreen()
end


main = Screen()

function main:paint(gc)
    gc:drawString("Hello World! Press enter :D", 10, 10, "top")
end

function main:enterKey()
    pushScreen(popup)
end

pushScreen(main)

Adriweb and Jim Bauwens created a more advanced screen manager together with a nice widget API, you can find it on the github page for EEPro I think.
Title: Re: Numstrat - Stumbling into Lua
Post by: Adriweb on September 23, 2012, 08:04:03 am
Indeed, that's ETK. Jim created the core of it.
You can find the screen + widgets etc. here 
https://github.com/adriweb/EEPro-for-Nspire/blob/master/Global%20Libraries

But that's far more advanced and probably too complex for simpler usecases than FormulaPro (eepro).
Title: Re: Numstrat - Stumbling into Lua
Post by: Jim Bauwens on September 23, 2012, 08:53:54 am
Jonius, that code that Twilight gave you should point you in the right direction :) (Thanks Twilight!)
You can also find some more info here: http://www.inspired-lua.org/2012/02/how-to-create-a-screen-manager/ .

Adriweb: isn't Tower defence more simple ? I think most apps will be more simple than FormulaPro :P
Title: Re: Numstrat - Stumbling into Lua
Post by: Adriweb on September 23, 2012, 09:18:03 am
Well, you can "simply" take this code until line 1440 : https://github.com/adriweb/LuaTowerDefense/blob/master/LuaTowerDefense.lua
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on August 13, 2013, 03:26:13 am
Levak made an excellent Lua sudoku btw (quite advanced/complex Nspire Lua though) available here :
http://levak.free.fr/ftp/nspire/Sudoku/ (http://levak.free.fr/ftp/nspire/Sudoku/)

I could not find this anywhere on Omnimaga, then realised it probably was on TI Planet. It was.
The .tns file from levak.free.fr works.
But using the sourcecode (.lua) and compiling with Luna I cannot get the code to run at all with
Error: attempt to call global 'PushScreen' (a nil value)

As I do not understand French, posting in English on TI Planet would be quite weird...

I was looking at this topic when I saw the link for Sudoku above.
This is probably totally off topic. ;)
Title: Re: Numstrat - Stumbling into Lua
Post by: Levak on August 13, 2013, 07:20:04 am
But using the sourcecode (.lua) and compiling with Luna I cannot get the code to run at all with
Error: attempt to call global 'PushScreen' (a nil value)
Have you followed the correct order of source-concatenation described in http://levak.free.fr/ftp/nspire/Sudoku/MakeFile.sh ?

i.e:
Buttons.lua
Menu.lua
Request.lua
Constants.lua
ScreenManager.lua
Engine.lua
Help.lua
Sudoku.lua
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on August 14, 2013, 06:53:13 pm
Hmm I see, that would probably be it. I was missing out on all those other files.
Looking at the MakeFile, I can see that it requires MakeTNS.py, which I have downloaded from ticalc.org.
Sudoku.lua isn't in the MakeFile commands...? Oh I see it is, it's at the top as a variable.

This would be the Linux commands more or less:
Code: [Select]
maketns.py Sudokut.tns Buttons.lua Menu.lua Request.lua Constants.lua ScreenManager.lua Engine.lua Help.lua Sudoku.lua
But since I use Windows, the MakeTNS readme told me to go into the dist folder and execute there
Code: [Select]
maketns Sudokut.tns Buttons.lua Menu.lua Request.lua Constants.lua ScreenManager.lua Engine.lua Help.lua Sudoku.lua
However it gave me an output of a 10kb tns file where the actual working tns is 13kb. And it wouldn't run on the calculator.

This seems overly complicated. But at least I learnt something.

Title: Re: Numstrat - Stumbling into Lua
Post by: Levak on August 14, 2013, 07:29:52 pm
... well
MakeTNS was used way before Luna ever existed, even before 0S 3.0.2 used to exist and block custom XML files without encoding.
Thus, files had to be resaved oncalc or using TINCS resulting in bigger files.
If you cannot run this file using 3.1.0 this is only because of that.
Just concatenate the files yourself and compile it with Luna.
Title: Re: Numstrat - Stumbling into Lua
Post by: Jonius7 on August 14, 2013, 09:58:39 pm
Oh of course, things were different in OS 3.0.x.
Interesting after manually concatenating all the files into one, I got a file size of 9kb. But it works!
Um, I found a bug almost straight away. If you press the delete key, it crashes.
Title: Re: Numstrat - Stumbling into Lua
Post by: Adriweb on August 15, 2013, 05:50:02 am
if for 3.2, [lua]platform.registerErrorHandler[/lua] ftw :P

(/me runs or not, but meh, why not)

Anyway, as I'm sometimes saying, these scrits are indeed old-generation, with no techniques that have been "found"/written more or less recently, using deprecated code, not using the new things (obviously), etc. etc.
Title: Re: Numstrat - Stumbling into Lua
Post by: Jim Bauwens on August 15, 2013, 06:03:17 am
Well, you should always try to avoid that your script throws errors to the user level (so that you need to catch it with registerErrorHandler). Of course it's not bad to implement it in case that something goes wrong unexpectedly, but still a well designed script should never actually be able to arrive to that point.
Title: Re: Numstrat - Stumbling into Lua
Post by: Adriweb on August 15, 2013, 06:31:36 am
Well, you should always try to avoid that your script throws errors to the user level (so that you need to catch it with registerErrorHandler). Of course it's not bad to implement it in case that something goes wrong unexpectedly, but still a well designed script should never actually be able to arrive to that point.
Of course :)