• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved How to make wand loop trough all damage types in lua

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,454
Solutions
1
Reaction score
627
Location
Estonia
Im not good with Scripting and i'm trying to make:
onUseWeapon to shuffel trough all the listed damage types and then do 1 attack with each attack type.
(this all also should happen in 1 attack)

EDIT: Fixed code. This script will do 5 different elemental damages in singel hit
LUA:
local combat, types = {}, {COMBAT_FIREDAMAGE, COMBAT_EARTHDAMAGE, COMBAT_ICEDAMAGE, COMBAT_ENERGYDAMAGE, COMBAT_DEATHDAMAGE}
for x = 1, #types do
  combat[x] = Combat()
  combat[x]:setParameter(COMBAT_PARAM_TYPE, types[x])
  combat[x]:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_FIREATTACK)
  combat[x]:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_FIRE)

  function onGetFormulaValues(cid, level, maglevel)
  min = -10
  max = -10
  return min, max
  end
  combat[x]:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
end

function onUseWeapon(player, var)
  for x = 1, #types do
  addEvent(function()  combat[x]:execute(player, var) end, x * 100)
  end
  return true
end
 
Last edited:
Im not good with Scripting and i'm trying to make:
onUseWeapon to shuffel trough all the listed damage types and then do 1 attack with each attack type.
(this all also should happen in 1 attack)

Below i brought out the code i came up with, but it does not take the parameters from the loop.
What im doing wrong? Can someone help me to fix this code?

PHP:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_FIREATTACK)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_FIRE)
combat:setParameter(attacktype)


local parameters = {"COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE",
"COMBAT_PARAM_TYPE, COMBAT_EARTHDAMAGE",
"COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE",
"COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE",
"COMBAT_PARAM_TYPE, COMBAT_DEATHDAMAGE"}


function onGetFormulaValues(cid, level, maglevel)
    min = -(10)
    max = -(10)
    return min, max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onUseWeapon(player, var)
    for attacktype = 1, #parameters do
    combat:execute(player, var)
    end
    return 1
  
end
Maybe do what you did for the second line but for earth, ice, energy and death also
 
i don't understand?
I don't need animation effects at all i just wanted to see if some part of the script works
 
Code:
local combat, types = {}, {COMBAT_FIREDAMAGE, COMBAT_EARTHDAMAGE, COMBAT_ICEDAMAGE, COMBAT_ENERGYDAMAGE, COMBAT_DEATHDAMAGE}
for x = 1, #types do
     combat[x] = Combat()
     combat[x]:setParameter(COMBAT_PARAM_TYPE, types[x])
     combat[x]:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_FIREATTACK)
     combat[x]:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_FIRE)

     function onGetFormulaValues(cid, level, maglevel)
         min = -10
         max = -10
         return min, max
     end
     combat[x]:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
end

function onUseWeapon(player, var)
     return combat[math.random(#types)]:execute(player, var)
end
 
@Limos
Almost works like i want.
How do i remove math.random?
I want the onUseWeapon to execute all the weapon types not 1 of them.
if i simply remove it then it doesn't again do any damage.
 
Code:
local combat, types = {}, {COMBAT_FIREDAMAGE, COMBAT_EARTHDAMAGE, COMBAT_ICEDAMAGE, COMBAT_ENERGYDAMAGE, COMBAT_DEATHDAMAGE}
for x = 1, #types do
     combat[x] = Combat()
     combat[x]:setParameter(COMBAT_PARAM_TYPE, types[x])
     combat[x]:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_FIREATTACK)
     combat[x]:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_FIRE)

     function onGetFormulaValues(cid, level, maglevel)
         min = -10
         max = -10
         return min, max
     end
     combat[x]:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
end

function onUseWeapon(player, var)
     for x = 1, #types do
         addEvent(function() combat[x]:execute(player, var) end, x * 100)
     end
     return true
end
 
Amazing, it works
I figured i gotta do loop again but i would have never guessed i would have had to include some addEvent line
This is something new too for me and not entirely understanding it:
addEvent - stacks some function/method?
function() - a function with no parameters?
combat[x]: '...' - x shows the for loop count
end - ends the function()
x*100 - uhm.. ?? wild guess it has something to do with addEvent intervals? (because i guess Events need some kind of time schedule)
end - ends the loop
return true - releases all the addEvents?
 
The function addEvent is a timer function, it executes an other function after a certain time.
http://otland.net/threads/how-to-using-addevent.225292/

You can write function() without any name or parameters in addEvent since it's not being called, but added directly in addEvent, you can learn more about this in the addEvent tutorial.

In this script combat is a table and there are 5 combats added in the table.
combat[1] will be the first combat, combat[2] the second, etc.
The for loop repeats all codes inside it, the x stands for the numbers, first time it repeats it x means 1, second time 2, third time 3, etc.
So if you do this.
Code:
for x = 1, #types do
      addEvent(function() combat[x]:execute(player, var) end, x * 100)
end
It's the same thing as doing this.
Code:
addEvent(function() combat[1]:execute(player, var) end, 1 * 100)
addEvent(function() combat[2]:execute(player, var) end, 2 * 100)
addEvent(function() combat[3]:execute(player, var) end, 3 * 100)
addEvent(function() combat[4]:execute(player, var) end, 4 * 100)
addEvent(function() combat[5]:execute(player, var) end, 5 * 100)
But doing this with a loop is faster and it makes the script smaller.

The end is indeed to close the function, all functions need to be closed with end.
The x * 100 is the time in milliseconds, so there is 100 milliseconds between every combat.
The end under that is indeed to close the for loop, since all for loops need to close with end.
return true is to make the function return true, using return like "return combat:execute(player, var)" what you often see in weapons/spells works the same way.
 
Thank you both for your replies! I'm using TFS 1.2 and if it's alright I'll post everything I've changed and you ca tell me where i'm wrong?

Weapons.xml
Code:
<wand id="2184" level="1" mana="10" min="150" max="300" type="death"> <!-- Crystal Wand -->


items.xml
Code:
<item id="2184" article="a" name="crystal wand">
        <attribute key="weaponType" value="wand" />
       <attribute key="shootType" value="death" />
        <attribute key="shootType" value="smallice" />
        <attribute key="shootType" value="fire" />
        <attribute key="shootType" value="energy" />
        <attribute key="shootType" value="smallearth" />
        <attribute key="range" value="6" />
        <attribute key="weight" value="2800" />
my lua file is the same as yours and its in weapons/scripts :)

Thanks for all the help!

I've also got it set into movements.xml for the equip/deEquip
 
Last edited by a moderator:
remove all the shhotypes in items.xml..

and you havent even registarted script in weapons.xml

this is gow i have it.
<wand id="2184" script="wand.lua" /> <!-- magic wand -->
 
Thanks again for the quick reply, after changing what you've said now my server wont even turn on.

[Error - BaseEvents::loadFromXml] Failed to load data/weapons/weapons.xml: Start-end tags mismatch
Line 38:
</wand>

Are you sure that this is supposed to be loaded into weapons.xml? I don't have any other "script" features in my weapons xml
^
 
<wand id="2184" script="wand.lua" /> <!-- magic wand --> Looks the same to me ?

Are you sure this works for tfs 1.2? I've taken the script directly from your edited post above, created a new .lua file and pasted the script there, saved it into weapons/scripts.

I added that line of code you just sent me to my weapons.xml and it just bugs my console with mismatch lines. This is the only code in my weapons xml with a "script" setting.

<wand id="2184" script="wand.lua" /> <!-- magic wand -->
<vocation name="Sorcerer" />
</wand>
 
Last edited by a moderator:
Back
Top