Creating new instances

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Creating new instances

Post by Miken1 »

Sorry for the title, don't know what this is called :roll:

Anyways, so I am playing around with physics and i have this box wich spawns if I press "E" but when i release the button it disappeares but i wan't it to spawn, drop to the ground, spawn a new one while the other one stays on the ground, repeat.


Hope i explained this well enough, thanks.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Creating new instances

Post by Nixola »

Well, it's quite hard to help you without being able to see your code, could you please upload a .love file?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Re: Creating new instances

Post by Miken1 »

I thought that was not needed but sure.

Code: Select all

  
  obj = {}

  obj.height = 50
  obj.width = 50
  obj.y
  obj.x
  obj.yvel
  obj.xvel
  obj.pic = love.graphics.newImage("box.png")
   
      
   
function objectMouse() 
 
  obj.x = love.mouse.getX()
  obj.y = love.mouse.getY()
  
  if love.keyboard.isDown("e") then
  
     spawn = true
   	 
	 else
	 spawn = false
  
  end
end
  
function objectSpawn()
  if spawn == true then
    love.graphics.draw(obj.pic, obj.x, obj.y)  
  end
end
  
     
function DRAW_OBJECT()

     objectSpawn()
  
   end
    
function UPDATE_OBJECT(dt)

     objectMouse()

end

So here it is and let me explain it again.
When I press "E" the box that is bound to the mouse position gets drawn but it's still at the mouse position wich is my problem because I wan't it to fall to the ground so I can spawn a new one.

Sorry for the inconvenience i just thought my code wasn't necessary.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Creating new instances

Post by Roland_Yonaba »

Make distincts objects.

Code: Select all

-- A register for all spawned objects
local objects = {}

-- a factory object function
local function spawnObject(x,y, width, height,...)
  -- create the object
  local newObject = {
    x = x, y = y,
    width = width, height = height,
   -- and whatever else you might want to add
  }
  
  --register the object
  table.insert(objects,newObject)

  -- return it. Unused here, but it might be useful.
  return newObject
end
Then, when pressing key "E".

Code: Select all

function love.keypressed(k,u)
  if k == 'e' then
    spawnObject(...) -- the three dots are just placeholders, fill them with your own values
  end
end
Obviously, if you need to display all your objects, move or animate them, you will have to loop through the collection of objects.

Code: Select all

for i, object in ipairs(objects) do
  -- do_some_stuff_with object
end
Hope this helps.

PS/ Wrote this from scratch, so it might contain some typos. Just get the logic. :)
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Re: Creating new instances

Post by Miken1 »

Distinct objects, that's it! Thanks alot!
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Re: Creating new instances

Post by Miken1 »

Can this be explained in an easier way? Having trouble to implement and understand it.

Thanks.
User avatar
ac3raven
Citizen
Posts: 60
Joined: Tue May 19, 2009 1:14 am

Re: Creating new instances

Post by ac3raven »

Check this out:

https://www.youtube.com/watch?v=S2oO2wbsXQs

In that tutorial, the bullets are created in a manner similar to how you want to create your physics objects.
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Re: Creating new instances

Post by Miken1 »

ac3raven wrote:Check this out:

https://www.youtube.com/watch?v=S2oO2wbsXQs

In that tutorial, the bullets are created in a manner similar to how you want to create your physics objects.
Just found this video before checking here!
Thanks alot, I really löve this helpful community.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Creating new instances

Post by Roland_Yonaba »

Miken1 wrote:Can this be explained in an easier way? Having trouble to implement and understand it.
Thanks.
No problem. :awesome:

The following is going to be a long and boring explanation of the logic given above.
For implementation purpose, you can still refer to my previous post.


Let us consider a not so different example, where you want to have different objects drawn at the same time.
Each object stand on its own, and is different from the others. Therefore, it should have its own attributes (x,y, width, height, etc.).

I will assume that an object is represented by a table. If I need 3 different objects,i'll have to create three tables.
But because all those objects are similar, they can share a certain template. So I can basically write a factory function that returns a new object.

Code: Select all

local function spawnObject(x,y, width, height)
  -- create the object
  local newObject = {x = x, y = y, width = width, height = height}

  -- return it.
  return newObject
end
The function above can be used this way:

Code: Select all

local obj1 = spawnObject(10,10, 50, 50) -- A 50x50 object spawned at position 10,10
local obj2 = spawnObject(30, 75, 25, 25) -- A 25x25 object spawned at position 30,75
This is nice, because although obj1 and obj2 have the same template, they are different.

Code: Select all

-- move obj1
print(obj1.x) --> 10
obj1.x = obj1.x + 10
print(obj1.x) --> 20

--move obj2
print(obj2.x) --> 30
obj2.x = obj2.x + 10
print(obj2.x) --> 40
At this point, everything sounds nice. But you might encounter a problem. What if you are planning to work with a large number of objects ?
And this is where the fun begins. Basically, you can create another table (let us name it collectionOfObjects) where you will register all your objects.

Code: Select all

local collectionOfObjects = {}
local function spawnObject(x,y, width, height)
 -- same as before
end

-- spawning objects
local obj1 = spawnObject(10,10, 50, 50)
table.insert(collectionOfObjects, obj1) -- register obj1 in collection
local obj2 = spawnObject(30, 75, 25, 25) -- A 25x25 object spawned at position 30,75
table.insert(collectionOfObjects, obj2) -- register obj2 in collection
In case you want to move all your objects, for instance, this can be done fairly simply, by looping through the entire collectionOfObjects with ipairs:

Code: Select all

for i,obj in ipairs(collectionOfObjects) do
  obj.x = obj.x + 10
end
See ?
Last minor enhancement, you might find painful to register manually each new object.
Fair enough, this can be done automatically by the factory function.

Code: Select all


local collectionOfObjects = {}

local function spawnObject(x,y, width, height)
  -- creates the object
  local newObject = {x = x, y = y, width = width, height = height}
 
  --register the object
  table.insert(collectionOfObjects,newObject)

  -- return it.
  return newObject
end
Usage example:

Code: Select all

local obj1 = spawnObject(10,10, 50, 50) -- obj1 is already registered in the collectionOfObjects at position 1
print('Let us check if obj1 == collectionOfObjects[1]', obj1 == collectionOfObjects[1]) --> true

local obj2 = spawnObject(30, 75, 25, 25) -- obj2 is already registered in the collectionOfObjects at position 2
print('Let us check if obj2 == collectionOfObjects[2]', obj2 == collectionOfObjects[2]) --> true
That's the general idea. Later on, you can bind the factory function to trigger a new object when a specific key is pressed.
or whatever. It depends on your needs.

Hope this helps.
Feel free to ask if anything goes wrong. :ultraglee:
User avatar
Miken1
Prole
Posts: 42
Joined: Tue Nov 05, 2013 8:35 am
Location: Sweden

Re: Creating new instances

Post by Miken1 »

This is amazing! thank you soooo much.

Just one last question, why do you use local functions instead of just normal functions?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests