Assign a 'require' to an index arbitrarily?
Posted: Sun Feb 21, 2021 10:23 pm
Hi. I definitely tried to find a good answer to this but forgive me if this has been answered elsewhere and I'll gladly hop onto that thread if so. So let's say I want a *simple* table like this in an external file, where the following is the entire file:
And then if I have a bunch of these individually defined entity profiles I'd like to be able to assign them and reference them generically. For example:
But I've obviously learned the hard way that this just gives me a table with a bunch of bool trues. Theoretically for this test code I would want map.actorblocks[1], [2], etc to refer to the tables directly in accordance with the above designation. So if I refer to map.actorblocks[1] it will bring up block_normal.
Without making the individual files more complex, how could I store simple tables (e.g. without a load() function for each one) externally and then pull them into the program? Eventually this will become a modding API which is why I'm trying to figure out at least part of this simple implementation for now. I did try return = {codestuff} but it did the same thing so I'm guessing require is the wrong way to go about this? If dofile() is the right way to go how in the heck do I find the syntax for it, because the lua API isn't clarifying how to use it?
I'm mortified to attach a .love, because it is a huge mess of mostly-working code with entities designed with a variety of paradigms as I kept changing my mind, and this particular attempt represents a massive refactor I'm undertaking. Hopefully this is an abstract enough question, though.
Code: Select all
--block_normal.lua
block_normal = {
name = 'block_normal',
w = 36,
h = 36,
collision = 'normal',
color = 'cyan',
states = {
idle = {
outline = 'black',
linewidth = 1,
color = 'cyan', --arbitrary redundancy, btw, to test my backend system
{'LG_RECT','A',0},
},
}
}
Code: Select all
--lolol spaghetti file structure
map.actorblocks = {
require 'engine.actorprop.blocks.block_normal',
require 'engine.actorprop.blocks.block_cling',
require 'engine.actorprop.blocks.block_greenheal',
require 'engine.actorprop.blocks.block_lava',
require 'engine.actorprop.blocks.block_bouncy',
}
Without making the individual files more complex, how could I store simple tables (e.g. without a load() function for each one) externally and then pull them into the program? Eventually this will become a modding API which is why I'm trying to figure out at least part of this simple implementation for now. I did try return = {codestuff} but it did the same thing so I'm guessing require is the wrong way to go about this? If dofile() is the right way to go how in the heck do I find the syntax for it, because the lua API isn't clarifying how to use it?
I'm mortified to attach a .love, because it is a huge mess of mostly-working code with entities designed with a variety of paradigms as I kept changing my mind, and this particular attempt represents a massive refactor I'm undertaking. Hopefully this is an abstract enough question, though.