Spell Casting System

I’ve been really stuck on this for awhile and how to best optimize a spell system I’ve been fixing up and changing that another person had shown me.

Basically for the past month or so I’ve been trying to work on a type of spell casting addon/gamemode that gives the player
spells based on what class they are, which is actually another thing I’ve been thinking of redoing also.


--clientside
SelectTargetFunc()


--serverside
function CastSpell( ply, target, spell )
        return Spells[spell].OnCast( ply, target )
end


--shared
Spells = {};
Spells["spellname"] = {
       DisplayName = "namehere",
       OnCast = function() blah end,
       ect..
}

For the clientside function I call it every time the player presses TAB or when the scoreboardHide hook is called, to trace from the player’s shootpos to get an entity,
and if it hits the world the target is the player himself. Then it sends the entity to the server to use it as the target argument in the CastSpell function.
From there it uses the player that cast it, the target, and whatever spell I want to cast and uses the OnCast function to deal damage, display effects ect.

Is the above a good way of handling all this? Some things I didn’t put in the CastSpell function above that I have on my server are things like getting a trace for
line of sight, range, cooldown prediction and other conditions set by the spell itself(like making it so the player cannot target himself with a damaging spell)

I feel like a lot of this is messy and could be done better, and as of now the only way for me to use effects, doing channeling for spells(basically where you have to
stand still to cast continue to cast the spell) and other suchs things are within the OnCast function within the spell’s table.

Its missing a lot and I’m not sure where else to go with all this. Any tips or suggestions for where I could take this?
As a last note, I’m doing it this way in particular(instead of using sweps) because I’d like for the player to press the 1-9 keys on their keyboard for spells, kinda like
in World of Warcraft, and for the player to be able to interchange the spells they use for each key.

Your idea is very interesting and appealing to me, and for that I rate your thread a winner. However, this doesn’t look like a system, but instead bits and pieces of what can be used for a system. Unless you have some working content already I’d approach this differently. My friend once tried to make a system you described and he is very good at GLua. He had a VERY tough time, and couldn’t get certain errors to be solved. Would you consider using sweps and forcing the user to switch and fire automatically when a key is pressed? Just a wild sketch I thought off the top of my head…

your on the right track, you just need to make your core systems more sophisticated to support channeling/buffs etc by default

and make a bunch of easy helper functions for common crap such as fire projectile, create aoe aura etc

@pogh10
Yeah, I guess system isn’t really the correct word for this lol, I guess more like a small addon or something of the likes.
I’ve actually been thinking of switching to sweps, I guess I just didn’t like the idea of using the primary fire to use one spell at a time. I thought it would be interesting to be able to do things like having some
spells on and off a global cooldown, so then you could do something like use a quick buff that only lasts
a few seconds, then at the same time begin casting a firebolt of some kind that deals extra burst to
a target, thus creating these bursts cooldowns you see in some MMOs if that makes sense.

@legendofrobbo
Recently I’ve been trying to split up the cast function, so instead of just CastSpell(); it will be
BeginCast( name ), which then goes to find out if the target is valid, in range, and the spell is off
cooldown and if the spell needs to be channeled or is AOE, then it will go into a function like
BeginChannel( name ) where it begins the channeling(Kinda like Life Drain from WoW) where if you
move it cancels, then finally it actually casts the spell with EndCast( name ) where it does the
damage/effects/ect.

When I get home from work today, I’ll try to update the OP and show a little bit more of what I’m working on and how its working so far with some images

Remember. Gmod Entities very shit -_-

I working to gmod league of legend mods
this addon include systems:

Abilities - sub abilities included
Heroes - (Lee sin, Yasuo(in progress))
Projectiles - new realtime intersection(capsule, sphere, obb…)
Buffs
Move States
Cooldown box hud

if you interesting this addons (developer)? please add me

You should be using metatables. Each spell should define what type of spell it is, its cooldown, the conditions for it to be able to target someone/something, what it does to its targets, etc.

Once you have a nice framework for metatables, it’ll be a lot easier to continue development.

I made a system like this ages ago, it was quite nice in my opinion but I can’t share it with you as I lost the files when I upgraded (forgot to back it up)

The table format was like this:



Spells = {
   ability1 = {
       cooldown = 15,
       func = function()
            print("blah")
       end,
   }
}


and I could execute it like:



function CastSpell(name, ply)
    if CurrentCD >= name.cooldown then
         name.func() 
    end
end
CastSpell(table, player)


With CurrentCD being a variable saved for that ability spell.
It was something like that anyway. Definitely agree with what man with hat said above.

Sorry for the late reply, I’ve been looking up a lot about metatables, metamethods and object oriented
based lua. I was almost thinking of making objects out of spells, kind of like how sweps/entities are objects.
But after that point I’d have no clue where to go from there. I may just continue to expand on what I had before.
Creating a serverside function that calls the function of a spell from its shared table.

completely disregard anything i said

Huh? I saw what you had said, I basically have the same thing set up. May I ask how you had your
client handle casting functions? There was a function that Acecool had shown me that allowed me to call functions by pressing the 1 - 9 keys.

The keys would then send a net message to the server with the target entity info, then allowed the
OnCast function to be cast on the target. Idk if I’m explaining this very well or not.

Just using the net system would work well, or even a simple console command.



if SERVER then
util.AddNetworkString("_castspell")
    function CallSpell(ply, name)
      if ListOfSpells[name] then
         if ply.CoolDown >= Spells[name].cooldown then 
            Spells[name].func()
         end 
      end 
    end 
elseif CLIENT then
    function SendCast(name)
       net.Start("_castspell")
       net.WriteString(name)
       net.SendToServer()
     end
     SendCast("LeeSin1")
end 


try something around that