Page 1 of 1

onAttack problem

Posted: Sun Jan 06, 2013 6:06 am
by Leki
Hi, I have this script:

Code: Select all

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

   onAttack = function(champion, weapon)
      -- check if it is the ice_sword
      if weapon.name == "ice_sword" then
         print ("hello world")
      end
   end,
}
the probelem is, that game crashes if champion attacks with empty hand.
Empty hand returns nil and nil.name --> crash, I guess.

I solve this with one more if:

Code: Select all

   onAttack = function(champion, weapon)
      if weapon == nil then
           print("empty hand")
           return
      else
           -- check if it is the ice_sword
           if weapon.name == "ice_sword" then
               print ("hello world")
           end
      end
   end,
is this right solution or I am doing somthing wrong?

Re: onAttack problem

Posted: Sun Jan 06, 2013 7:49 am
by dasarby
That seems right to me. Because the character is not guaranteed to have a weapon, you need to check if there is a weapon before checking the weapon name.

If all you care about is the ice sword, you can do something like:

Code: Select all

   onAttack = function(champion, weapon)
      if weapon ~= nil and weapon.name == "ice_sword" then
           print ("hello world")
      end
   end
}
This will check that the weapon is not nil (~=) and then check if it is the ice sword.

Re: onAttack problem

Posted: Sun Jan 06, 2013 11:20 am
by Leki
OK then. Thanks m8 :)

Re: onAttack problem

Posted: Sun Jan 06, 2013 2:37 pm
by Komag
you could always pass it all to a function in your dungeon instead of in that lua file, so you have more info with crashes and also won't crash totally, just stop preview.

Code: Select all

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

   onAttack = function(champion, weapon)
      attackScript.doAttack(champion, weapon)
   end,
}
then in the dungeon have a script entity called attackScript and do your code in the function doAttack

Re: onAttack problem

Posted: Sun Jan 06, 2013 7:27 pm
by Leki
Komag wrote:you could always pass it all to a function in your dungeon instead of in that lua file, so you have more info with crashes and also won't crash totally, just stop preview.

Code: Select all

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

   onAttack = function(champion, weapon)
      attackScript.doAttack(champion, weapon)
   end,
}
then in the dungeon have a script entity called attackScript and do your code in the function doAttack
...but in that way I cannot public sources as a independent plugin I guess --> code must be part of "mod_objects.lua" --> I have no idea what method is better. Full crashes or moving "final" scripts to the external lua file :?

Re: onAttack problem

Posted: Sun Jan 06, 2013 11:52 pm
by Diarmuid
Just a quick tip, I recently discovered that in lua non-nil values return true, so you can write:

Code: Select all

if weapon and weapon == "sword" then
instead of

Code: Select all

if weapon ~= nil and weapon == "sword" then
cleans up code. :)

Re: onAttack problem

Posted: Mon Jan 07, 2013 12:23 am
by Komag
people can just copy paste the text, doesn't have to be actual files they copy/install