Page 1 of 1

Best way to make multiple items?

Posted: Sat Sep 01, 2018 3:13 am
by HarryCourt
Hello everyone! Newbie here!

I am having an issue with my game. I want to have various items, such as a stick, glass bottle and rock. I have it planned out; Give it a sprite, then a health value and a ID. then whenever the player clicks on the screen, decrease the health value. If the health value reaches 0, get rid of that item, and replace it with another at random (Using the ID variable).

However, I have no clue on how to do this. People have suggested doing it with classes, but I have no idea how to make a class in another script and execute it in main. How could I do this? Are there any suggestions?

Re: Best way to make multiple items?

Posted: Sat Sep 01, 2018 5:28 pm
by pgimeno
Hi, welcome to the forums. You can use classes, or not, but either way, knowing how to organize your code in separate files is essential to keep it maintainable, otherwise as your projects grow, you end up with a huge main.lua where you can no longer find the stuff you need. (Edit: And Lua/LuaJIT has a limit of 256 locals active per file, which makes this even more of a necessity)

The main thing to keep in mind is that the Lua files can return values, and you can read these values as the result of require(). For example, you could have in classes/object.lua something like:

Code: Select all

local ObjectClass = {}
local ObjectClassMT = {__index = ObjectClass}

function ObjectClass.new(id, sprite, health)
  local instance = {}
  instance.id = id
  instance.sprite = sprite
  instance.health = health
  return setmetatable(instance, ObjectClassMT)
end
-- add other methods here

return ObjectClass
Then in your main.lua:

Code: Select all

local ObjectClass = require('classes.object')

local scene = {}

local function newStick()
  return ObjectClass.new(1, love.graphics.newImage('images/stick.png'), 10)
end

local function newBottle()
  return ObjectClass.new(2, love.graphics.newImage('images/bottle.png'), 4)
end

local function newRock()
  return ObjectClass.new(3, love.graphics.newImage('images/rock.png'), 30)
end

...
Hope that helps.

Re: Best way to make multiple items?

Posted: Sun Sep 02, 2018 12:26 am
by HarryCourt
pgimeno wrote: Sat Sep 01, 2018 5:28 pm Hi, welcome to the forums. You can use classes, or not, but either way, knowing how to organize your code in separate files is essential to keep it maintainable, otherwise as your projects grow, you end up with a huge main.lua where you can no longer find the stuff you need. (Edit: And Lua/LuaJIT has a limit of 256 locals active per file, which makes this even more of a necessity)

The main thing to keep in mind is that the Lua files can return values, and you can read these values as the result of require(). For example, you could have in classes/object.lua something like:

Code: Select all

local ObjectClass = {}
local ObjectClassMT = {__index = ObjectClass}

function ObjectClass.new(id, sprite, health)
  local instance = {}
  instance.id = id
  instance.sprite = sprite
  instance.health = health
  return setmetatable(instance, ObjectClassMT)
end
-- add other methods here

return ObjectClass
Then in your main.lua:

Code: Select all

local ObjectClass = require('classes.object')

local scene = {}

local function newStick()
  return ObjectClass.new(1, love.graphics.newImage('images/stick.png'), 10)
end

local function newBottle()
  return ObjectClass.new(2, love.graphics.newImage('images/bottle.png'), 4)
end

local function newRock()
  return ObjectClass.new(3, love.graphics.newImage('images/rock.png'), 30)
end

...
Hope that helps.
Hey! Thank you for the reply! I sort of get it, but I am having another issue. How do I remove the health from the item that is instanced, and then once that health is <= 0, how do I instance another using random integers between a specific number?

For assistance, I've added the file "breakit.love" for easier access. Hope you can help me out here ^^

Re: Best way to make multiple items?

Posted: Sun Sep 02, 2018 11:15 pm
by pgimeno
I have no idea of how your game is supposed to work, and I'm having trouble understanding what you meant above.

Anyway, I think you need to decide whether you want to learn the intricacies of object-oriented programming and go that route, or to keep it in a more classical way. In principle, the three functions that I provided are for creating objects that you would draw, of the three kinds that you pointed out. You're not using any of them, therefore you're not taking advantage of object-oriented programming, and you probably should start with something that doesn't use it, until you get the gist of programming itself.

Teaching the basics that you require would probably be too long for being in forum format. I suggest you look for tutorials that show you some basics, try to learn from the examples how things are done, and then apply what you learned to making your own game.