Help with spell system and client/server interaction

So I’ve got this gamemode of sorts that I’ve been working on for the past day or two, and have since finished most of the clientside code:

After starting some of the serverside coding I ran into a few problems with modularity; sure I could hard code all of the spells and their effects into a single file, but that would be terrible to work with and would be no fun for anyone planning on modding or adding stuff into the gamemode.

I want to make a folder of spells that are individual lua files, read by the main script at the serverside Initialize. They’d have some base variables like icon, name, tooltip, spellID but would all contain their own code for the spells themselves.

I have no problem coding a base with smaller children files, but I’ve simply got no idea how to set it up to be read as I described. I’d also like to be able to call it from LocalPlayer in the client in such a way as this: LocalPlayer():RunSpell(spellID, target). It would be a bit like the SWEP system, I suppose.

Any help would be amazing!

local SPELL

SPELL = { }
SPELL.__index = SPELL

AccessorFunc( SPELL, "m_sName", "Name", FORCE_STRING )
AccessorFunc( SPELL, "m_sDesc", "Description", FORCE_STRING )
AccessorFunc( SPELL, "m_iManaCost", "ManaCost", FORCE_NUMBER )
AccessorFunc( SPELL, "m_iReqLevel", "RequiredLevel", FORCE_NUMBER )


function SPELL:CanCast( pl )
	return pl:GetMana( ) >= self:GetManaCost( ) and pl:GetLevel( ) >= self:GetRequiredLevel( )

function SPELL:GetID( )
	return self.ID or SPELL_INVALID

function SPELL:Cast( pl )

--And so on

local _R = debug.getregistry( )

_R.Spell = SPELL

local k, v

GM.Spells = { }

local curindex = 1

for k, v in ipairs( file.Find( "<gamemode path>/spells/*.lua", LUA_PATH ) ) do
	local SPELL = setmetatable( { ID = curindex }, _R.Spell )
	include( "spells/" .. v )
	if SERVER then
		AddCSLuaFile( "spells/" .. v )
	table.insert( GM.Spells, SPELL )
        curindex = curindex + 1

That is the gist of it. Not tested but should point you in the right direction.

Neat. I’ll see what I can get working with this.

looks very nice! are you going to release it?

That’s the plan. If this works out then its all just menus and polish.

Forgive my noobishness, but what exactly does AccessorFunc do? Also, is this meant to be run from the gamemode or within the base spell file?

Got it. So that’s just there for the sake of neatness, I suppose.


snip It works! Thanks Kogitsune!