I am scripting actions of certain cards in my trading card game. Each card has there own Lua script that loads there scriptable behaviors for them (for special card effects that alter the rules of the game). Things like onTrash or onPlaced lie in the Lua script files.
The project structure is like so:
Root Folder
~ data
> cards
* behaviors
- 011.lua
- 012.lua (The files are named after the id of the cards)
- etc…
* cards.json (Contains an array of data for the cards)
~ main.lua (For now we’ll say the cards are loaded in main.lua)
Cards are loaded the cards.json file. A for loop goes through the array in the json file and applies an effect using a script that is associated with the card.
The script looks like this:
[lua]local M = {}
function M.main(card)
function card:onPlace(event)
end
function card:onTrash(event)
end
function card:onDraw(event)
end
function card:activate(event)
end
end
[/lua]
The game is supposed to require the script and apply the functions you see to the card object passed through.
The Problem
The problem is the script works in the simulator if I run it from sublime and only from any other file besides the card script. When I build the app for android, it doesn’t work either.
Here is the code:
[lua]for k,v in ipairs(cards) do
if v.type == “effect” then
local behavior = require(“data.cards.behaviors.” … string.format("%03d", v.id))
if behavior then
behavior.main(v)
end
end[/lua]
I was wondering if it had something to do with my code, the simulator, or just Corona SDK?
My initial thought is that “id” is an invalid (nil) string (line 5 in the code block above). Shouldn’t it be something like “v.id” because it’s part of the “ipairs()” iteration just like “v.type”?
OK, in terms of file-related things that work in the Simulator but not on a device, that is usually (99%) a case mismatch issue, for example you write “data” (lowercase “d”) in your code but the project folder is “Data” (capital “D”). The Simulator is very forgiving on case matches folders/files, but devices are absolutely strict on the matter.
Can you please confirm that there’s no case mismatches in your setup/code?
I never use capitals in any of my directory names. It doesn’t seem to be a case problem. But there’s also the problem of the script not being loaded when you start the project in sublime while the script is in focus. If i change the focus of the window on a different file like main.lua, it works just fine.
I have tried moving the files to the root directory and there are still not changes. I have tried changing the name of the files, manually required the files instead of dynamically. Nothing.
I think I found one main issue. In your “card.lua” module, I’m seeing two places where you call “io.open()”, but it looks like you’re trying to directly open a file via its string name. This won’t work… you need to define the actual path where that file exists, using the “system.pathForFile()” API.
You’ve done that elsewhere in your project, just not in “card.lua”, so basically your conditional check for if “script” or “scriptFile” exists fails, and nothing inside the block executes.
My initial thought is that “id” is an invalid (nil) string (line 5 in the code block above). Shouldn’t it be something like “v.id” because it’s part of the “ipairs()” iteration just like “v.type”?
OK, in terms of file-related things that work in the Simulator but not on a device, that is usually (99%) a case mismatch issue, for example you write “data” (lowercase “d”) in your code but the project folder is “Data” (capital “D”). The Simulator is very forgiving on case matches folders/files, but devices are absolutely strict on the matter.
Can you please confirm that there’s no case mismatches in your setup/code?
I never use capitals in any of my directory names. It doesn’t seem to be a case problem. But there’s also the problem of the script not being loaded when you start the project in sublime while the script is in focus. If i change the focus of the window on a different file like main.lua, it works just fine.
I have tried moving the files to the root directory and there are still not changes. I have tried changing the name of the files, manually required the files instead of dynamically. Nothing.