[SOLVED] Problem with functions in an array

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Le Codex
Prole
Posts: 31
Joined: Thu Feb 09, 2017 10:56 am
Location: France
Contact:

[SOLVED] Problem with functions in an array

Post by Le Codex »

Hi,

I've started a new project, which is a kind of TCG. I'm currently using an array to store all the informations of the cards (name, health, effects...). For the effect part, I use functions to represent those effects (deal() to deal damage, play() to play a card...) The problem is that those functions are to be executed once the object is created, because they use variables that are created only with the object. But when I start the programm, it seems like it executes them even though I don't call them, since they're in the array. :shock:

main.lua :

Code: Select all

Object = require("Librairy/classic")
require("Librairy/card")

hand = {}
board = {}

--[[
All cards are encoded like this :
{Name, Health, Effect trigger, Effect}
Effect triggers :
  "s" : Spawn
  "d" : Death
  "k" : Kill
  "p" : Permanent
  "h" : Harm (each time it hits)

Tokens are cards that are spawned by characters. They're uncollectible.
]]--

function love.load()
  Tokens = {
    {"Teleporter", 1, "s"}, --The teleporter will be a spawning plateform
    {"Doll", 1, "d", effect = function() deal(board[self.crea], -1) end}, --Heal his creator once it dies
    {"Mech", 1, "d", effect = function() swap(self, board[self.crea]) end} --Swap with his creator once it dies
  }
  Deck = {
    {"Jetpack", 3, "s"}, --Spawn wherever he wants
    {"Engineer", 4, "d", effect = function() play(Tokens[1],  self.id) end}, --Create a Teleporter at death. Self.id is the position of the card object in the 'board' array (will be added later)
    {"Magician", 6, "k", effect = function() deal(hand[love.math.random(1, #hand)], 1) end} --Deal one damage to a random card in your hand once it kills
  }
  draw(5) --Draw your first five cards
end

function love.update(dt)
  --Nothing yet
end

function love.draw()
  love.graphics.push()
  --I'll add later camera movement
  for i,card in ipairs(hand) do
    --Draw the cards that are in your hand
    if card.cond == "s" then love.graphics.setColor(255, 255, 255, 255) end
    if card.cond == "d" then love.graphics.setColor(255, 0, 0, 255) end
    if card.cond == "k" then love.graphics.setColor(0, 255, 0, 255) end
    if card.cond == "p" then love.graphics.setColor(0, 0, 255, 255) end
    if card.cond == "h" then love.graphics.setColor(255, 255, 0, 255) end
    love.graphics.rectangle("fill", 71*i, 500, 70, 100)
    love.graphics.setColor(0, 0, 0, 255)
    love.graphics.print(card.health, 71*i + 2, 502)
    love.graphics.print(card.name, 71*i + 5, 522)
  end
  love.graphics.pop()
end

--Draw cards
function draw(amount)
  for i=1,amount do
    draw = love.math.random(1, #Deck)
    table.insert(hand, Card(Deck[draw]))
  end
end

--Play a card on the board
function play(location, id, byWho)
  if location == hand then table.remove(hand, i) end
  if location == Tokens then table.insert(board, Card(location[id], byWho)) else table.insert(board, location[id]) end
end

--Deal damage to a card on the board
function deal(target, amount)
  if target == "r" then target = board[love.math.random(1, #board)] end
  target.health = target.health - amount
end

--Swap two cards' position
function swap(target1, target2)
  local x = target1.x
  local y = target1.y
  target1.x = target2.x
  target1.y = target2.y
  target2.x = x
  target2.y = y
end
card.lua :

Code: Select all

Card =  Object:extend()

function Card:new(card, creator)
  self.name = card[1]
  self.health = card[2]
  self.cond = card[3]
  self.effect = card.effect
  if creator then self.crea = creator end
end
I've no idea how to fix this problem, so I think a second look on it will help me ^^
Last edited by Le Codex on Mon Jul 03, 2017 2:51 pm, edited 1 time in total.

Code: Select all

if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Problem with functions in an array

Post by raidho36 »

Throw an error from inside one of those functions and inspect stack trace to see where it comes from.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Problem with functions in an array

Post by Sir_Silver »

Hey, I also happen to be making a card game! =)

I've looked over at your code, haven't yet tried to run it, but I don't see anywhere where you call your card's effect function, or even call card:new(), I think wherever this problem is happening isn't in the block of code that you've given us to look at. The easiest way for me to find this problem would be to have a look at your .love file and load it right up into a code editor.
User avatar
Le Codex
Prole
Posts: 31
Joined: Thu Feb 09, 2017 10:56 am
Location: France
Contact:

Re: Problem with functions in an array

Post by Le Codex »

I launched it again and it worked well because of a stupid thing.

Before uploading the file, I modified it so the card's info array changed from:

Code: Select all

{"Name", 5, "s", deal("r", 1)}
to:

Code: Select all

{"Name", 5, "s", effect = function() deal("r", 1) end}
So I changed and fixed the bug, but I didn't notice it because I changed only a few of the info arrays and not all of them. The traceback was leading me back the the deal() function, which is called multiple times. And it just so happened that I didn't changed one of the two lines with deal() in it. So it was called normally, with wrong values as arguments.
I souldn't have asked for that :(

I actually call Card:new().' classic.lua' manage for me the classes. Every time I call Card(), I actually call Card:new().

Sorry for wasting your time '-_-

Code: Select all

if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest