image generalization

Your Original Question
 

is there any way that i can have multiple images under one name …

 
Answer: If I take your question at face value, then “No.”  That is not a thing.   All image files need have unique names on the ‘disk’.
  
Longer Answer: 
Of course I don’t think that is what you meant.  What I think you want is to be able to create game objects using any image you want, but to have all of those objects be considered ‘obstacles’, ‘pickups’, or some other category of things.
 
In other words, I think you want to employ logic in your game that treats objects by their category, and your question is, “How do I group objects by category?”
 
Use Arbitrary Fields (like id)
This has been pointed out to you and I think you mostly get it.
 
@nick_sherman ponited out that you can add an arbitrary field to any display object and later check for it.
 
Ex:

local obj = display.newImage( ... ) obj.id = "obstacle"

 
In my second-to-last post I quoted your post of this code:

ob1 = display.newImage("TLCorner.png") ob1.xScale = 0.45 ob1.yScale = 0.45 ob1.x = 201 ob1.y = 112 ob1.id = obstacle 

 
I wanted to show that you could simplify it and wanted to show you how the concept could be applied using a builder. 
 
What I didn’t mention was, “Your code is wrong.”  Specifically this line:

 ob1.id = obstacle

That line, all similar lines  should have quotes around the word obstacle.

 ob1.id = "obstacle"

 
Syntax Errors?
Later in the thread, you posted:
 

Contextually, I don’t know what you mean, but I’m guessing you typed in some code and got errors.  We can’t do much to help you with that if we don’t see the code.  Still, all you need to solve syntax errors is provided right in the console.  Just go to the line specified, or the few lines before that and you should see the source of your syntax error.
 
 
Back To The Original Question

… i have multiple obstacle images and i dont want to code each ones interactions with the player, is there a way i can generalize multiple images into one name like obstacle?

Answer: I think what you want here is a collision listener for the player object that knows how to deal with different categories of collided objects.

You would do that like this:

function player.collision( self, event ) local phase = event.phase local other = event.other -- thing player collided with if( phase == "began" ) then if( other.id == "obstacle" ) then -- do whatever you are supposed to with obstacles here elseif( other.id == "pickup" ) then -- do whatever you are supposed to with pickups here elseif( other.id == "danger" ) then -- do whatever you are supposed to with dangers here end end return false end player:addEventListener( "collision" )

That is about all I can get out of all the posts I’ve seen in this thread.  I hope this is what you’re looking for.

I’m in the same boat as Ed in that I don’t seem to fully understand what it is that you are asking.

I’m just taking a blind guess here at what I think you might be asking.

If you create display objects called ob1, ob2, etc. then the only way that you can refer back to them specifically is by referring to the actual object, e.g. the only way to get the x coordinate or id of ob1 is to use ob1.x or ob1.id, etc.

Commonly, when you are creating a large amount of similar objects, such as tiles, then you create a table. For example, when using your code as you presented it:

local ob = {} ob[1] = display.newImage("TLCorner.png") ob[1].xScale = 0.45 ob[1].yScale = 0.45 ob[1].x = 201 ob[1].y = 112 ob[1].id = "obstacle" ob[2] = display.newImage("TMidCent.png") ob[2].xScale = 0.45 ob[2].yScale = 0.45 ob[2].x = 240 ob[2].y = 112 ob[2].id = "obstacle" -- etc.

With tables, you can refer to objects, like tiles, much easier. As you could check the x coordinate or id of any element in the ob table by using ob[n].x or ob[n].id, where n is a number you want.

In your specific use, if I still remember it correctly, if you have… let’s say X number of rows and Y number of columns, then you could create the tiles in the following loop.
 

local tile = {} local rowCount = 8 local columnCount = 12 for i = 1, rowCount do tile[i] = {} for j = 1, columnCount do tile[i][j] = display.newRect( x, y, width, height ) end end

This would create a table for each row and fill that table with as many tiles as there are columns. This style also allows you to control the size and coordinates of all the tiles from one location. But, with this method, you could refer to any specific tile by using tile[row][column] code, e.g. if you want to refer to tile on row 2, column 3, you’d write tile[2][3].

