Linking strings / textures together somehow

In my most recent WIP game I have ran into a little trouble which has got me thinking but my idea seems a little too complex. So here is what I want to do. After I open a menu and click the item I want to place, the menu closes and then I click where i want to place it and everything works great. Now here is the problem: How do I link each string (drawn in the menu so you know which one to click) with a (libgdx) texture (which is drawn once you click somewhere) Is there anyway to do this or would I have to just do something like this:


if(buildingStringIsClicked){
     closeTheMenu;
     batch.draw(twoStoryBuilding, 200, 200);
}
if(somethingElseStringIsClicked){
     closeTheMenu;
     batch.draw(someTexture, 200, 200);
}
//more if statements here for each string

Alternatively I could even create a method that takes a string and a texture as a parameter. The only reason I don’t want to do this is there may be around 100 different placable items creating a lot of method calling or if statements. I am not actually you can link a string and a texture together but does anyone have any advice in a direction to go from here, or would this method above be the way to go? All input is appreciated :smiley:

There are many ways of doing this. You could use a HashMap<String, Texture> and work with that.

Read up on hashmaps: here and here.


Map<String, Texture> map = new HashMap<String, Texture>();

//put texture
m1.put("Option 1", textureObject);

//get texture
m1.get("Option 1");

Note however, that this method is probably inefficient, unnecessary, and a waste of memory.

You can also use Enums to do what you want.

Since hash maps may be inefficient for this task, would that still be your first recommendation, or would u recommend enums first?

Using a HashMap is faster than your current code anyway.

If you can’t do it with enums, then use HashMaps.

Awesome, I will look into it more this weekend thank you for the help and links!

I just remembered about this post and went to go try out these hash maps and they are surprisingly cool! I feel like such a nerd 8)

Not sure if you would want to do this, or if it’s even recommended by better programmers than myself but you can always declare your own class and extend Texture.

It would look like…


public class Texture2 extends Texture
{
    String str;
    
    public Texture2(FileHandle file, String str)
    {
        super(file);
        this.str = str;
    }
    //... More constructors here
    
    // You can also add get/set methods if you want
}

In the main code just change ‘new Texture(blah)’ to ‘new Texture2(blah, string)’

Then it’s always tied to that texture.

Edit: Updated the code example.

You will find that HashMaps are very useful for lots of things in game development.

I would not have it linked to a texture in the way you are trying. Have you object/entity/building have a texture it uses to draw itself with and then set that texture. I would have a hashmap like everyone says but create it once during start up probably when you load the resources. Heck, if you have a resource manager class, just use that.