Page 1 of 1

Help turning a drawable name into a string.

Posted: Tue Jun 30, 2015 4:05 pm
by Cookie10monster
Hello, I am working on a game and I want to have it so that when you mouse over an item in your inventory, it tells you what it is. Currently, I have all the items in the Inventory as drawables. ex. Inventory.item[1] = Stone (stone is a drawable). I think I need to move away from this system and find a different way to store the information, but I am not sure which way I should do this. Should I have the items be just a name and have a table with the images for it? ex.
Images[Stone] = love.graphics.newImage("Stone.png)
Inventory.item[1] = Stone
love.graphics.draw(Images[Inventory.item[1]]

of should I somehow create a Stone object and attach a drawable to it? Thank you!

Re: Help turning a drawable name into a string.

Posted: Tue Jun 30, 2015 4:09 pm
by Muzz

Code: Select all

print(tostring(Inventory.item[1]))
Thats the easiest way to do it, though i would suggest just saving a string internally in each object.

Code: Select all

images[stone].name = "stone"

print(Inventory.item[1].name)

Hope that makes sense.

Re: Help turning a drawable name into a string.

Posted: Tue Jun 30, 2015 5:18 pm
by Cookie10monster
tostring just printed "image", but I am working on the second option. Thank you!

Re: Help turning a drawable name into a string.

Posted: Tue Jun 30, 2015 6:40 pm
by Muzz
Haha whoops yeah.

That's what i get for providing help at 5 am in the morning >_>

Re: Help turning a drawable name into a string.

Posted: Wed Jul 01, 2015 1:06 pm
by Ranguna259
In your exemple, Stone, is a variable that has not be defined yet, not a string variable.
When you do:

Code: Select all

Images[Stone] = love.graphics.newImage("Stone.png)
(firstly, you'll get an error because you forgot a " in the end of "Stone.png, this is the correct way of doing this:)

Code: Select all

Images[Stone] = love.graphics.newImage("Stone.png")
Secondly, if you don't define the variable Stone then that is the same way as writing this:

Code: Select all

Images[nil] = love.graphics.newImage("Stone.png")
And you'll get an error saying that you can't index a nil value, so you probably want to do this:

Code: Select all

Images["Stone"] = love.graphics.newImage("Stone.png")
You have to put Stone inside "", this way Lua will interpert Stone as a string and not as a variable.
Or you could define the variable Stone first, like so:

Code: Select all

Stone = "Stone" --variable Stone in a string variable containing the string "Stone"
Images[Stone] = love.graphics.newImage("Stone.png")
After that what, you'll want to do to save the name of the item is to write the string "Stone" into your item table:

Code: Select all

Inventory.item[1] = "Stone" 
or:

Code: Select all

Stone = "Stone"
Inventory.item[1] = Stone