Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Metatables and Metamethods? [closed]

Asked by 9 years ago

Hi everyone, someone on the Scripting Helpers sub-forum on ROBLOX helped me with a card class script for my card game and this is what I have from modifying it so far:

cardTypes = {
    ["0"] = {0,0},
    ["1"] = {1,0},
    ["2"] = {2,0},
    ["3"] = {3,0},
    ["4"] = {0,1},
    ["5"] = {1,1},
    ["6"] = {2,1},
    ["7"] = {3,1},
    ["8"] = {0,2},
    ["9"] = {1,2},
    ["DrawTwo"] = {2,2},
    ["Reverse"] = {3,2},
    ["Skip"] = {4,0},
    ["Wild"] = {0,0},
    ["DrawFourWild"] = {1,0}
}

cardColours = {
    ["Red"] = "rbxassetid://184598259",
    ["Blue"] = "rbxassetid://184598232",
    ["Green"] = "rbxassetid://184598240",
    ["Yellow"] = "rbxassetid://184598224",
    ["Wild"] = "rbxassetid://184598210"
}

Card = {};

function Card:new()
    local nCard = {colour = "rbxassetid://184598259", offsetX = 0, offsetY = 0, player = nil}
    self.__index = self
    return setmetatable(nCard, self);
end;

function Card:SetColour(Colour)
    Colour = Colour or "Red";
    self.colour = cardColours[Colour]
end

function Card:SetType(Type)
    Type = tostring(Type) or "1"
    self.offsetX = (cardTypes[Type][1]*174)
    self.offsetY = (cardTypes[Type][2]*236)
end

function Card:SetPlayer(Plr)
    Plr = Plr or nil
    self.player = Plr
end

I would like to know how I would insert the data from the Card:new() function into the Card table so I can use it for setting values when creating a hand for the player in a later part of the script. I was thinking of doing the following, but I'm not sure whether or not to put in self or nCard.

function Card:new()
    local nCard = {colour = "rbxassetid://184598259", offsetX = 0, offsetY = 0, player = nil}
    self.__index = self
    table.insert(Card,self)
    return setmetatable(nCard, self);
end;

Any help is appreciated, thanks!

Locked by Spongocardo, BlackJPI, and adark

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

You actually don't want to put nCard inside Card (by the way, self inside your new function is actually a reference to Card)

When you call Card:new(), you are given the playing card's table as a return value, which you can then put into a table for the Player's Hand. You don't want to mess with the Card table you've created.

1
Oh right, thanks. Additional question: How would I go about making it so that when a new card is inserted into the player's hand table, an ImageButton is copied to the player's hand frame with data from that new card? Spongocardo 1991 — 9y
0
You could try using a proxy table with metamethod hackery. http://www.lua.org/pil/13.4.4.html Otherwise, try copying the image in in the code that inserts the PlayerCard table into the PlayerHand table. If you're got Filtering enabled, use a RemoteEvent. adark 5487 — 9y
1
Alright, I'm going to look into it myself again. Thank you. Spongocardo 1991 — 9y
Ad