1. Post #1
    thetree's Avatar
    February 2010
    41 Posts
    Hey,

    How might I go about making bullets?
    I've made a bullet class, but I can't figure out how I go about using them, as in, editing them once they're made.

    I have:
    Code:
    Bullet = {}
    Bullet.new = function(x, y)
    	local self = {}
    	local x = x or love.mouse.getX()
    	local y = y or love.mouse.getY()
    		
    	self.getX = function() return x end
    	self.getY = function() return y end
    	self.setX = function(val) x = val end
    	self.setY = function(val) Y = val end
    	return self
    end
    Obviously I can do a
    Code:
    if fire then
        bullet = Bullet.new(x, y)
    end
    But how can I make more than 1 and be able to modify its position?

    I tried something like
    Code:
    bullets = {}
    for i = 0, 20 do
        bullets[i] = Bullet.new()
    end
    for i = 0, 20 do
        bullets[i].setX(500)
    end
    but that didn't seem to work :/

    Any help?

    Thanks

  2. Post #2
    Gold Member
    BlkDucky's Avatar
    May 2008
    6,477 Posts
    "love.mouse.love.mouse.getX()"

    what are you

    what
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Funny Funny x 5 (list)

  3. Post #3
    Gold Member

    March 2005
    3,028 Posts
    That's not the idiomatic way to handle classes in Lua. Look up metatables.
    Reply With Quote Edit / Delete Reply Windows 7 Show Events Agree Agree x 1 (list)

  4. Post #4
    Gold Member
    BlkDucky's Avatar
    May 2008
    6,477 Posts
    surely that doesn't work?

    edit: automerge etc.

  5. Post #5
    Gold Member

    March 2005
    3,028 Posts
    In fact, your code doesn't even make sense. Try this:

    Code:
    Bullet_mt = {}
    Bullet_mt.getX = function(self)
      return self.x
    end
    Bullet_mt.getY = function(self)
      return self.y
    end
    Bullet_mt.setX = function(self, x)
      self.x = x
    end
    Bullet_mt.setY = function(self, y)
      self.y = y
    end
    
    function NewBullet(x, y)
      local self = {}
      setmetatable(self, Bullet_mt)
    
      self.x = x or love.mouse.getX()
      self.y = y or love.mouse.getY()
    
      return self
    end
    Then you can use this syntactic sugar to invoke the functions:

    Code:
    local bullet = NewBullet(0, 0)
    -- x:y(z) is the same as y(x, z)
    bullet:setX(100)
    print bullet:getX()

  6. Post #6
    thetree's Avatar
    February 2010
    41 Posts
    "love.mouse.love.mouse.getX()"

    what are you

    what
    Sorry, I started writing it out manually, then copy pasted it in forgetting to remove the first love.mouse. Its not like that in the code.

    In fact, your code doesn't even make sense. Try this:

    Code:
    Bullet_mt = {}
    Bullet_mt.getX = function(self)
      return self.x
    end
    Bullet_mt.getY = function(self)
      return self.y
    end
    Bullet_mt.setX = function(self, x)
      self.x = x
    end
    Bullet_mt.setY = function(self, y)
      self.y = y
    end
    
    function NewBullet(x, y)
      local self = {}
      setmetatable(self, Bullet_mt)
    
      self.x = x or love.mouse.getX()
      self.y = y or love.mouse.getY()
    
      return self
    end
    Then you can use this syntactic sugar to invoke the functions:

    Code:
    local bullet = NewBullet(0, 0)
    -- x:y(z) is the same as y(x, z)
    bullet:setX(100)
    print bullet:getX()
    Ok, thanks. But I still don't get how I'd go about using multiple bullets.

  7. Post #7
    Gold Member

    March 2005
    3,028 Posts
    Ok, thanks. But I still don't get how I'd go about using multiple bullets.
    You're using them correctly, but it didn't work before because the class itself was broken.
    Reply With Quote Edit / Delete Reply Windows 7 Show Events Friendly Friendly x 1 (list)

  8. Post #8
    thetree's Avatar
    February 2010
    41 Posts
    You're using them correctly, but it didn't work before because the class itself was broken.
    Excellent, thank you very much, everything works now :)