I'm trying to find specific cards in a deck that correspond to some die. Basically, the die are rolled, then the cards are removed from the deck into another pile based on the die rolled. What's the best way to do that?
I'm trying to find specific cards in a deck that correspond to some die. Basically, the die are rolled, then the cards are removed from the deck into another pile based on the die rolled. What's the best way to do that?
You just loop through cards in the deck and do your check. Use takeObject to get them out to some position.
Code:for k,card in ipairs(deck.getObjects()) do -- e.g. take one with 'Die result X' name, X is the die number if card.nickname == ('Die result ' .. die.getValue()) then local targetPos, targetRot = anotherPile.getPosition(), anotherPile.getRotation() targetPos.y = targetPos.y + 2 deck.takeObject({index = card.index, position = targetPos, rotation = targetRot}) end end
Last edited by Tragic; 03-20-2018 at 02:08 AM.
Nickname is whatever you put in the "Name" field when you take the card out of the deck and right click it. "Description" is the box just under it.
So far, I have this but am getting some errors. "Object reference not set to an instance of an object."
The function is supposed to get the rotation value of 6 die and find a card in a deck that matches that die and put it in a new pile, so the new pile should have 6 cards, but it seems to be random as it is now.
function forecast() --creates the forecast deck
fcpos = {31.39, 1.03, 8.79}
local rolleddie = {}
local fcfound = nil
local fcdeck = nil
local objectsInZone = fcczone.getObjects()
for k, v in ipairs(dice_GUID) do
table.insert(rolleddie,getObjectFromGUID(v).getRot ationValue())
end
for i, object in ipairs(objectsInZone) do --find deck in zone
if object.tag == "Deck" then
fcfound = object.getObjects()
--local fcdeck_GUID = object.getGUID()
fcdeck = getObjectFromGUID(object.getGUID())
end
end
for j, w in ipairs(rolleddie) do --for each die
for h, card in ipairs(fcfound) do --for each card in the found deck
if card.nickname == w then
fcdeck.takeObject({
index = card.index,
position = fcpos
})
end
end
end
end
fcdeck = getObjectFromGUID(object.getGUID()) Can just be fcdeck=object I do believe.
As for the error you got, you got it because you attempted to reference an object which does not exist. Try to print before each use of an object you have, ie. print(fcdeck) before doing fcdeck.takeObject.
Additionally, when you take a card by its index, all other higher indexes shift down automatically. This means if you want index 2 and index 3, if you take index 2, now the card that used to be index 3 is shifted down to index 2. At least if memory serves me it does. I usually track the card GUIDs instead. I think I put an example of pulling a card by one of its properties here [url]http://steamcommunity.com/sharedfiles/filedetails/?id=879494019[/url]
If you DO take cards by GUID, be aware that a custom deck assigns all of its cards the same GUID. I made this to split out all the cards at once, forcing them to be assigned new GUIDs, then you can save the table/deck now that all the cards have unique guids. [url]http://steamcommunity.com/sharedfiles/filedetails/?id=1180142950[/url]
If you are still having issues, let me know and I'll work up a demo script
Much appreciated!