Luacontroller et tableaux

French
Post Reply
skippythekangoo
New member
Posts: 6
Joined: Mon Apr 24, 2023 09:27

Luacontroller et tableaux

by skippythekangoo » Post

Salutations à toutes et à tous.

J'essais de faire un petit afficheur 4x4 pixels à base de luacontroller, dont un qui gère tout ceux qui font "affichage".

Voici le code qui devrait bien aller, mais non... :(

Code: Select all

mem.count = 1

-- Frame 1
local ligne_4_frame_1 = {"1", "1", "1", "1"}
local ligne_3_frame_1 = {"1", "1", "1", "1"}
local ligne_2_frame_1 = {"1", "1", "1", "1"}
local ligne_1_frame_1 = {"1", "1", "1", "1"}

-- Frame 2
local ligne_4_frame_2 = {"0", "0", "0", "0"}
local ligne_3_frame_2 = {"0", "0", "0", "0"}
local ligne_2_frame_2 = {"0", "0", "0", "0"}
local ligne_1_frame_2 = {"0", "0", "0", "0"}

-- Frame 3
local ligne_4_frame_3 = {"1", "0", "0", "0"}
local ligne_3_frame_3 = {"1", "0", "0", "0"}
local ligne_2_frame_3 = {"1", "0", "0", "0"}
local ligne_1_frame_3 = {"1", "0", "0", "0"}

-- Frame 4
local ligne_4_frame_4 = {"0", "1", "0", "0"}
local ligne_3_frame_4 = {"0", "1", "0", "0"}
local ligne_2_frame_4 = {"0", "1", "0", "0"}
local ligne_1_frame_4 = {"0", "1", "0", "0"}

-- Frame 5
local ligne_4_frame_5 = {"0", "0", "1", "0"}
local ligne_3_frame_5 = {"0", "0", "1", "0"}
local ligne_2_frame_5 = {"0", "0", "1", "0"}
local ligne_1_frame_5 = {"0", "0", "1", "0"}

-- Frame 6
local ligne_4_frame_6 = {"0", "0", "0", "1"}
local ligne_3_frame_6 = {"0", "0", "0", "1"}
local ligne_2_frame_6 = {"0", "0", "0", "1"}
local ligne_1_frame_6 = {"0", "0", "0", "1"}

-- Frame 7
local ligne_4_frame_7 = {"0", "0", "0", "0"}
local ligne_3_frame_7 = {"0", "0", "0", "0"}
local ligne_2_frame_7 = {"0", "0", "0", "0"}
local ligne_1_frame_7 = {"0", "0", "0", "0"}

if event.type == "digiline" and event.channel == "master" then

  local iid="run"

  if mem.count == nil then
    mem.count = 1
  end
  print("mem.count = "..mem.count)
  i = mem.count
  msg = (ligne_1_frame_[i])
  --print("msg = "..msg)
  --digiline_send("lua_1", ligne_1_frame_[mem.count])
  --digiline_send("lua_2", ligne_2_frame_[mem.count])
  --digiline_send("lua_3", ligne_3_frame_[mem.count])
  --digiline_send("lua_4", ligne_4_frame_[mem.count])
  mem.count = mem.count+1
  print("mem.count+1 = "..mem.count)
  iid = "stop"
  interrupt(1, iid)
end
Dès que je fais appel à "ligne_1_frame_" en chargeant la variable "msg", je reçoit une erreur :

Code: Select all

023-05-01 11:30:11: ACTION[Server]: Lua controller programming error: (load):54: attempt to index global 'ligne_1_frame_' (a nil value)
je ne comprends pas ce qui ne va pas dans mon code.

Merci à celles et ceux qu'y m'ont lu et à aux autres qui m'apporteront un début de réponse.

Cordialement...

User avatar
Blockhead
Moderator
Posts: 2170
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Luacontroller et tableaux

by Blockhead » Post

Vous ne pouvez envoyer qu'un seul message digiline par exécution du Luacontroller (par interrupt).

Utiliser des tableaux dans des tableaux

(via Google Translate)

Code: Select all

if event.type == "program" then
    local frames = {
        {
            {"1", "1", "1", "1"},
            {"1", "1", "1", "1"},
            {"1", "1", "1", "1"},
            {"1", "1", "1", "1"},
        },
        {
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
        },
        {
            {"1", "0", "0", "0"},
            {"1", "0", "0", "0"},
            {"1", "0", "0", "0"},
            {"1", "0", "0", "0"},
        },
        {
            {"0", "1", "0", "0"},
            {"0", "1", "0", "0"},
            {"0", "1", "0", "0"},
            {"0", "1", "0", "0"},
        },
        {
            {"0", "0", "1", "0"},
            {"0", "0", "1", "0"},
            {"0", "0", "1", "0"},
            {"0", "0", "1", "0"},
        },
        {
            {"0", "0", "0", "1"},
            {"0", "0", "0", "1"},
            {"0", "0", "0", "1"},
            {"0", "0", "0", "1"},
        },
        {
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
            {"0", "0", "0", "0"},
        },
    }
    
    mem.frames = frames
    mem.max = #frames*4

elseif event.type == "digiline" and event.channel == "master" then
    local frames = mem.frames

    mem.count = (mem.count or 0) + 1
    if mem.count > mem.max then
        mem.count = 1
    end
    local frame_index = mem.count
    local lign_index = math.floor(((frame_index-1)/4)+1)

    digiline_send("lua_" .. tostring(lign_index),
        frames[frame_index][lign_index])

    interrupt(1, "run")
end

--for i=1,#frames*4 do print(i, math.floor((i-1)/4)+1) end
-- 1 1
-- 2 1
-- 3 1
-- 4 1
-- 5 2 etc
La version tricherie: (référence)

Code: Select all

--digiline_send("lua_" .. tostring(i), _G["ligne_1_frame_"..tostring(mem.count)])
Last edited by Blockhead on Wed May 24, 2023 03:44, edited 1 time in total.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

Astrobe
Member
Posts: 635
Joined: Sun Apr 01, 2018 10:46

Re: Luacontroller et tableaux

by Astrobe » Post

Thanks, Blockhead, for your quite complete answer -- perhaps a bit too much, don't show to newbies dirty Lua tricks... ;-)
My game? It's Minefall.

skippythekangoo
New member
Posts: 6
Joined: Mon Apr 24, 2023 09:27

Re: Luacontroller et tableaux

by skippythekangoo » Post

Salutations.

Désolé du lag... :p

J'ai beau retourner le truc dans tout les sens, j'ai une erreur en lignes 59 :

(load):59: attempt to index a nil value

Si j'ai bien saisi, la variable "frames" est nulle, et donc "digiline_send" ne peux envoyer de message nul...

User avatar
Blockhead
Moderator
Posts: 2170
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Luacontroller et tableaux

by Blockhead » Post

skippythekangoo wrote:
Tue May 23, 2023 16:07
Salutations.

Désolé du lag... :p

J'ai beau retourner le truc dans tout les sens, j'ai une erreur en lignes 59 :

(load):59: attempt to index a nil value

Si j'ai bien saisi, la variable "frames" est nulle, et donc "digiline_send" ne peux envoyer de message nul...
Voir ma dernière modification. J'avais besoin d'une déclaration de variable "local".
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

Post Reply