How do you easily do Spawn?

Sorry another beginner question.

So i created a function that creates a ship, but now I want to create multiple ships depending on what the score is. How do I create more instances of the same object?

I created a table ship{} but I don’t know how to assign each instance their name i.e. ship1, ship2 and have it assign automatically the first name available since some of them get destroyed and removed by removeSelf() command.

Thanks in advance.

[import]uid: 8192 topic_id: 1873 reply_id: 301873[/import]

I’ve been using a simple Class type structure so you could have something like this in Ship.lua:

  
module(..., package.seeall)  
  
function new(x, y)  
  
 local self = display.newImage("ship.png", x, y)  
  
 function self:fire()  
 print("Fire!")  
 end   
  
 return self  
  
end  
  

and then in your main.lua file (or anywhere else), just have:

local ship = require("Ship")  
  
local ships = {}  
  
local ship1 = ship.new(10, 10)  
local ship2 = ship.new(40, 40)  
  
ships[#ships + 1] = ship1  
ship1.name = "Ship" .. #ships  
  
ships[#ships + 1] = ship2  
ship2.name = "Ship" .. #ships  

As for respawning in general, rather than removing them explicitly like that I would just have a pool of ship objects that have already been killed (that aren’t removed, just hidden) then you can just reactivate them as needed rather than constantly creating and destroying things.

Hopefully this answer will be of some use however it is kind of rushed as I have to be somewhere, if it didn’t help just say and I will try to help further. [import]uid: 5833 topic_id: 1873 reply_id: 5523[/import]

Thanks for the reply.

These are the part that I don’t understand

  
ships[#ships + 1] = ship1  
ship1.name = "Ship" .. #ships  
   
ships[#ships + 1] = ship2  
ship2.name = "Ship" .. #ships  
  

Also what does this mean

  
module(..., package.seeall)  
  

is it some sort of text to put at the start or does it do something. Thanks for the help again.

[import]uid: 8192 topic_id: 1873 reply_id: 5532[/import]

ships[#ships + 1] = ship1

This is adding an element (in this case, the ship1 table) into the ships table.

Specifically this bit #ships + 1 is saying to place it at the index of whatever the current count of items in the table is (at the beginning this is 0) plus 1. It would be the same as doing this:

ships[1] = ship1  
ships[2] = ship2  

Then this second line: ship1.name = "Ship" .. #ships is setting an element in the ship1 table (called “name”) to the value of “Ship” and whatever the count of items in the ships array, again this is the same as saying this:

ship1.name = "Ship1"  
ship1.name = "Ship2"  

Finally, this line module(..., package.seeall) is placed at the top of external modules ( so in this case, “ship.lua”) and I believe that it is setting the metadata of the module so that it inherits stuff from global, however I am not entirely sure on this (will need to ask someone more in the know). I know it is needed though :slight_smile:

Hopefully that clears some stuff up, if not just ask again.

[import]uid: 5833 topic_id: 1873 reply_id: 5534[/import]

Ok I am having problems implementing this. I have multiple ships flying and when they crash on the ground I need to spawn the graphic of the crater left by the crash.

so I have the following code

  
craters={}  
  
local spawnCrater = function ()   
  
  
 i=craters[#craters+1]  
  
 crater[i] = sprite.newSprite (craterSet)  
 crater[i]:prepare("Glowing")  
 crater[i]:play("Glowing")  
  

It doesn’t seem to work. What I am trying to do is when the crash happens, create a new instance of the crater and give it the next available number. I need to be able to remove that as well after a while.

The method that you mentioned about creating and hiding instances works well and I am using it for the ships, since I know how many total ships I can have at a time on the screen but in this case I could have an unknown number of craters.

Thanks again for the help.

[import]uid: 8192 topic_id: 1873 reply_id: 5547[/import]

Try this for the creation of the craters:

local craters = {}  
  
local spawnCrater = function(x, y)  
   
 local craterIndex = #craters + 1  
  
 local newCrater = sprite.newSprite(craterSet)  
  
 newCrater.x = x  
 newCrater.y = y  
  
 newCrater:prepare("Glowing")  
 newCrater:play("Glowing")  
  
 craters[craterIndex] = newCrater  
  
end  
  
local getCrater = function(index)  
 return craters[index]  
end  
  
local getCraterCount = function()  
 return #craters  
end  

As for the removing of them, you could still use two separate lists. One for active and one for deactivated craters.

local activeCraters = {}  
local inactiveCraters = {}  
  
local getCrater = function(x, y)  
  
 if(#inactiveCraters \> 0) then -- There are craters in the inactive list so reactivate them  
 local crater = table.remove(inactiveCraters)  
  
 activeCraters[#activeCraters + 1] = crater   
  
 crater.x = x  
 crater.y = y  
 crater.isVisible = true  
  
 return crater  
 else -- There aren't any spare deactivated ones, so create a new one  
 local crater = spawnCrater(x, y)  
  
 activeCraters[#activeCraters + 1] = crater   
  
 return crater   
 end  
end  
local destroyCrater = function(index)  
  
 local crater = table.remove(activeCraters, index)  
  
 crater.isVisible = false  
  
 inactiveCraters[#inactiveCraters + 1] = crater   
end  
  

This code may not be perfect as I haven’t checked it, but it should work (in theory :slight_smile: )

Hope it helps. [import]uid: 5833 topic_id: 1873 reply_id: 5548[/import]

So my problem now is that all my functions are not localized to the instance but they are distributed across or usually to another instance. Say if I do something to one ship like destroying it, it actually destroys the first one created. Same for the craters. [import]uid: 8192 topic_id: 1873 reply_id: 5567[/import]

That sounds about right yes, I try to go as much OO (well, as OO as I can make it anyway with Lua) as possible but that’s just how I like it. Everyone’s coding style is different so my methods may not be right for you. [import]uid: 5833 topic_id: 1873 reply_id: 5574[/import]

How would I call the fire function for ship1?

ship1:fire? [import]uid: 8192 topic_id: 1873 reply_id: 5912[/import]

Assuming you’ve created your ship as I described above then yes just call:

ship1:fire()