Is there a way to make the player roll a normal spell via trigger?

Midlou

L5: Dapper Member
Jan 12, 2016
222
254
I know it's possible to roll a rare spell using a trigger output, but there's no option for a normal spell
 
Solution
Good news!
With VScript it's possibile to roll any spell!
(or rather... fake a roll and give an arbitrary spell).

C:
// Enable spells
SetUsingSpells(true)

// Get the player's spellbook if it's equipped
::GetSpellbook <- function(player) {
    for (local child = player.FirstMoveChild(); child != null; child = child.NextMovePeer()) {
        if (child.GetClassname() == "tf_weapon_spellbook") return child
    }
    return null
}

// Set the spellbook's current spell and amount of charges
::SetSpell <- function(spellbook, id, amt) {
    NetProps.SetPropInt(spellbook, "m_iSelectedSpellIndex", id)
    NetProps.SetPropInt(spellbook, "m_iSpellCharges", amt)
}

// Roll a random spell
::RollSpell <- function(player, spells =...

Box Of Paper

L3: Member
Jul 15, 2019
121
148
Good news!
With VScript it's possibile to roll any spell!
(or rather... fake a roll and give an arbitrary spell).

C:
// Enable spells
SetUsingSpells(true)

// Get the player's spellbook if it's equipped
::GetSpellbook <- function(player) {
    for (local child = player.FirstMoveChild(); child != null; child = child.NextMovePeer()) {
        if (child.GetClassname() == "tf_weapon_spellbook") return child
    }
    return null
}

// Set the spellbook's current spell and amount of charges
::SetSpell <- function(spellbook, id, amt) {
    NetProps.SetPropInt(spellbook, "m_iSelectedSpellIndex", id)
    NetProps.SetPropInt(spellbook, "m_iSpellCharges", amt)
}

// Roll a random spell
::RollSpell <- function(player, spells = [[0,2],[1,2],[2,1],[3,1],[4,2],[5,1]], delay = 1) {
    local spellbook = ::GetSpellbook(player)
    if (!spellbook) return
    local spell = spells[RandomInt(0, spells.len()-1)]
    ::SetSpell(spellbook, -2, 0)
    EntFireByHandle(spellbook, "RunScriptCode", "::SetSpell(self,"+spell[0]+","+spell[1]+")", delay, null, null)
}

// Clear spellbooks on round start (otherwise the rolling animation may get stuck)
for (local spellbook; spellbook = Entities.FindByClassname(spellbook, "tf_weapon_spellbook");) {
    ::SetSpell(spellbook, -1, 0)
}

Once the script has run, you can target a player and input RunScriptCode ::RollSpell(self, [[0,4],[1,3]], 2).

The example grants the player either 4 fireballs or 3 bat spells after a rolling animation of 2 seconds.
If you omit the spells argument then it defaults to the common spells; if you omit the delay then the rolling animation defaults to 1 second duration.
So instead of RollRareSpell "" you RunScriptCode "::RollSpell(self)"


If you don't need the rolling animation then you can RunScriptCode ::RollSpell(self, [[0,2]], 0) or RunScriptCode ::SetSpell(::GetSpellbook(self), 0, 2)

The example gives two fireballs to the player.


Code:
~ m_iSelectedSpellIndex (charges amt) ~
-2 - rolling animation (?)
-1 - no spell (...)
0 - fireball (2)
1 - bats (2)
2 - uber (1)
3 - pumkins (1)
4 - jump (2)
5 - invis (1)
6 - tele (2)
7 - energy-ball (1)
8 - mini (1)
9 - meteors (1)
10 - eyeball boss (1)
11 - skeleton army (1)
12 - kart punch
13 - kart parachute
14 - kart invul
15 - kart bomb
 
Last edited:
Solution