Page 1 of 1

Script name and path

Posted: Sun Apr 06, 2014 6:22 pm
by Daniel_Cortez
Hey all!

I'm making an entities system for my game.
Right now I'm organizing files the next way: entity file is located at "entities/<entity name>.lua" and all related stuff is located in "entities/<entity name>/" folder.
To get the name of that folder, I need to know the path and the name of the entity script. So... Is there any way to get that information?

Re: Script name and path

Posted: Sun Apr 06, 2014 7:47 pm
by davisdude
I don't know much about your system, but here's my input:
When you create the entity, just use the entity name, then you can do something like this:

Code: Select all

local Entity = {}

function Entity.New( Name, Information )
    return { 
        Script = love.filesystem.load( 'entities/' .. Name .. '.lua' )(), 
        Related = 'entities/' .. Name .. '/', 
        Information = Information, 
    }
end
This is known as concatenation. You can also do it with " and ", but I like ' and ' more.

Re: Script name and path

Posted: Sun Apr 06, 2014 8:18 pm
by MGinshe
when you require() a file, lua passes the path as the a "..." operator. examle:

Code: Select all

---- main.lua:

require("lua/classes/player")

---- lua/classes/player.lua:

print(...)

-- this would output "lua/classes/player"

you'll still need to use string.gsub to split the path (lua/classes/) and the filename (player), which is what the next bit of code does

Code: Select all

-- lua/classes/player.lua

local className = (...):gsub("^(.*/+)", "") -- note the parenthesis around ...
print(className)

-- this would output "player"

Re: Script name and path

Posted: Tue Apr 08, 2014 7:43 am
by Daniel_Cortez
Yeah, I'm already using string concatenation. Just needed to get the path of that script with code inside that script, so the entity would be able to load its graphics, collision data, etc.
And looks like the "..." operator should solve that problem.