Script name and path

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
Daniel_Cortez
Prole
Posts: 11
Joined: Sun Apr 06, 2014 4:43 pm
Location: Novokuznetsk, Russia

Script name and path

Post 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?
↑↑↓↓↑↑↑↑
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Script name and path

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
MGinshe
Prole
Posts: 31
Joined: Sun Apr 17, 2011 3:50 am
Location: New Zealand

Re: Script name and path

Post 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"
Daniel_Cortez
Prole
Posts: 11
Joined: Sun Apr 06, 2014 4:43 pm
Location: Novokuznetsk, Russia

Re: Script name and path

Post 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.
↑↑↓↓↑↑↑↑
Post Reply

Who is online

Users browsing this forum: Bing [Bot], CleoCommunist, Google [Bot] and 5 guests