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

Lua Could u rate my code ? :D

BulawOw

Soul of Shinobi
Joined
Sep 15, 2014
Messages
204
Solutions
8
Reaction score
62
LUA:
local spellEvents = {}


local spellOriginalOutfits = {}

local config1 = {
    OUTFIT_FIRST = 1013,
    OUTFIT_LAST = 1016,
}

local config2 = {
    OUTFIT_FIRST = 1023,
    OUTFIT_LAST = 1026,
}

local config3 = {
    OUTFIT_FIRST = 1018,
}

local function copyOutfit(creatureId, newLooktype)
    local originalOutfit = spellOriginalOutfits[creatureId]
    local outfit = {
        lookType = newLooktype,
        lookHead = originalOutfit.lookHead,
        lookBody = originalOutfit.lookBody,
        lookLegs = originalOutfit.lookLegs,
        lookFeet = originalOutfit.lookFeet,
        lookAddons = originalOutfit.lookAddons,
        lookMount = originalOutfit.lookMount
    }
    return outfit
end

local function setOriginalOutfit(creature, lookType)
    creature = Creature(creature)
    if not creature or creature:isRemoved() then
        return
    end

    creature:setOutfit(spellOriginalOutfits[creature:getId()])

end

local function animateGateThird(creature, lookType)
    creature = Creature(creature)
    if not creature or creature:isRemoved() then
        return
    end

    creature:setOutfit(copyOutfit(creature:getId(), lookType))

    if lookType == config3.OUTFIT_FIRST then
        spellEvents[creature:getId()] = addEvent(setOriginalOutfit, 100, creature)
    else
        spellEvents[creature:getId()] = addEvent(animateGateThird, 200, creature)
    end
end

local function animateGateSecond(creature, lookType)
    creature = Creature(creature)
    if not creature or creature:isRemoved() then
        return
    end

    local pos = creature:getPosition()
    local playerOffset = creature:getPosition()
        playerOffset.x = playerOffset.x + 1
        playerOffset.y = playerOffset.y + 1
   
    creature:setOutfit(copyOutfit(creature:getId(), lookType))

    if lookType == config2.OUTFIT_LAST then
        spellEvents[creature:getId()] = addEvent(animateGateThird, 300, creature, config3.OUTFIT_FIRST)
        Game.sendAnimatedText("text2", pos, 180)
        playerOffset:sendMagicEffect(86)
    else
        spellEvents[creature:getId()] = addEvent(animateGateSecond, 100, creature, lookType + 1)
    end
end

local function animateGate(creature, lookType)
    creature = Creature(creature)
    if not creature or creature:isRemoved() then
        return
    end

    local pos = creature:getPosition()

    creature:setOutfit(copyOutfit(creature:getId(), lookType))


    if lookType == config1.OUTFIT_LAST then
        spellEvents[creature:getId()] = addEvent(animateGateSecond, 1000, creature, config2.OUTFIT_FIRST)
        Game.sendAnimatedText("text1", pos, 180)
    else
        spellEvents[creature:getId()] = addEvent(animateGate, 50, creature, lookType + 1)
    end
end

function onCastSpell(creature, variant)  

    local originalOutfit = creature:getOutfit()
    spellOriginalOutfits[creature:getId()] = originalOutfit
    spellOriginalOutfits[creature:getId()] = copyOutfit(creature:getId(), originalOutfit.lookType)

    if spellEvents[creature:getId()] then
        stopEvent(spellEvents[creature:getId()])
    end

    spellEvents[creature:getId()] = addEvent(animateGate, 10, creature, config1.OUTFIT_FIRST, position)
end

Just like in title how is the structure if its stable (probably not cause i had to turn off safe scripts ;p) if not maybe how can i improve it, etc.

Thanks in advance
 
If you're going to be using a parameter more then once, it's almost always more efficient to put it into a local, so you don't need to calculate the variable a second/3/4/5/6/7 time.

Example:

old code
LUA:
function onCastSpell(creature, variant) 

    local originalOutfit = creature:getOutfit()
    spellOriginalOutfits[creature:getId()] = originalOutfit
    spellOriginalOutfits[creature:getId()] = copyOutfit(creature:getId(), originalOutfit.lookType)

    if spellEvents[creature:getId()] then
        stopEvent(spellEvents[creature:getId()])
    end

    spellEvents[creature:getId()] = addEvent(animateGate, 10, creature, config1.OUTFIT_FIRST, position)
end
new code
LUA:
function onCastSpell(creature, variant) 

    local originalOutfit, creature_id = creature:getOutfit(), creature:getId()
    spellOriginalOutfits[creature_id] = originalOutfit
    spellOriginalOutfits[creature_id] = copyOutfit(creature_id, originalOutfit.lookType)

    if spellEvents[creature_id] then
        stopEvent(spellEvents[creature_id])
    end

    spellEvents[creature_id] = addEvent(animateGate, 10, creature, config1.OUTFIT_FIRST, position)
end

or in places like this, where you already have the creatureId, and you overwrite the variable, then re-create it again later.
LUA:
local function animateGate(creature, lookType)
    creature = Creature(creature)
    if not creature or creature:isRemoved() then
        return
    end

    local pos = creature:getPosition()

    creature:setOutfit(copyOutfit(creature:getId(), lookType))


    if lookType == config1.OUTFIT_LAST then
        spellEvents[creature:getId()] = addEvent(animateGateSecond, 1000, creature, config2.OUTFIT_FIRST)
        Game.sendAnimatedText("text1", pos, 180)
    else
        spellEvents[creature:getId()] = addEvent(animateGate, 50, creature, lookType + 1)
    end
end
In situations like this, you should create a new local variable to hold the creature info, so you don't need to re-create it.
LUA:
local function animateGate(creature_id, lookType)
    local temp_creature = Creature(creature_id)
    if not temp_creature or temp_creature:isRemoved() then
        return
    end

    local pos = temp_creature:getPosition()

    temp_creature:setOutfit(copyOutfit(creature_id, lookType))


    if lookType == config1.OUTFIT_LAST then
        spellEvents[creature_id] = addEvent(animateGateSecond, 1000, temp_creature, config2.OUTFIT_FIRST)
        Game.sendAnimatedText("text1", pos, 180)
    else
        spellEvents[creature_id] = addEvent(animateGate, 50, temp_creature, lookType + 1)
    end
end

Have to run to the store, but that gives you some stuff to work on at least.
 
Last edited by a moderator:
Back
Top