Note that there are some explanatory texts on larger screens.

plurals
  1. POCorona LUA and OOP Design
    primarykey
    data
    text
    <p>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. </p> <p>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:</p> <pre><code>-- 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 </code></pre> <p>Then in my level code I simply do:</p> <pre><code>local weaponCanon = require("weaponCanon") weaponCanon.fire(100, 100) </code></pre> <p>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:</p> <pre><code>local playerWeapon = require("weaponCanon") local opponentWeapon = require("weaponCanon") </code></pre> <p>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.</p> <p>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.</p> <p>How can I make it return an instance and what is the structure of the LUA file to do so?</p> <p>Thanks or any and all help</p> <p>-d</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload