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?
Best way to make multiple items?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- HarryCourt
- Prole
- Posts: 3
- Joined: Sat Sep 01, 2018 3:07 am
Best way to make multiple items?
"He would often tell me that he didn't mind if people thought of him as cold or distant, he said that he knew he was actually a vibrant and compassionate person, but it takes time to really see that. It can be a very slow climb to get there."
Re: Best way to make multiple items?
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:
Then in your main.lua:
Hope that helps.
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
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
...
- HarryCourt
- Prole
- Posts: 3
- Joined: Sat Sep 01, 2018 3:07 am
Re: Best way to make multiple items?
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?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:
Then in your main.lua: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
Hope that helps.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 ...
For assistance, I've added the file "breakit.love" for easier access. Hope you can help me out here
- Attachments
-
- Game by Harry.zip
- Game File
- (2.78 KiB) Downloaded 130 times
"He would often tell me that he didn't mind if people thought of him as cold or distant, he said that he knew he was actually a vibrant and compassionate person, but it takes time to really see that. It can be a very slow climb to get there."
Re: Best way to make multiple items?
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.
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.
Who is online
Users browsing this forum: Bing [Bot] and 5 guests