Is it possible to add variable values to texture name when calling for a draw?

Hi,

I was wondering. Is it possible to add a variable to a texturename so when i draw an image the texture drawn is dependant of the value of the variable?
And how do I do that?

For example?
I want to draw a different texture dependant of my players nr. of lifes left.

Number of lifes is stored in the variable player_life

And i have different textures: player_ 01, player_02, player_03 etc

Now how would i do something like this:

draw(player_0[+player_life], x,y)

Or do I need an extra line of code to set which texture i want to use?

What rendering method are you using?

I’m drawing on a spritebatch (libgdx)

You can do that same thing if you store the textures in a texture array called player_0

Yes. But then I still have to add and initialise an array somewhere plus i have to add all the different textures manually (I believe) to the different array positions.

If i want to do it like that & automate the proces i guess i should probably use textureRegion like this f.e.:


say :
int player_life = 2;
 
& I have 3 textures (blocks1,blocks2,blocks3) stored in a TextureRegion[]

		blocks = new TextureRegion[3];
		for(int i = 0; i < blocks.length; i++) {
			blocks[i] = new TextureRegion(tex, 32 + i * 16, 0, 16, 16);
		}

....
i can draw 2nd texture by: 

sb.draw(blocks[player_life], x, y);


But what I actually would like to know is if it is possible to call for different textures to be drawn the way i stated above.

I don’t think so. But just for clarification, you are talking about having 3 separate texture variables which share all of the same letters in them except for one. You are then trying to stitch a number corresponding to the texture you want on the end of the variable tag when you call the texture for drawing. Basically treating it like a string?

Yes

You’re just creating a sprite sheet, that’s pretty normal usage of TextureRegions.
Note though that [icode]blocks[player_life][/icode] is the 3rd texture, not 2nd. (2 is the 3rd element, 1 would be 2nd)

If you ever want to do animation, sprite sheets are often used for that as well: https://github.com/libgdx/libgdx/wiki/2D-Animation

blocks[2] is indeed the third…my bad ;p

But then… so I guess it’s really not possible to stich a variable to a string to create the specific texture’s name you want to call within a draw call the way i would like to?

Well, if you wanted to use a literal [icode]String[/icode], you have to have some method to “parse” it, hence why it will always be less efficient (and shoddy) than just an indexed table.

It could be simple:


Map<String, TextureRegion> texMap;

draw(texMap.get("player_0" + playerLife));

Of course this is stupid.

Using actual variables, and no Strings, just using a different form of table:


TextureRegion player_01, player_02, player_03;

TextureRegion toUse = null;

switch (playerLife) {
    case 1:
        toUse = player_01;
        break;
    case 2:
        toUse = player_02;
        break;
    case 3:
        toUse = player_03;
        break;
}

draw(toUse);

I think is close to what you meant, but there are no macros or similar in java to generate this code for you. I hope you see that this is still quite unnecessarily painful.
The equivalent is this as shown earlier:


TextureRegion[] textures;

draw(textures[playerLife]); // or if a more complicated mapping is required: textures[mapFunc(playerLife)]

The real question: why would you have separate variables to begin with? Ideally you have an array (or List, etc) from the start, and this whole thing becomes a non-issue.

Much appreciated BP.