concatenate two separate variables to equal an existing object?

for the sake of the example i’ll limit the objects and variables in my code to the following 

local Blair = display.newImage("AR.png") local BlairS1 = display.newImage("GC.png") local enemy.target = Blair local enemy.targetSpot = S1

to explain, “Blair” is my hero character. There are a total of 6 spots around him that an enemy can move to and attack the hero from, so I have more objects for each spot (BlairS1, BlairS2, BlairS3, BlairS4, BlairS5, BlairS6)

I am trying to get the enemy to concatenate the variables “enemy.target” and “enemy.targetSpot”

local moveTo = enemy.target .. enemy.targetSpot

I was assuming that the result of this would be “BlairS1” (the existing object), but I get this error

“Attempt to concatenate field ‘target’ (a table value)”

Anyone have an Idea on how I would get this to work?

You can only concatenate strings together (and also numbers, but that’s because Lua converts them to strings).  “Blair” is a display object, which is really just a table.  A simple workaround is to give Blair a name field (Blair.name = “Blair”) and use that.  However I suspect there is probably a better way of accomplishing your task.  

You could put Blair and his six spots into a display.newGroup() and then move the group around.

The concatenation operator only joins strings together as JonPM said.

Here is the Lua definition of the concatenation operator: http://www.lua.org/pil/3.4.html

You can concatenate names as JonPM said, but if you want to be able to concatenate these names and then use the string you’ve created to reference an object, the first thing that comes to mind is using a table, as you can index the objects in the table using strings. The following may not be at all suitable for what you’re trying to achieve, as which things have .name properties and which are in tables would depend on which things need to be concatenated and which things need to be referenced with the concatenated strings…

local Player = {} local Player["Blair"] = display.newImage("AR.png") local Player[BlairS1"] = display.newImage("GC.png") local enemy.target = Blair local enemy.target.name = "Blair" local enemy.targetSpot = S1 local enemy.targetSpot.name = "S1" local moveTo = enemy.target.name .. enemy.targetSpot.name

moveTo is now a string, and you can reference the object Player[moveTo] which is now the same as Player[“BlairS1”].

Cheers,

Simon

Dixon Court Entertainment

I don’t think the OP really wants to concatenate as in strings. I think what he really wants is a multiple body object: the main character and the 6 (think hex map) hexagons around him. He wants to move the character + the 6 surrounding objects together. Ergo my suggestion to put them in a group and move the group.

Rob

sorry for the late response, haven’t been able to hop on the computer these last few days.

Rob is right with the thinking of a hex map, but my issue is not moving Blair and his 6 surrounding objects, its moving an enemy to one of the surrounding object.

for movement I use transitions. I have found that for some reason if I use transition to Blair.x + 40 (in order to keep enemy in front of Blair and not overlap, I add +40) the transition slows to a stop instead of keeping a constant speed and its due to the +40.

to overcome this, I used the hex map Idea and use transition to BlairS1.x which would be equivalent to Blair.x +40

The post comes into play because I will have multiple enemies and I dont want them to overlap each other, so I am trying to figure out how to make it so the enemies can scan the hex map and find an open spot to move to.

that is where I came up with the idea for enemy.target and enemy.targetSpot

enemy.target will tell the enemy who to attack, and enemy.targetSpot would tell the enemy where to move. (insert original post here).

Although I do think you guys came up with some great ideas and I think I might try a few out because it might help clean things up in my code.

Rob, I was interested in your idea of adding the hero and all surrounding objects into the same display group. The only problem is that I already have all objects in a display group for layering purposes and I know it wont allow me to put an object into 2 groups. I was wondering though, would I be able to put an existing display group with objects in it into a new display group?

I figure it would be like a table within a table and that would work instead of having one object in 2 different groups.

You can only concatenate strings together (and also numbers, but that’s because Lua converts them to strings).  “Blair” is a display object, which is really just a table.  A simple workaround is to give Blair a name field (Blair.name = “Blair”) and use that.  However I suspect there is probably a better way of accomplishing your task.  

You could put Blair and his six spots into a display.newGroup() and then move the group around.

The concatenation operator only joins strings together as JonPM said.

Here is the Lua definition of the concatenation operator: http://www.lua.org/pil/3.4.html

You can concatenate names as JonPM said, but if you want to be able to concatenate these names and then use the string you’ve created to reference an object, the first thing that comes to mind is using a table, as you can index the objects in the table using strings. The following may not be at all suitable for what you’re trying to achieve, as which things have .name properties and which are in tables would depend on which things need to be concatenated and which things need to be referenced with the concatenated strings…

local Player = {} local Player["Blair"] = display.newImage("AR.png") local Player[BlairS1"] = display.newImage("GC.png") local enemy.target = Blair local enemy.target.name = "Blair" local enemy.targetSpot = S1 local enemy.targetSpot.name = "S1" local moveTo = enemy.target.name .. enemy.targetSpot.name

moveTo is now a string, and you can reference the object Player[moveTo] which is now the same as Player[“BlairS1”].

Cheers,

Simon

Dixon Court Entertainment

I don’t think the OP really wants to concatenate as in strings. I think what he really wants is a multiple body object: the main character and the 6 (think hex map) hexagons around him. He wants to move the character + the 6 surrounding objects together. Ergo my suggestion to put them in a group and move the group.

Rob

sorry for the late response, haven’t been able to hop on the computer these last few days.

Rob is right with the thinking of a hex map, but my issue is not moving Blair and his 6 surrounding objects, its moving an enemy to one of the surrounding object.

for movement I use transitions. I have found that for some reason if I use transition to Blair.x + 40 (in order to keep enemy in front of Blair and not overlap, I add +40) the transition slows to a stop instead of keeping a constant speed and its due to the +40.

to overcome this, I used the hex map Idea and use transition to BlairS1.x which would be equivalent to Blair.x +40

The post comes into play because I will have multiple enemies and I dont want them to overlap each other, so I am trying to figure out how to make it so the enemies can scan the hex map and find an open spot to move to.

that is where I came up with the idea for enemy.target and enemy.targetSpot

enemy.target will tell the enemy who to attack, and enemy.targetSpot would tell the enemy where to move. (insert original post here).

Although I do think you guys came up with some great ideas and I think I might try a few out because it might help clean things up in my code.

Rob, I was interested in your idea of adding the hero and all surrounding objects into the same display group. The only problem is that I already have all objects in a display group for layering purposes and I know it wont allow me to put an object into 2 groups. I was wondering though, would I be able to put an existing display group with objects in it into a new display group?

I figure it would be like a table within a table and that would work instead of having one object in 2 different groups.