My question is how would I take code from a file, e.g.
Code: Select all
if argument then
block
end
Would it be best to save the file as a lua object file and then load that as an object?
Code: Select all
if argument then
block
end
Something about like this should work: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.and load it into an object's :update(dt) or anywhere in the object for that matter?Code: Select all
if argument then block end
Would it be best to save the file as a lua object file and then load that as an object?
Code: Select all
love.filesystem.load("file.lua")()
Code: Select all
return function(self)
-- do stuff with self here
end
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')
So would this work?tentus wrote:Something about like this should work: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.and load it into an object's :update(dt) or anywhere in the object for that matter?Code: Select all
if argument then block end
Would it be best to save the file as a lua object file and then load that as an object?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.Code: Select all
love.filesystem.load("file.lua")()
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:mirage wrote:So would this work?Code: Select all
NPC_Name = love.filesystem.load("NPC_Name.lua")() NewNPC = NPC_Name:new()
Code: Select all
--NPC_Name.lua
local t = {}
function t:new()
--blah
end
return t
Users browsing this forum: No registered users and 2 guests