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?
Script name and path
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 11
- Joined: Sun Apr 06, 2014 4:43 pm
- Location: Novokuznetsk, Russia
Script name and path
↑↑↓↓↑↑↑↑
Re: Script name and path
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:
This is known as concatenation. You can also do it with " and ", but I like ' and ' more.
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
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
Re: Script name and path
when you require() a file, lua passes the path as the a "..." operator. examle:
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
---- 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"
-
- Prole
- Posts: 11
- Joined: Sun Apr 06, 2014 4:43 pm
- Location: Novokuznetsk, Russia
Re: Script name and path
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.
And looks like the "..." operator should solve that problem.
↑↑↓↓↑↑↑↑
Who is online
Users browsing this forum: Bing [Bot] and 5 guests