Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:20:41+00:00 2026-05-23T11:20:41+00:00

I was following this tutorial http://www.crawlspacegames.com/blog/inheritance-in-lua/ and created 2 objects (drums and Guitar) that

  • 0

I was following this tutorial http://www.crawlspacegames.com/blog/inheritance-in-lua/ and created 2 objects (drums and Guitar) that inherit from MusicalInstrument. Everything worked fine until I added timer functions, then for some reason only 1 from the 2 objects that inherit from MusicalInstrument gets called

MusicalInstrument.lua:

module(...,package.seeall)

MusicalInstrument.type="undefined"

local function listener()
print("timer action: "..MusicalInstrument.type)
end

function MusicalInstrument:play(tpe)
    MusicalInstrument.type = tpe;
    print("play called by: "..MusicalInstrument.type)
    timer.performWithDelay(500,listener,3)
end

function MusicalInstrument:new( o )
    x = x or {} -- can be parameterized, defaults to a new table
    setmetatable(x, self)
    self.__index = self
    return x
end

Guitar.lua

module(...,package.seeall)
require("MusicalInstrument")

gtr = {}

setmetatable(gtr, {__index = MusicalInstrument:new()})

return gtr

Drums.lua

module(...,package.seeall)
require("MusicalInstrument")

drms = {}

setmetatable(drms, {__index = MusicalInstrument:new()})

return drms

main.lua

--    CLEAR TERMINAL    --
os.execute('clear')
print( "clear" ) 
--------------------------


local drms=require("Drums")

drms:play("Drums")

local gtr=require("Guitar")

gtr:play("Guitar")

This is the terminal output:

clear
play called by: Drums
play called by: Guitar
timer action: Guitar
timer action: Guitar
timer action: Guitar
timer action: Guitar
timer action: Guitar
timer action: Guitar

I excepted the output to have 3 guitar time calls and 3 drums timer calls

Any ideas on how to make this work will be appreciated much!!

Thanks

—————————– Edited after another try ——————-

The following change in MusicalInstrument

module(...,package.seeall)

MusicalInstrument.type="undefined"

function MusicalInstrument:listener()
print("timer action: "..MusicalInstrument.type)
end

function MusicalInstrument:play(tpe)
    MusicalInstrument.type = tpe;
    print("play called by: "..MusicalInstrument.type)
    timer.performWithDelay(500,MusicalInstrument:listener(),3)
end

function MusicalInstrument:new( o )
    x = x or {} -- can be parameterized, defaults to a new table
    setmetatable(x, self)
    self.__index = self
    return x
end

Resulted with the following output :

clear
play called by: Drums
timer action: Drums
play called by: Guitar
timer action: Guitar

The correct instruments were called by the timer but only once

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:20:42+00:00Added an answer on May 23, 2026 at 11:20 am

    In both listener and MusicalInstrument:play() you write and read the same variable for both instances.

    You actually want to set the instrument type per instance here. Lua is not exactly my primary language, but e.g. something like:

    function MusicalInstrument:listener()
        print("timer action: "..self.type)
    end
    
    function MusicalInstrument:play(tpe)
        self.type = tpe;
        local f = function() self:listener() end
        timer.performWithDelay(500, f, 3)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://www.codinghorror.com/blog/2008/04/setting-up-subversion-on-windows.html I am following this as a guide to install subversion. The tutorial at
I've been following this tutorial on how to create an OData service. http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx And
I have created a dataset with the C# designer following this tutorial http://www.devx.com/dotnet/Article/28678/1954 ;
I was following this tutorial on http://www.youtube.com/watch?v=LBnPfAtswgw and was able to replicate that in
I'm following this tutorial: http://www.javascripttoolbox.com/jquery/ And I'm trying to get table rows that are
I was following this tutorial: http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html And checking out the demo here: http://qpoit.com/marcofolio_demo/apple_search/ I
I am following this tutorial: http://www.csharp-station.com/Tutorials/Lesson01.aspx I pasted this into a text file, named
Following this tutorial: http://www.vogella.de/articles/AndroidServices/article.html#receiver I created my own project. Manifest: <?xml version=1.0 encoding=utf-8?> <manifest
I am following this tutorial http://www.codeproject.com/KB/cpp/authforwebservices.aspx They have this in the tutorial [SoapHeader(Authentication, Required
I'm following this tutorial on Flash Pro CS4: http://www.baycongroup.com/flashCS4/09_flashCS4.html I have a button. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.