Stuck with local variables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Stuck with local variables

Post by Robin »

Yes. Nothing is ever copied in Lua unless you do it yourself, it's all object references.
Help us help you: attach a .love.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Stuck with local variables

Post by DaedalusYoung »

Oh, I see. So while everything is local, requiring it in all relevant files still make it act like a global.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Stuck with local variables

Post by Plu »

Basically, except you are now explicitly copying in a value from the global playing field instead of silently assuming it has to be there.

I personally think that including files like that is also not the best solution. However, the stuff below might be a little advanced.. if you don't understand feel free to either ask or ignore it for the time being. ;)

That said, you could also pass the player to the bullet when you create it:

Code: Select all

function spawnBullet( player )
  bullet = {
    owner = player
    -- behaviour code and the like also goes here
  }
end
And register the bullet with a collision handler to detect enemies:

Code: Select all

-- this one is semi-global because it's a very high-level object
local collision = require 'collision'
collision.register( bullet )

function collision.register( obj )
  table.insert( self.registered, obj )
end

-- every frame, you call this function to update the collision handler and handle the collisions
collision.update = function()
  -- handle collisions by iterating over all known objects and checking them for collisions with each other object... left as an excercise to the reader

  -- assume we have a current object we're checking collision with, and a target that we know overlaps the current object
  if( current.collidesWith( target ) then
    current.handleCollisionWith( target )
  end
end
Now the bullet has collision functions to check if it collides with stuff, and to handle that collision:

Code: Select all

function bullet.collidesWith( target )
  -- if you want the bullet to ignore the collision, you can return false. for example, indestructible enemies could have this property set
  if not target.hittable then
    return false
  end

  -- remember how we set the player when we created the bullet? I explain why we're checking against factionID below.
  if self.owner.factionID ~= target.factionID then
    return true
  end
end

function bullet.handleCollisionWith( target )
  -- whatever you like... lets just reduce HP or something
  target.hp = target.hp - 1
  -- you could also use a function like target.takeDamage( 1 ), that would let you handle death logic, impact animations, etc on the target.
end
Now this probably looks more complicated, and it is. But there's also advantages... for example, if you do this:

Code: Select all

-- spawning a new bullet but passing an enemy as the owner
spawnBullet( enemy )
it will work with 0 extra added lines of code, because this bullet now has an enemy as its owner, will only collide with objects that belong to factions other than that enemy (ie; the player) and will reduce their HP on impact just like it did to enemies when we assigned the player as owner.

And hey; if you register wall sections as objects with a factionID, then these bullets will also trigger on hitting those walls... if the walls react to losing HP then you could destroy them as well. (again; adding 0 lines of code for the new collision type).

The above is still a bit sloppy, but I hope it at least shows you the power of using more localised variables that you pass specifically to objects over constantly referring to globals (or semi-globals by constantly including the same file).
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Stuck with local variables

Post by DaedalusYoung »

Well, I can understand that, but where do you define the function spawnBullet()? Shouldn't it be part of the bullet module, so it's bullet.spawn() instead? How else can you call that function so you can pass any argument you like?

I will have a go at creating some simple, small game using these techniques in the new year, I know that should help me understand it more.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Stuck with local variables

Post by Plu »

Yes in a final version you'd want create to be a part of the bullet module. It's just a simple example so some parts might be a little rough. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests