The class system that comes bundled with vx allows simple object orientation in Lua. Pretty much all the main classes in vx are written with it. It is not essential to understand every detail about it, but it certainly helps.
If you want to create your own classes in your game, you can do so by using a special global function:
vergeclass(name_string [, parent_table]) -> table
vergeclass "name" -> table
vergeclass "name"(parent_table) -> table
vx.Object).
Here is an example of a simple Hero class which uses vx.
-- Declaring the hero class.
vergeclass "Hero" do
-- The init routine, creates a hero with 5 hp.
function Hero:__init()
self.hp = 5
end
end
The above code declares a class called "Hero". It then adds a special method called __init() which handles the setup when creating a new Hero.
Now that we've declared this class, and given it the class an init function, we can create instances of it:
hero = Hero()
Great!
If we wanted to check how much hp a particular hero had, we would do the following:
print(hero.hp)
To change the HP, we can simply set the variable.
-- Now you only have two hp. hero.hp = 2
Let's say we want to add a method to change our hero, which allows him to get more hp. We could write one like this:
-- Declaring the hero class.
vergeclass "Hero" do
-- The init routine, creates a hero with 5 hp.
function Hero:__init()
self.hp = 5
end
-- The hero gets more 4 HP
function Hero:Heal()
self.hp = self + 2
end
end
To use the method we defined on the hero class, we would go:
hero:Heal()
If we wanted to extend this hero class, we can do by writing this:
-- A magic hero is like a hero, except he gets magic!
vergeclass "MagicHero"(Hero) do
function MagicHero:__init()
-- This function MUST be called inside of your inheriting class's init so it knows how to properly construct things.
super()
-- Magic heroes have less hp, because they spent time learning magic instead of exercise.
self.hp = 2
-- Magic heroes have some mp.
self.mp = 2
end
-- Use one mp to cast a spell.
function MagicHero:Cast(spell_name)
print("Casting " .. spell_name)
self.mp = self.mp - 1
end
end
Fun. I don't really want to write any more right now. Tell me if this stuff needs more clarification.