Help turning a drawable name into a string.

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
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Help turning a drawable name into a string.

Post 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!
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Help turning a drawable name into a string.

Post 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.
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: Help turning a drawable name into a string.

Post by Cookie10monster »

tostring just printed "image", but I am working on the second option. Thank you!
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Help turning a drawable name into a string.

Post by Muzz »

Haha whoops yeah.

That's what i get for providing help at 5 am in the morning >_>
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Help turning a drawable name into a string.

Post 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 
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 4 guests