0

I am making a card game and im now revamping how individual card information is stored.

a single card would have the following values:

x, y, suit, number, state

this is to be stored in a deck array where card info can be accessed with,

deck[i].suit

yes, yes i can find the suit and number based on its index but no

now i could just individually type up the info for each card but im lazy so i made a init_deck function, that gets called once during...initialization.

deck={}
// lua starts loops with one
function init_deck(deck)
  for s=1,4 do  //  iterate through the 4 suits
    for n=1,13 do  //  iterate from ace to king
      add(deck,{x=0,y=0,suit=s,number=n,state=1})
    end
  end
end

function init()
  init_deck(deck)
end

function draw()
  print(deck[2].number) // should print 2  // output is 13
  print(deck[2].suit) // should print 1  // output is 4
  print(deck[2].state) // should print 1  // output is 1
end

ive also used table.insert where add is being called to the same effect(with the same parameters, also tried with an index parameter, didint work), for some reason the variables that are affected by the for loop are assigned the highest value in the loop.

I am quite confused, Im assuming its a pico-8 thing because the lua documentation includes these things. any support would be appreciated, i now realize i could have manually made every card in the time it took to type this but...

6
  • call init() before any print(), maybe some old values are being printed here. Commented Sep 23 at 16:42
  • 2
    Your call to add is missing a closing parenth. Is that in the original code, or a copy-paste mistake? It would help if you formatted your code so that it was readable. Commented Sep 23 at 21:01
  • @adabsurdum fixed the indentation, its not a copy paste just a recreation of the relevant code. Commented Sep 24 at 12:24
  • According to the docs you should be using _init and _draw, not init and draw. Is this another "recreation" error? Best to provide accurate code examples. IAC, the code works in regular Lua (with table.insert replacing add; I don't think that PICO-8 includes the Lua Standard Library, though). This should work in PICO-8 (modulo _init vs. init); the problem is likely in code not shown. Possible that other code is modifying the global deck. Commented Sep 24 at 15:13
  • 1
    Also: // tokens do not introduce comments in Lua, nor in PICO-8, I believe. Please provide accurate code samples that reproduce the problem. minimal reproducible example Commented Sep 24 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.