• 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!

Lua Checking if target <= 3 range from me?

Zell

Intermediate OT User
Joined
Oct 23, 2010
Messages
214
Reaction score
117
Hi again my dears <3

I'm making a spell that checks if the target are on <= 3 range and if he are on pztile or not and then
doCombat..etcetc you know.

I have 1-2h reading scripts from here but i can't find the way for my version i think.
Im using a simple 0.6.3 on 7.6 server and i can't make it with this functions


Edit: i search on my luascript.cpp and i see getspectator function (but i dont know how to use)..maybe with this we can make it?
Thanks!
 
Solution
Try this:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
    local min = (level * -0.9 + maglevel * -1) * 1 * 4
    local max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

local function onCastSpell1(cid)
    if(isCreature(cid)) then
        local pos = getCreaturePosition(cid)
        doSendAnimatedText(pos, "    +2 ", 119)
        doSendMagicEffect(pos, 12)
    end
end

local function...
Code:
getDistanceBetween(getThingPos(target), getThingPos(cid)) <= 3

Thanks for help ruth, but i dont have this function on my server (i think)

Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetThingPos(). Thing not found

Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

data/spells/scripts/attack/moe kor.lua:48: attempt to call global 'getDistanceBetween' (a nil value)
stack traceback:
        data/spells/scripts/attack/moe kor.lua:48: in function <data/spells/scripts/attack/moe kor.lua:15>

Maybe can i copy that from other sources?
 
put this in functions

Code:
function getDistanceBetween(pos1, pos2)
    local xDif = math.abs(pos1.x - pos2.x)
    local yDif = math.abs(pos1.y - pos2.y)

    local posDif = math.max(xDif, yDif)
    if (pos1.z ~= pos2.z) then
        posDif = (posDif + 9 + 6)
    end
    return posDif
end
 
put this in functions

Code:
function getDistanceBetween(pos1, pos2)
    local xDif = math.abs(pos1.x - pos2.x)
    local yDif = math.abs(pos1.y - pos2.y)

    local posDif = math.max(xDif, yDif)
    if (pos1.z ~= pos2.z) then
        posDif = (posDif + 9 + 6)
    end
    return posDif
end

I have this in functions but the same effect, maybe the server dont read the xml file?
 
so put this function above onCastSpell in your moe kor.lua

now i have this error

Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetThingPos(). Thing not found

maybe im writting something wrong in the script?

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
       
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")



function onCastSpell(cid, target, var)

local parameters = { cid = cid, var = var, combat = combat}

local function onCastSpell1()
    doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
    doSendMagicEffect(getCreaturePosition(cid), 12)
end

local function onCastSpell2()
        doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
        doSendMagicEffect(getCreaturePosition(cid), 12)
end


 local function onCastSpell3(parameters)
   
            doCombat(parameters.cid, parameters.combat, parameters.var)
        end
               

doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
                    doSendMagicEffect(getCreaturePosition(cid), 12)
addEvent(onCastSpell1, 900) 
addEvent(onCastSpell2, 1900) 

    if getDistanceBetween(getThingPos(cid), getThingPos(target)) <= 3 then
addEvent(onCastSpell3, 2900, parameters) 
else
               
       
    return FALSE
    end
return TRUE
end
 
make sure target is defined in onCastSpell(cid, target, var) if not try this

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
  
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
setCombatCallback(combat1, CALLBACK_PARAM_TARGETCREATURE, "getDistance")
function getDistance(cid, target, parameters)
  if getDistanceBetween(getThingPos(cid), getThingPos(target)) <= 3 then
      addEvent(onCastSpell3, 2900, parameters)
      return TRUE
  end
  return FALSE
end
function onCastSpell(cid, target, var)
local parameters = { cid = cid, var = var, combat = combat}
local function onCastSpell1()
    doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
    doSendMagicEffect(getCreaturePosition(cid), 12)
end
local function onCastSpell2()
        doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
        doSendMagicEffect(getCreaturePosition(cid), 12)
end


local function onCastSpell3(parameters)
 
            doCombat(parameters.cid, parameters.combat, parameters.var)
        end
          
doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
                    doSendMagicEffect(getCreaturePosition(cid), 12)
addEvent(onCastSpell1, 900)
addEvent(onCastSpell2, 1900)
    if not getDistance(cid, target, parameters) then
    return FALSE
    end
return TRUE
end
 
