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

  • SEARCH
  • Home
  • 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 6033467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:29:14+00:00 2026-05-23T05:29:14+00:00

So I come from the traditional game development that uses OOP principles and from

  • 0

So I come from the traditional game development that uses OOP principles and from what I’ve seen you can mimic this using LUA once you know what you are doing. In some of the code postings I found out how you can use the director class and create files that have a new() function etc.

What I’m looking for is a way to manage my weapons. I have a player and an opponent and I would prefer to have one weapon class, say weaponCanon. What I have done is:

-- private vars here
local power
local canonSprite
local whatever

local someFunction = function()
...
end

-- Private stuff here
local weaponCanon = {}

weaponCanon.fire = function(atX, atY)
...
end

weaponCanon.reset = function()
...
end

return weaponCanon

Then in my level code I simply do:

local weaponCanon = require("weaponCanon")
weaponCanon.fire(100, 100)

This works great and allows me to use a “private” and “public” mentality when coding up my weapons. The problem is that if I want the player and opponent to have a canon:

local playerWeapon = require("weaponCanon")
local opponentWeapon = require("weaponCanon")

This simply returns the same object instead of a new instance to that object. So I only get one weaponCanon at the opponentWeapon location. This is obviously now what I want/need.

Our game has many weapons in it and it would be nice to only have one version of each file with a setting telling us if its the opponents weapon or players weapon. The alternative is to copy each file and create a weaponPlayerCanon and a weaponOpponentCanon but I cringe at the thought of modifications to one file and having to change 2+ files every time.

How can I make it return an instance and what is the structure of the LUA file to do so?

Thanks or any and all help

-d

  • 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-23T05:29:15+00:00Added an answer on May 23, 2026 at 5:29 am

    I guess you are trying to model a class with your source file. This means you should also have a function to create a new instance of that class unless you want them to share all their state.

    Something along the lines of (untested):

    local WeaponCannon = {}
    WeaponCannon.__index = WeaponCannon
    function WeaponCannon:new()
      return setmetatable({}, self)
    end
    
    function WeaponCannon:fire(x, y)
      -- Do something via the self reference and arguments (x, y)
    end
    
    return WeaponCannon
    

    And in your calling code (also untested):

    require('WeaponCannon')
    local playerWeapon = WeaponCannon:new()
    local opponentWeapon = WeaponCannon:new()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from more of an Android development background so apologies if this is
I come from a CVS background. I'm currently investigating using SVN for a project.
I come from the Java world, where you can hide variables and functions and
I come from a .net background so the empty classes (models) that I'm seeing
I come from a Java/C++ OOP background and am trying to get into JavaScript
I come from frontend background, so I apologize if this db question seems dumb.
I come from a windows w/ visual-studio background for native development and I am
This is a pretty generic question, but I come from a few years with
I come from a C++ background. In C++ I can store a memory adress
I come from blackberry support forum, and got to solve this soon. any tips

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.