Load code from file into object

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
User avatar
mirage
Prole
Posts: 8
Joined: Thu Sep 01, 2011 7:59 pm

Load code from file into object

Post by mirage »

I am trying to write a program that allows the user to create custom NPCs with custom AI. In the program they will be able to create custom code, save it as a preset it they like (to a file in the save directory) then apply it to the NPC.

My question is how would I take code from a file, e.g.

Code: Select all

if argument then
block
end
and load it into an object's :update(dt) or anywhere in the object for that matter?

Would it be best to save the file as a lua object file and then load that as an object?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Load code from file into object

Post by tentus »

mirage wrote:I am trying to write a program that allows the user to create custom NPCs with custom AI. In the program they will be able to create custom code, save it as a preset it they like (to a file in the save directory) then apply it to the NPC.

My question is how would I take code from a file, e.g.

Code: Select all

if argument then
block
end
and load it into an object's :update(dt) or anywhere in the object for that matter?

Would it be best to save the file as a lua object file and then load that as an object?
Something about like this should work:

Code: Select all

love.filesystem.load("file.lua")()
Given that the contents are valid Lua, they will be executed like normal. I do something similar for levels in my game Kurosuke, so that users can easily create, save, and later load levels.
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Load code from file into object

Post by kikito »

First of all, I think you are using the word "object" too loosely. I don't understand what you mean by "object" in your post... in general. It seems as if you are using it with several meanings.

Anyway.

The simplest way to do it would be creating a .lua file that returns a function. Like this:

Code: Select all

return function(self)
  -- do stuff with self here
end
Imagine that that file is called "player.lua". In order to set that function as the update key of a lua table called "player", you can do it like this in your code:

Code: Select all

local player = {} -- this could be something else, I've set player to an empty player as an example
player.update = require('player')
That's the most basic implementation. It doesn't, for example, check for syntax errors in the source file, so your game could crash if the users put errors on their files. The same would happen if the file didn't exist, or wasn't readable because of the user's permissions.

A more robust, but more complex solution would employ love.filesystem.load, and pcall. But that would start being complex; I can't code it right now.

Finally, if you wanted to specify more than one function on that file (for example update and draw) then the simplest way of doing that involves setmetatable and setfenv, which are quite advanced. Or returning a table with functions inside, but that would be more inconvenient to your users.
When I write def I mean function.
User avatar
mirage
Prole
Posts: 8
Joined: Thu Sep 01, 2011 7:59 pm

Re: Load code from file into object

Post by mirage »

tentus wrote:
mirage wrote:I am trying to write a program that allows the user to create custom NPCs with custom AI. In the program they will be able to create custom code, save it as a preset it they like (to a file in the save directory) then apply it to the NPC.

My question is how would I take code from a file, e.g.

Code: Select all

if argument then
block
end
and load it into an object's :update(dt) or anywhere in the object for that matter?

Would it be best to save the file as a lua object file and then load that as an object?
Something about like this should work:

Code: Select all

love.filesystem.load("file.lua")()
Given that the contents are valid Lua, they will be executed like normal. I do something similar for levels in my game Kurosuke, so that users can easily create, save, and later load levels.
So would this work?

Code: Select all

NPC_Name = love.filesystem.load("NPC_Name.lua")()
NewNPC = NPC_Name:new()
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Load code from file into object

Post by Robin »

mirage wrote:So would this work?

Code: Select all

NPC_Name = love.filesystem.load("NPC_Name.lua")()
NewNPC = NPC_Name:new()
Depends on what is in "NPC_Name.lua", but if you have "NPC_Name.lua" return a table which contains something like this:

Code: Select all

--NPC_Name.lua
local t = {}
function t:new()
 --blah
end
return t
it should work.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests