I'm getting a strange error that I can't for the life of me crack.
I'm coding a card game and I have two tables of different lengths. One links entries to functions, and the other holds the played cards. The first table is for attributes that certain cards in the deck have.
ATTRIBUTES = {
Reset = RuleBook.Do_Reset,
Go_Lower= RuleBook.Do_Go_Lower,
Mirror = RuleBook.Do_Mirror}
The way these functions are called is as follows:
ATTRIBUTES[cardPile[#cardPile].Attribute]()
I've printed out of contents of both the card object and the ATTRIBUTES table and both are completely in tact. Cards that have an attribute have a table entry under Attribute for a function, and those link up to the Do_... functions. Yet the above line of code doesn't appear to work. If anyone has ideas or suggestions they'd be appreciated.
Lua lets you basically use any kind of lua value as a key in a table. The problem with your code above is that your ATTRIBUTE
table uses strings as the key but cardPile[#cardPile].Attribute
is a function NOT a string.
When you perform the lookup here:
ATTRIBUTES[cardPile[#cardPile].Attribute]()
You're saying lookup the corresponding value in ATTRIBUTES
having the key cardPile[#cardPile].Attribute
which is a function. Your ATTRIBUTES
table as you have it defined only contain strings as keys -- it has no functions as keys so nil is returned.
Two possible fixes for this:
Assuming cardPile
's Attribute
field already refers to the function you want, you can just call it like this:
cardPile[#cardPile].Attribute()
The alternative is to change how you setup card_obj
's Attribute
field -- make it refer to a string instead of the the function:
function Card.Create(Suit, Number, Name)
local card_obj = {}
-- ...
if( card_obj.number == 1 ) then
card_obj.Attribute = "Reset"
elseif( card_obj.number == 6 ) then
card_obj.Attribute = "Go_Lower"
-- ... etc.
else
card_obj.Attribute = nil;
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.