Assign a 'require' to an index arbitrarily?

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
Sarcose
Prole
Posts: 48
Joined: Wed Jul 08, 2015 12:09 am

Assign a 'require' to an index arbitrarily?

Post by Sarcose »

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:

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},
		},
	}
}
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:

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',
}
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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Assign a 'require' to an index arbitrarily?

Post by zorg »

Nothing simpler to solve, just put a

Code: Select all

return block_normal
at the very end of your block_normal.lua file, that will return the table itself through require, not just whether the file was successfully required in or not. (filename and table name doesn't need to correlate necessarily, but here, it does)

It's weird that you say it didn't work when you tried it though, it should have returned the table instead of true.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Sarcose
Prole
Posts: 48
Joined: Wed Jul 08, 2015 12:09 am

Re: Assign a 'require' to an index arbitrarily?

Post by Sarcose »

zorg wrote: Mon Feb 22, 2021 4:30 am Nothing simpler to solve, just put a

Code: Select all

return block_normal
at the very end of your block_normal.lua file, that will return the table itself through require, not just whether the file was successfully required in or not. (filename and table name doesn't need to correlate necessarily, but here, it does)

It's weird that you say it didn't work when you tried it though, it should have returned the table instead of true.
This definitely was the solution. I can't believe it didn't work for me the first time. I guess the issue was I was trying to get fancy and require a requires.lua, and in the pipeline messed up the syntax somewhere. Thanks! I feel really silly lol
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 0 guests