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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
notcl4y
Citizen
Posts: 85
Joined: Fri Nov 25, 2022 12:23 pm

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

Post 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.
Last edited by notcl4y on Sat Aug 12, 2023 11:12 am, edited 1 time in total.

Code: Select all

loves_lua = "not so",
wants_to = true
User avatar
notcl4y
Citizen
Posts: 85
Joined: Fri Nov 25, 2022 12:23 pm

Re: Adding variables to a table return nil

Post 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?

Code: Select all

loves_lua = "not so",
wants_to = true
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
User avatar
notcl4y
Citizen
Posts: 85
Joined: Fri Nov 25, 2022 12:23 pm

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

Post by notcl4y »

dusoft wrote: Sat Aug 12, 2023 12:49 pm Also: your questions belong under Support forum.
Either do others I think?

Code: Select all

loves_lua = "not so",
wants_to = true
Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests