[Need Help] Loading Dynamic Content (aka mods)

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
armisius
Prole
Posts: 10
Joined: Fri Aug 21, 2015 5:57 pm

[Need Help] Loading Dynamic Content (aka mods)

Post by armisius »

I'm working on a game and, as I don't like the hardcoded way to add items or objects, I'm dealing with Dynamic content saved to a specific folder. I've done the code that checks for content and adds the filename to a variable so if you add a custom monter in "assets/monsters" theorically it will add it to the game (at least the path to the monster's data file), BUT the thing I don't know, is how to write these data files, the formats options are JSON,LUA,INI,TXT, I've tried with JSON but all the libs for windows are binaries and I want my game to be somewhat standalone (the only dependence must be löve2d :3) I don't know if I'm explaining things right.
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by MicroMacro »

Not sure if I understood things right. I've been building my most recent game to be somewhat modular using metatables, but you don't necessarily need that. If by "modding" do you mean like being able to add stuff to your game? Because then I suggest looping through wherever you're saving user mods, not source code ( i.e. appdata/mygame/mods or something like that ) and load it that way.

Unless I didn't understand what you meant.
https://github.com/ebernerd- where you can find all my work.
armisius
Prole
Posts: 10
Joined: Fri Aug 21, 2015 5:57 pm

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by armisius »

MicroMacro wrote:Not sure if I understood things right. I've been building my most recent game to be somewhat modular using metatables, but you don't necessarily need that. If by "modding" do you mean like being able to add stuff to your game? Because then I suggest looping through wherever you're saving user mods, not source code ( i.e. appdata/mygame/mods or something like that ) and load it that way.

Unless I didn't understand what you meant.
By "modding" I mean add stuff like monsters or items or tiles, or simply changing the player's sprite but witout touching the code. I've managed to get something like. Lets suposse my game has only 1 item: (there is the item image and the item specifications like name, id and other stuff)
Image
And if I want to add more items, I just make more json files and add more art (those arts arent mine)
Image
and the next time you open the game it will load the new item "gun" to the game.
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by MicroMacro »

Alright. The way I do it in my game is that I have different folders for each object type: worldTypes, blocks, and items. I loop through each one and find the block, read it's table, and throw it into a metatable so that any other values not added get added and then the table is put into the global environment.

So with yours ( basing on how I do it ) you'd want to loop through the .json files ( and the image files if you need to, however I suggest adding the texture link IN the json file ) and then grab the data from that and then standardize it with a prototype.

Make sense?
https://github.com/ebernerd- where you can find all my work.
armisius
Prole
Posts: 10
Joined: Fri Aug 21, 2015 5:57 pm

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by armisius »

MicroMacro wrote:Alright. The way I do it in my game is that I have different folders for each object type: worldTypes, blocks, and items. I loop through each one and find the block, read it's table, and throw it into a metatable so that any other values not added get added and then the table is put into the global environment.

So with yours ( basing on how I do it ) you'd want to loop through the .json files ( and the image files if you need to, however I suggest adding the texture link IN the json file ) and then grab the data from that and then standardize it with a prototype.

Make sense?
Sure, I've managed to get somewhat loading script, but it throws an Image
this is the code I've made: (the first json loaded is disc.json)
disc.json

Code: Select all

{
	"index": 0,
	"name": "disc",
	"sprite": "disc.png",
	"height": 32,
	"width": 32
}
assets

Code: Select all

assets = {
  [0] = {"music",         "assets/music",        ["content"] = {}},
  [1] = {"items",         "assets/items",        ["content"] = {}},
  [2] = {"backgrounds",   "assets/backgrounds",  ["content"] = {}},
  [3] = {"monsters",      "assets/monsters",     ["content"] = {}},
  [4] = {"player",        "assets/player",       ["content"] = {}},
  [5] = {"effects",       "assets/effects",      ["content"] = {}},
  [6] = {"liquids",       "assets/liquids",      ["content"] = {}},
  [7] = {"placeables",    "assets/placeables",   ["content"] = {}},
  [8] = {"tiles",         "assets/tiles"},       ["content"] = {}
}
loader script

Code: Select all

function load_assets()
  for x = 0, 8 do --There are 9 type of GameObjects (assets)
    local files = love.filesystem.getDirectoryItems(assets[x][2]) --Get the files under specified assets
    for k,v in pairs(files) do --Loop through all the files in the assets
      if string.match(v,".json") then --If the file has a ".json" in it...
        local __file = assets[x][2].."/"..v --Set __file to the path of the file
        if love.filesystem.isFile(__file) then --If is a file...
          utils.msgbox(__file.." is a file.","System") --Display a messagebox
        end
        local fileread = io.open(__file,"r") --Open __file for reading
        local content = fileread:read("*all") --Read the whole file
        local data = JSON:decode(content) --Decode the json and make it a lua table
        assets[x]["content"][data.index]["name"] = data.name --Save the object's name in the asset's index
        assets[x]["content"][data.index]["sprite"] = love.graphics.newImage(assets[x][2].."/"..data.sprite) --Save and loads the object's image
      end
    end
  end
end
EDIT: mispelled the spritedir, changed from __file to assets[x][2].."/"..data.sprite
EDIT2: line 61: local content = fileread:read("*all") --Read the whole file
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by MicroMacro »

I dont suggest using io.open when you can just use love.filesystem.read( ). Even better, make the table setup like "return { }" then all you need to do is "local t = pcall( love.filesystem.load( path )( ) )"
https://github.com/ebernerd- where you can find all my work.
armisius
Prole
Posts: 10
Joined: Fri Aug 21, 2015 5:57 pm

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by armisius »

MicroMacro wrote:I dont suggest using io.open when you can just use love.filesystem.read( ). Even better, make the table setup like "return { }" then all you need to do is "local t = pcall( love.filesystem.load( path )( ) )"
hmmm.... I didn't get the "make the table setup like "return { }"" part and by "t" you meand the contents of the JSON? also, should I make some metaobjects like a metaobject for "monsters" that have default values so you don't need to type them in the json?
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by MicroMacro »

So, here's an example:

Code: Select all

--A lua file ( I know you're using json ) that has data
return {
  itema = "thing",
  itemb = "thing",
}

Code: Select all

--the loading code
for k, v in pairs( love.filesystem.getDirectoryItems( dir ) do
  --get the file
  local t = pcall( love.filesystem.load( fileLocation )( ) )
  --t is now that table from the file. Do something with it.
end
https://github.com/ebernerd- where you can find all my work.
armisius
Prole
Posts: 10
Joined: Fri Aug 21, 2015 5:57 pm

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by armisius »

MicroMacro wrote:So, here's an example:

Code: Select all

--A lua file ( I know you're using json ) that has data
return {
  itema = "thing",
  itemb = "thing",
}

Code: Select all

--the loading code
for k, v in pairs( love.filesystem.getDirectoryItems( dir ) do
  --get the file
  local t = pcall( love.filesystem.load( fileLocation )( ) )
  --t is now that table from the file. Do something with it.
end
ohhhhhh now I get it, so, I'm moving back from json to lua... that means that I don't need no more the JSON library, right?
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: [Need Help] Loading Dynamic Content (aka mods)

Post by MicroMacro »

If you choose to go with the method I said, yeah.
https://github.com/ebernerd- where you can find all my work.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 70 guests