I hope this answer was at least close to what you were asking :stuck_out_tongue:

 

my teacher said something about having them in a table and then just checking if that name is in the table, how would i do that?

Your teacher?  Why isn’t your teacher helping with this?  

I’m sorry if you’re not getting sufficient help from your teacher, that bites.

… to the question 

The use of the term ‘name’ doesn’t make any sense in this context.  Anything you use as an index in a table must be unique or you are referencing the same position in the table.

So, if you did this:

local objs = {} objs["obstacle"] = display.new...() objs["obstacle"] = display.new...()

You will have created two objects, but only one (the last one created) will be referenced in the table.

Perhaps, your teacher meant or said, ‘reference objects by their ID’.

local objs = {} local tmp = display.new...() objs[tmp] = tmp local tmp = display.new...() objs[tmp] = tmp

The table above has two unique entries, each referencing one of the two created objects.

However, because you used non-numeric indices, you have to iterate over the table like this:

for k,v in pairs( objs ) do ... do some work on the objects here v is the object k is the index end

For a concrete example, lets change the color of all objects in the table to red:

for k,v in pairs( objs ) do v:setFillColor(1,0,0) end

Now, lets apply the ID concept.  Let’s add a few objects and set pickups to yellow and obstacles to red:

local objs = {} -- make two pickup objects local tmp = display.new...() tmp.id = "pickup" objs[tmp] = tmp local tmp = display.new...() tmp.id = "pickup" objs[tmp] = tmp -- make two obstacle objects local tmp = display.new...() tmp.id = "obstacle" objs[tmp] = tmp local tmp = display.new...() tmp.id = "obstacle" objs[tmp] = tmp

later…

for k,v in pairs( objs ) do if( v.id == "pickup" ) then v:setFillColor(1,1,0) elseif( v.id == "obstacle" ) then v:setFillColor(1,0,0) end end

What about removing objects from the table?

I’m going to use the collision code I provided in a prior post and assume that all objects are in the table objs and that objs is visible in the collision listener.

If all that is true, I can do this:

-- somewhere above the listener local score = 0 local objs = {} function player.collision( self, event ) local phase = event.phase local other = event.other -- thing player collided with if( phase == "began" ) then if( other.id == "obstacle" ) then -- do whatever you are supposed to with obstacles here elseif( other.id == "pickup" ) then -- lets increment the score, delete the pickup object, and remove it from the tracking -- table score = score + 1 display.remove(other) objs[other] = nil elseif( other.id == "danger" ) then -- do whatever you are supposed to with dangers here end end return false end player:addEventListener( "collision" )

If this is still not what you want, I suggest we back up a bit.

Tell us clearly and concisely what your game is and the mechanics in the game.  Please try to use clear sentences, distinct paragraphs,  and bullet lists if you can.

Knowing more about your project may help us to help you.

right now i know how to make tables and the stuff i need to do for it, i just dont know how to call to a table say for example, i make the table, now i just need to know how i can check the table for its contents, the rest im sure i can do from there

It really seems that you should ask your teacher. Ed and I have given you plenty of examples on tables already and it seems that none of our suggestions or advice are helping you out. It also seems that a lot of information is lost in translation.

If you want to output a table’s contents, you’d use https://docs.coronalabs.com/tutorial/data/outputTable/index.html, but I already know that this isn’t what you are actually asking, even if you said that you wanted to check the contents.

Since you create the tables yourself, then you already know all of the contents of those tables may have. Also, for clarity sake, display objects in Corona are also lua tables, which means that when you create a display object, they are treated as tables.

So, help us help you. What is it exactly that you want to do with tables?

  1. When you say that you want to check the table for its contents, what do you literally mean?
  • My previous post included examples of how you could refer to a table and get information from there, but apparently that wasn’t what you needed.

2) What kind of information will be in such a table and why do you need to check it?

  • It is important to understand what type of information you will be storing in your tables and the exact use you have for them, i.e. why do you need to check something from the table? Is it to identify if an object is an obstacle? Great, but in what sense and where do you need to detect it? For collisions, or?

