Page 1 of 1

How can I do an action when a light or dark spell is cast?

Posted: Tue Oct 30, 2012 1:56 pm
by Komag
How can I set it up to do a separate action whenever a party casts a light spell or a dark spell?

I want to trigger something else, say, a door opening, if the party casts a light spell. I found this info:
onCastSpell: a hook which is called when a champion casts a spell. The function gets two parameters, the casting champion and the name of the spell. If the function returns false, the casting is cancelled.
but I have no idea how to set it up or if this is what I need to use to do what I'm talking about. I have no experience working with hooks, sorry! :?

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 2:17 pm
by Komag
I tried putting this in my init.lua:

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, light)
       print("you cast light")
  end,
}
It sorta works, but it prints "you cast light" no matter what spell I cast!

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 2:27 pm
by Komag
Okay, some minor progress, but also a step back - if I put this in I get some results:

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, thespell)
       print("you cast "..thespell)
  end
}
now it will print accurately whatever spell I cast. But I want to do a certain action only when "light" is cast (and a different action for when "darkness" is cast)

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 3:28 pm
by Grimwold
Looks like you're almost there.. This should hopefully work, but is untested.

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, thespell)
       if thespell == "light" then
         hudPrint("you cast" .. thespell)
         return true
       elseif thespell == "darkness" then
         hudPrint("you cast" .. thespell)
         return true
       else
         return true
       end
  end
}
the effect should be.. hudPrint "you cast light" if you did cast light, or hudPrint "you cast darkness" otherwise nothing happens if you cast anything else.
I added return true.. to each state, in case you want to change them to return false (i.e. spell fails). otherwise you can remove all return values and put return true before the final end.
Komag wrote:Okay, some minor progress, but also a step back - if I put this in I get some results:

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, thespell)
       print("you cast "..thespell)
  end
}
now it will print accurately whatever spell I cast. But I want to do a certain action only when "light" is cast (and a different action for when "darkness" is cast)

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 5:53 pm
by Komag
YES, it works great! Now I can do "whatever I want" upon casting those spells! You are awesome, truly :D

...now I'm trying to be able to put most of the code in the dungeon - how can I make something like this work without crashing with an "attempt to call global "castLight" (a nil value)" error crashing the editor?

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, thespell)
    if thespell == "light" then
       castLight()
      elseif thespell == "darkness" then
       castDarkness()
    end
    return true
  end
}
I put functions in a script entity in the dungeon:

Code: Select all

function castLight()
  print("you cast light")
end
function castDarkness()
  print("you cast darkness")
end

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 5:58 pm
by Komag
I got it working! I added the name of the script entity before the function, like so:

Code: Select all

cloneObject{
   name = "party",
   baseObject = "party",

  onCastSpell = function(self, thespell)
    if thespell == "light" then
       tombScript.castLight()
      elseif thespell == "darkness" then
       tombScript.castDarkness()
    end
    return true
  end
}

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 6:51 pm
by Grimwold
Yeah! Glad you got it working!

I've found it's always best to put the code in a script entity, as the editor crashes very easily if you mess up the hook code in the lua files.

Re: How can I do an action when a light or dark spell is cas

Posted: Tue Oct 30, 2012 7:04 pm
by Komag
agreed, and it's easier to deal with I think