Last edited:
make sure target is defined in onCastSpell(cid, target, var) if not try this

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
 
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
setCombatCallback(combat1, CALLBACK_PARAM_TARGETCREATURE, "getDistance")
function getDistance(cid, target, parameters)
  if getDistanceBetween(getThingPos(cid), getThingPos(target)) <= 3 then
      addEvent(onCastSpell3, 2900, parameters)
      return TRUE
  end
  return FALSE
end
function onCastSpell(cid, target, var)
local parameters = { cid = cid, var = var, combat = combat}
local function onCastSpell1()
    doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
    doSendMagicEffect(getCreaturePosition(cid), 12)
end
local function onCastSpell2()
        doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
        doSendMagicEffect(getCreaturePosition(cid), 12)
end


local function onCastSpell3(parameters)
 
            doCombat(parameters.cid, parameters.combat, parameters.var)
        end
         
doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
                    doSendMagicEffect(getCreaturePosition(cid), 12)
addEvent(onCastSpell1, 900)
addEvent(onCastSpell2, 1900)
    if not getDistance(cid, target, parameters) then
    return FALSE
    end
return TRUE
end


Now
Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetThingPos(). Thing not found

With the last script i get the same, the spells "works" but when check if TARGET Something -> error. thing not found.
I dont know why can't check info about monsters on Spells..Maybe we need add "creature.uid" or something equal check if is creature that "target"?

Today i was some hours trying editing the script but the same error.. :(

Thanks again, and i hope tomorrow we will finish it! :)
 
Now
Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetThingPos(). Thing not found

With the last script i get the same, the spells "works" but when check if TARGET Something -> error. thing not found.
I dont know why can't check info about monsters on Spells..Maybe we need add "creature.uid" or something equal check if is creature that "target"?

Today i was some hours trying editing the script but the same error.. :(

Thanks again, and i hope tomorrow we will finish it! :)
What if you add this, before the code that contains the check for the targetID
Lua:
local target = var.number

That is atleast how i did find the target in some of my spells

edit:
Else you can add this to see what is contained inside cid, target or var
Lua:
for key,value in pairs(var) do print(key,value) end
 
Last edited:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
      
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
function onCastSpell(cid, target, var)
local parameters = { cid = cid, var = var, combat = combat, target = target}
local function onCastSpell1()
    doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
    doSendMagicEffect(getCreaturePosition(cid), 12)
end
local function onCastSpell2()
        doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
        doSendMagicEffect(getCreaturePosition(cid), 12)
end
local function onCastSpell3(parameters)
  
            doCombat(parameters.cid, parameters.combat, parameters.var)
        end
              
doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
                    doSendMagicEffect(getCreaturePosition(cid), 12)
addEvent(onCastSpell1, 900)
addEvent(onCastSpell2, 1900)
    if getDistanceBetween(getThingPos(parameters.cid), getThingPos(parameters.target)) <= 3 then
addEvent(onCastSpell3, 2900, parameters)
else
              
      
    return FALSE
    end
return TRUE
end
 
JESUS...It cant be!! WHY!

Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:eek:nCastSpell

luaGetThingPos(). Thing not found
 
JESUS...It cant be!! WHY!

Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:eek:nCastSpell

luaGetThingPos(). Thing not found
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)

function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end

setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(cid, target, var)
   local parameters = { cid = cid, var = var, combat = combat, target = target}
   print(getCreatureName(parameters.target))

   local function onCastSpell1()
      doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end

   local function onCastSpell2()
      doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end

   local function onCastSpell3(parameters)
       doCombat(parameters.cid, parameters.combat, parameters.var)
   end
   doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
   doSendMagicEffect(getCreaturePosition(cid), 12)
   addEvent(onCastSpell1, 900)
   addEvent(onCastSpell2, 1900)

   return TRUE
end

Does this print in console target name?
 
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)

function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end

setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(cid, target, var)
   local parameters = { cid = cid, var = var, combat = combat, target = target}
   print(getCreatureName(parameters.target))

   local function onCastSpell1()
      doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end

   local function onCastSpell2()
      doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end

   local function onCastSpell3(parameters)
       doCombat(parameters.cid, parameters.combat, parameters.var)
   end
   doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
   doSendMagicEffect(getCreaturePosition(cid), 12)
   addEvent(onCastSpell1, 900)
   addEvent(onCastSpell2, 1900)

   return TRUE
end

Does this print in console target name?


No, i think the server dont understand the reference of target inside Spells, onCasSpell
I was reading the spells.cpp andluascript.cpp searching this functions if "target" dont appear include the functions inside onCastSpell(cid, var...etc)

