Stuck with local variables
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Stuck with local variables
Yes. Nothing is ever copied in Lua unless you do it yourself, it's all object references.
Help us help you: attach a .love.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Stuck with local variables
Oh, I see. So while everything is local, requiring it in all relevant files still make it act like a global.
Re: Stuck with local variables
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:
And register the bullet with a collision handler to detect enemies:
Now the bullet has collision functions to check if it collides with stuff, and to handle that collision:
Now this probably looks more complicated, and it is. But there's also advantages... for example, if you do this:
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).
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
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
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
Code: Select all
-- spawning a new bullet but passing an enemy as the owner
spawnBullet( enemy )
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).
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Stuck with local variables
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.
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.
Re: Stuck with local variables
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.
Who is online
Users browsing this forum: Google [Bot] and 3 guests