Is there anyway to iterate the folder ? I have lots of scripts in a folder, and I want to load all of them. Or…I can only write them manually to list them. Thanks.
Do you mean you want to add all of them as script components to a game object, or include all of them through require-statements? To give a good answer, I would like to know more about your specific use case, like what the scripts do roughly and how you expect it to work. I have personally never felt this need, regardless of programming language, so that’s why I ask.
euh, it’s a card game. and I have a go-proto for all the cards. for each card, I have a lua file to be the data sheet apart from the basic card go script. but I can’t load it in the init callback because the file will not be included. so I have to create a loader and require it before the init callback. the loader will list all the card data. for example:
local set={}
set[hash(“green_coin”)] = require (“main/data/green/coin”)
return set
I have to add all the card info into the set (about 200 files). i can create a bat to make this list out of engine. i just wonder if there is a in engine method.
Thank you.
Ok, so each card will have a unique type and for each card type there will be some data associated? Will it be data only or Lua code as well? I would probably store the data files as custom resources and load them via sys.load_resource.
Do you have to know and load all files or could you load them one by one when needed, and over time build that local set = {}
in your example? If you organise your files in such a way that the file names can be composed from the card type then this seems like a doable thing (eg “green_coin” would match a file named “/main/data/green_coin” and “red_dragon” would match “/main/data/red_dragon”).
If the files contain only data then consider storing it in JSON format and convert to a Lua table using json.decode. If the data is actual Lua code then load it using loadstring().
The above solution allows for a very dynamic way of dealing with your card data and it would make it easy to patch the game data without having to release a new version of the game. On the other hand does it also make it a bit harder to manage and work with the code and data in your game. If the card types are known I would probably manually build that list of cards in your example and keep the card data in Lua modules that are required at runtime.
PS Using a hash as a table key as in your example will only work in debug builds. Use hash_to_hex() to convert a hash to a string that can be used as a table key.
Thank you. btw, will json data faster then lua table? I think lua table is ok for me. because I only load the data once and restore it in a global, so that I can access it from any card and load it in the init callback.
Do you mean json.decode() vs loadstring()? In any case I do not think it will matter since this isn’t something you do every frame.
nope , I mean just data = require “abc.lua” vs data = loadstring(json.decode(“abc.json”))(). for the format of data, there are not many differences.
A call to require will be cached in package.loaded meaning that multiple calls to require() will only load and execute the code of the module once. Using loadstring() and/or json.decode() will be slower unless you manually cache the result.