I have this error:

Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetCreatureName(). Creature not found
-1
 
No, i think the server dont understand the reference of target inside Spells, onCasSpell
I was reading the spells.cpp andluascript.cpp searching this functions if "target" dont appear include the functions inside onCastSpell(cid, var...etc)

I have this error:

Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/moe kor.lua:onCastSpell

luaGetCreatureName(). Creature not found
-1
Try this:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
        min = (level * -0.9 + maglevel * -1) * 1 * 4
    max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end

function onCastSpell3(cid, target)
   doCombat(cid, target)
end

setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
setCombatCallback(combat1, CALLBACK_PARAM_TARGETCREATURE, "getDistance")

function getDistance(cid, target)
  if getDistanceBetween(getThingPos(cid), getThingPos(target)) <= 3 then
      addEvent(onCastSpell3, 2900, cid, target)
 end
end

function onCastSpell(cid, target, var)
   local parameters = { cid = cid, var = var, combat = combat, target = target}
   print(getCreatureName(parameters.target))
   local function onCastSpell1()
      doSendAnimatedText(getCreaturePosition(cid),"    +2 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end
   local function onCastSpell2()
      doSendAnimatedText(getCreaturePosition(cid),"+1 ", 119)
      doSendMagicEffect(getCreaturePosition(cid), 12)
   end

   doSendAnimatedText(getCreaturePosition(cid),"  CASTING ", 119)
   doSendMagicEffect(getCreaturePosition(cid), 12)
   addEvent(onCastSpell1, 900)
   addEvent(onCastSpell2, 1900)
   return TRUE
end
 
Try this:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
    local min = (level * -0.9 + maglevel * -1) * 1 * 4
    local max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

local function onCastSpell1(cid)
    if(isCreature(cid)) then
        local pos = getCreaturePosition(cid)
        doSendAnimatedText(pos, "    +2 ", 119)
        doSendMagicEffect(pos, 12)
    end
end

local function onCastSpell2(cid)
    if(isCreature(cid)) then
        local pos = getCreaturePosition(cid)
        doSendAnimatedText(pos, "+1 ", 119)
        doSendMagicEffect(pos, 12)
    end
end

local function onCastSpell3(cid, var)
    local target = var.number
    if(isCreature(cid) and isCreature(target)) then
        local tPos = getCreaturePosition(target)
        if(getDistanceBetween(getCreaturePosition(cid), tPos) <= 3 and not getTilePzInfo(tPos)) then
            doCombat(cid, combat, var)
        end
    end
end

function onCastSpell(cid, var)
    local pos = getCreaturePosition(cid)
    doSendAnimatedText(pos, "  CASTING ", 119)
    doSendMagicEffect(pos, 12)
    addEvent(onCastSpell1, 900, cid)
    addEvent(onCastSpell2, 1900, cid)
    addEvent(onCastSpell3, 2900, cid, var)
    return true
end
 
Solution
Try this:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_FIREAREA)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ME_FIREAREA)
function onGetFormulaValues(cid, level, maglevel)
    local min = (level * -0.9 + maglevel * -1) * 1 * 4
    local max = (level * -1.2 + maglevel * -1.3) * 1 * 4
    return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

local function onCastSpell1(cid)
    if(isCreature(cid)) then
        local pos = getCreaturePosition(cid)
        doSendAnimatedText(pos, "    +2 ", 119)
        doSendMagicEffect(pos, 12)
    end
end

local function onCastSpell2(cid)
    if(isCreature(cid)) then
        local pos = getCreaturePosition(cid)
        doSendAnimatedText(pos, "+1 ", 119)
        doSendMagicEffect(pos, 12)
    end
end

local function onCastSpell3(cid, var)
    local target = var.number
    if(isCreature(cid) and isCreature(target)) then
        local tPos = getCreaturePosition(target)
        if(getDistanceBetween(getCreaturePosition(cid), tPos) <= 3 and not getTilePzInfo(tPos)) then
            doCombat(cid, combat, var)
        end
    end
end

function onCastSpell(cid, var)
    local pos = getCreaturePosition(cid)
    doSendAnimatedText(pos, "  CASTING ", 119)
    doSendMagicEffect(pos, 12)
    addEvent(onCastSpell1, 900, cid)
    addEvent(onCastSpell2, 1900, cid)
    addEvent(onCastSpell3, 2900, cid, var)
    return true
end

I try both scripts, but the yours is working! With a fail checking pztile, but i think i can fix this minor error!!

Thanks to all family!! :) :)
 
Back
Top