Page 1 of 1

How to load a lua file as a function (OLD: Adding variables to a table return nil)

Posted: Thu Aug 10, 2023 5:48 pm
by notcl4y
For some reason, a value doesn't get added into a table, and when I try to output or call it, it just returns nil or an error.
I tried using the unpack() function, load() but neither of them didn't work.

Code: Select all

function create_object(name, options, events)
	local obj = {
		type = name,

		x = 0,
		y = 0,

		width = 0,
		height = 0,

		-- ...
	}

	for key, value in pairs(options) do
		if obj[key] then
			obj[key] = value
		end
	end

	for key, value in pairs(events) do
		-- for some reason it doesn't add new variables into a table
		obj[key] = value
	end

	_G[name] = obj
end
Also I'm using a loader I wrote so it could be easier to create objects.

Code: Select all

function(folder)
	if not love.filesystem.getInfo("objects/" .. folder .. "/obj_info.lua") then
		return
	end

	local obj_info = dofile("objects/" .. folder .. "/obj_info.lua")

	local files = lf.getDirectoryItems("objects/" .. folder .. "/events")
	local events = {}

	for i=1, #files do
		local file = files[i]

		-- https://stackoverflow.com/questions/18884396/extracting-filename-only-with-pattern-matching
		-- "step.lua" > "step"
		local filename = file:match("(.+)%..+")

		local filehandler = io.open("objects/" .. folder .. "/events/" .. file)
		local data = filehandler:read("*all")
		filehandler:close()

		-- print(data)

		events[filename] = (function(self)
			print("works")
			load(data)()
		end)
	end

	create_object(obj_info.name, obj_info.properties, events)
end
Right now the object has only one event called step(), and when I try to add it into an object from the files it doesn't get added.
Something like this:

Code: Select all

object
	events
		step.lua
	obj_info.lua
And then from the files the table should have the methods the "events" folder has

Code: Select all

{
	-- some variables and methods
	-- ...
	
	step = function(self)
		load(data)()
	end
}
But for some reason the function doesn't get added.
Sorry for bad English.

Re: Adding variables to a table return nil

Posted: Fri Aug 11, 2023 11:23 am
by notcl4y
Ok, I fixed it but now my code doesn't detect the "self" keyword. Although I called the function via a colon sign. That's because I used a load function, I also tried using loadstring and dostring (ok, looks like this function doesn't exist meanwhile dofile does), but none of them worked. The code for the "step.lua" file looks like this:

Code: Select all

if love.keyboard.isDown("left") then
	self.x = self.x - 3
end

if love.keyboard.isDown("right") then
	self.x = self.x + 3
end
And to use that as a function, the code gets it as a string, calls it via load and puts it into a function. Is there an alternative way to do so?

Re: How to load a lua file as a function (OLD: Adding variables to a table return nil)

Posted: Sat Aug 12, 2023 12:49 pm
by dusoft
You mean modules?
https://www.tutorialspoint.com/lua/lua_modules.htm

Also please don't change topics for the same thread, but rather start a new forum post. Also: your questions belong under Support forum.

Re: How to load a lua file as a function (OLD: Adding variables to a table return nil)

Posted: Tue Aug 15, 2023 6:14 am
by notcl4y
dusoft wrote: Sat Aug 12, 2023 12:49 pm Also: your questions belong under Support forum.
Either do others I think?