Yep, you just give them all a common .id field or whatever you want to call it when the objects are spawned. Then in your collision listeners etc. you run different code depending on the .id field i.e enemy, powerup, obstacle.

ok how do i do that?

Display objects are just sophisticated lua tables, therefore you can add custom keys to the object like you would any other lua table.

[lua]

local image1 = display.newRect(0,0, 100, 100)

image.id = “obstacle”

image.name = “Rock”

image.hitPoints = 10000

local image2 = display.newRect(200, 200, 50, 50)

image2.id = “enemy”

image2.name = “Tony the Alien”

image2.hitPoints = 50

local image3 = display.newRect(300, 300, 200, 10)

image3.id = “enemy”

image3.name = “Dave the Snake”

image3.hitPoints = 10

[/lua]

Sure, I’d expect Rock to be tough, but not 200 times tougher than Tony the Alien in terms of hp. That’s a great example, but I think your math is wrong here, unless that Rock happens to also be called Dwayne.

It’s all relative. Tony and Dave have absolutely no stomach for a fight.

Dave prefers origami… sometimes a bit of cross-stitch

doing that creates an error for me

You are seeing an error because nick has a few typos in his sample code. On line 2, he creates “image1”, but then he refers to just “image” on rows 3, 4 and 5.

If you run it locally, you’ll receive an error message. My error message is “main.lua:2: attempt to index global ‘image’ (a nil value)” and simply means that on row 2, there is no “local image”, i.e. it has a nil value. You need to learn some basic debugging instead of just copying the code. :stuck_out_tongue:

i didnt copy his code, i put what i saw into mine, i dont typically use local since im told they arent necessary for what im doing

fixed it

 Well, you may not have copied all of his code, but you copied enough to reproduce nick’s error. :stuck_out_tongue:

Also, while using locals is not absolutely necessary, I would highly recommend on using them as locals are less risky than globals and they hog far less resources. Or in other words, globals are more prone to bugs, data leaks and will potentially make your game lag more than using locals.

You can read up on https://docs.coronalabs.com/tutorial/basics/globals/index.html.

ok so i have the whole id thing down but now that i think about it how will i call to it without just having to type the name anyways? i want to be able to just check if it falls under “obstacle” and thats when stuff happens

ob1 = display.newImage("TLCorner.png") ob1.xScale = 0.45 ob1.yScale = 0.45 ob1.x = 201 ob1.y = 112 ob1.id = obstacle ob2 = display.newImage("TMidCent.png") ob2.xScale = 0.45 ob2.yScale = 0.45 ob2.x = 240 ob2.y = 112 ob2.id = obstacle ob3 = display.newImage("TRCorner.png") ob3.xScale = 0.45 ob3.yScale = 0.45 ob3.x = 278 ob3.y = 112 ob3.id = obstacle ob4 = display.newImage("StubLeft.png") ob4.xScale = 0.45 ob4.yScale = 0.50 ob4.x = 160 ob4.y = 158 ob4.id = obstacle ob5 = display.newImage("mid.png") ob5.xScale = 0.45 ob5.yScale = 0.55 ob5.x = 201 ob5.y = 157 ob5.id = obstacle ob6 = display.newImage("mid.png") ob6.xScale = 0.45 ob6.yScale = 0.55 ob6.x = 240 ob6.y = 157 ob6.id = obstacle ob7 = display.newImage("mid.png") ob7.xScale = 0.45 ob7.yScale = 0.55 ob7.x = H ob7.y = 157 ob7.id = obstacle

There are numerous ways for doing that, so it depends on what exact use you need.

For instance, are they physics objects and you want to check their id for collision purposes? Then check https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling.

Or, if you want to know what the object’s id is when it is touched by the player, then check out https://docs.coronalabs.com/api/event/touch/index.html.

Or, if we are talking about that grid movement, you might want to check the nearby tiles ids to see if they are obstacles or not. In this case, a simple if else statement would check something like “if tile[xy].id == “obstacles” then” and so on.