Im having an issue figuring out how call "self" within a function

I have a single function that I want to apply to many objects.

local function targetEnemy(event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self end end

I have the event listeners to see if the objects have a touch event

enemy1:addEventListener( "touch", targetEnemy ) enemy2:addEventListener( "touch", targetEnemy )

When I use the code as it is above it will run without error, but it obviously does not change the activeChar.target to self, it remains nil

when I edit the function to the following by inserting “self”

local function targetEnemy(self, event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self print("hello") end end

I then run the program and I get the error

Attempt to index local ‘event’ (a nil value)

File: main.lua

Line: 146

I find it strange that the issue seems to be with “event” seeing as it works prior without “self” included.

Can someone help with the logic that I am missing behind this?

This tutorial might be of use:  https://coronalabs.com/blog/2015/12/01/tutorial-understanding-the-colon-vs-dot-operator/

Rob

thanks for the link, I have read through the article and I seem to not be understanding how that article would help in my situation.

from what I gather, that tutorial really only benefits when using colon or dot operator when I call the function in a single instance. I don’t see how that translates when I have multiple event listeners watching different objects and waiting to call the same function.

from what I gather, using that tutorial, I would have to create a new function for each individual object.

a little more assistance would be appreciated.

I see two ways to deal with this situation:

  1. you want to use “generic” listener functions (not tied to objects)

In this case there is no “self” and you can test for event.target (the object receiving the touch event)

local function targetEnemy(event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = event.target -- use event.target instead of self end end enemy1:addEventListener( "touch", targetEnemy )

or

  1. you want the listener to know who “self” is

In this case you can assign the “touch” function as a member of the object

local function targetTouch(self, event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self print("hello") end end -- assign a reference to the function and name it "touch" enemy1.touch = targetTouch -- no need to specify the function reference here if the event receiver has a function with the same name as the event. enemy1:addEventListener( "touch" )

Now, I don’t know exactly the inner mechanics of Corona but I believe that

in case 1) the event will be propagated as a function call like this

touch(event) 

where the receiver of the touch event will be referenced in event.target

in case 2) the event will be called like

enemy1:touch(event)

which is equivalent (Lua authors call it “sintactic sugar”) to this call

enemy1.touch(enemy1, event)

This way “self” in the touch handler will be the same object that is handling the touch event

Hope this helps

Thank you very much sciacchitano for clearing my issue up for me :slight_smile:

You’re welcome. :wink:

This tutorial might be of use:  https://coronalabs.com/blog/2015/12/01/tutorial-understanding-the-colon-vs-dot-operator/

Rob

thanks for the link, I have read through the article and I seem to not be understanding how that article would help in my situation.

from what I gather, that tutorial really only benefits when using colon or dot operator when I call the function in a single instance. I don’t see how that translates when I have multiple event listeners watching different objects and waiting to call the same function.

from what I gather, using that tutorial, I would have to create a new function for each individual object.

a little more assistance would be appreciated.

I see two ways to deal with this situation:

  1. you want to use “generic” listener functions (not tied to objects)

In this case there is no “self” and you can test for event.target (the object receiving the touch event)

local function targetEnemy(event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = event.target -- use event.target instead of self end end enemy1:addEventListener( "touch", targetEnemy )

or

  1. you want the listener to know who “self” is

In this case you can assign the “touch” function as a member of the object

local function targetTouch(self, event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self print("hello") end end -- assign a reference to the function and name it "touch" enemy1.touch = targetTouch -- no need to specify the function reference here if the event receiver has a function with the same name as the event. enemy1:addEventListener( "touch" )

Now, I don’t know exactly the inner mechanics of Corona but I believe that

in case 1) the event will be propagated as a function call like this

touch(event) 

where the receiver of the touch event will be referenced in event.target

in case 2) the event will be called like

enemy1:touch(event)

which is equivalent (Lua authors call it “sintactic sugar”) to this call

enemy1.touch(enemy1, event)

This way “self” in the touch handler will be the same object that is handling the touch event

Hope this helps

Thank you very much sciacchitano for clearing my issue up for me :slight_smile:

You’re welcome. :wink: