• 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 Get boots id value from creature

pasiak12

Well-Known Member
Joined
Jun 7, 2009
Messages
261
Solutions
13
Reaction score
71
Hi!

I am learning LUA programming on tfs 1.2

For the first task I decided to modify the Haste spell. I want it to behave differently depending if the player is wearing boots of haste.

This is my code:

Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)

local condition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 5000) -- standard time
condition:setFormula(0.3, 0, 0.3, 0) -- values
combat:setCondition(condition)


function onCastSpell(creature, variant)

x = creature:getId() -- player cid???
doPlayerSendTextMessage(x, MESSAGE_STATUS_CONSOLE_BLUE, "test msg")

local b = getPlayerSlotItem(x, 8)
print("ID: " .. x .. b)


if getPlayerSlotItem(x, 8) == 2195 then
    print("got boh")
    condition:setParameter(CONDITION_PARAM_TICKS, 10000) --10 sec
else
    print("no bohs")
    condition:setParameter(CONDITION_PARAM_TICKS, 1000) -- 1 sec
end
    return combat:execute(creature, variant)
end


As you can see I dont receive in function onCastSpell(creature, variant) the players cid. I think I get it from creature:getId() because the function to send the message works correctly on the player executing the spell.

The function getPlayerSlotItem(x, 8) where x should be CID returns a bad item array. Every time its called it returns a different value (the player wears the same boots all time).

I think it's not the correct cid. How to get players boots id when the function has only creature variable?

I would be grateful if anyone could send me a documentation to lua funcions of TFS 1.2. I know its possible to read it from the sources but it takes a lot of time to check out the functions from there.
 
Solution
my bad i forgot to change condition/combat words
Code:
local defaultCondition = Condition(CONDITION_HASTE)
defaultCondition:setParameter(CONDITION_PARAM_TICKS, 1000)
defaultCondition:setFormula(0.3, 0, 0.3, 0)

local defaultCombat = Combat()
defaultCombat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
defaultCombat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
defaultCombat:setCondition(defaultCondition)

local bohCondition = Condition(CONDITION_HASTE)
bohCondition:setParameter(CONDITION_PARAM_TICKS, 10000)
bohCondition:setFormula(0.3, 0, 0.3, 0)

local bohCombat = Combat()
bohCombat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
bohCombat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
bohCombat:setCondition(bohCondition)...
you do not need cid since 1.2 uses userdata + methods
here is the function documentation that i use (thanks to cbrm)
http://pastebin.com/gAPr7pV9

"getPlayerSlotItem" returns an item userdata anyways, so you can execute a method to get id, or access it with .itemid
you can use this instead
Code:
local condition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 5000) -- standard time
condition:setFormula(0.3, 0, 0.3, 0) -- values

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(condition)


function onCastSpell(creature, variant)
    local player = creature:getPlayer() -- get player userdata from creature if possible
    -- make sure player exists, if not then return false
    if not player then
        return false
    end

    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "test msg")


    if player:getSlotItem(CONST_SLOT_BOOTS).itemid == 2195 then
        print("got boh")
        condition:setParameter(CONDITION_PARAM_TICKS, 10000) --10 sec
    else
        print("no bohs")
        condition:setParameter(CONDITION_PARAM_TICKS, 1000) -- 1 sec
    end
    return combat:execute(player, variant)
end
 
Thanks for response!

I've edited the code. Changed the enum , added the 'no boots' exception. Everything works but its not changing the spell duration based on boots id.

Code:
local condition = Condition(CONDITION_HASTE)
--condition:setParameter(CONDITION_PARAM_TICKS, 1000) -- standard time [DISABLED]
condition:setFormula(0.3, 0, 0.3, 0) -- speed increase values

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(condition)


function onCastSpell(creature, variant)
    local player = creature:getPlayer() -- get player userdata from creature if possible
    -- make sure player exists, if not then return false
    if not player then
        return false
    end

    local bootsItem = player:getSlotItem(CONST_SLOT_FEET)
    if not bootsItem then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "no boots")
        return false
    end
    
    local id_b = player:getSlotItem(CONST_SLOT_FEET).itemid
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "BOOTS ID: " .. id_b)


    if id_b == 2195 then
        print("got boh")
        condition:setParameter(CONDITION_PARAM_TICKS, 10000) --10 sec
    else
        print("another boots")
        condition:setParameter(CONDITION_PARAM_TICKS, 1000) -- 1 sec
    end
    return combat:execute(player, variant)
end

The script prints correctly the messages ('got boh' , 'another boots'), so it goes to correct if cases. But the haste spell duration is always 1 sec. Seems like its ignoring condition:setParameter() funtion in onCastSpell area and taking the default spell time from sources. I am able to change the spell time duration only at start lua script.
Code:
local condition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 10000) --time
condition:setFormula(0.3, 0, 0.3, 0) -- speed increase values

I would like to change it dynamically depending on what boots are player wearing right now. Is it possible to do that without editing engine source code?
 
thats what i thought too, i was sure you couldn't change combat/conditions outside of functions

what you can do is create two conditions, and send the correct combat depending on their boots like this:
Code:
local defaultCondition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 1000)
condition:setFormula(0.3, 0, 0.3, 0)

local defaultCombat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(defaultCondition)

local bohCondition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 10000)
condition:setFormula(0.3, 0, 0.3, 0)

local bohCombat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(bohCondition)

function onCastSpell(creature, variant)
    local player = creature:getPlayer() -- get player userdata from creature if possible

    -- make sure player exists, if not then return false
    if not player then
        return false
    end

    local bootsItem = player:getSlotItem(CONST_SLOT_FEET)
    if not bootsItem then
        return false
    end
    -- If itemid is 2195, use bohCombat; else use defaultCombat
    local combat = (bootsItem.itemid == 2195) and bohCombat or defaultCombat
   
    return combat:execute(player, variant)
end
 
Code:
local defaultCondition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 1000)
condition:setFormula(0.3, 0, 0.3, 0)

local defaultCombat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(defaultCondition)

local bohCondition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 10000)
condition:setFormula(0.3, 0, 0.3, 0)

local bohCombat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(bohCondition)

Doesn't work. It gives me that error
ceJitJ4.png


I made some tests and I think there are must be a variables called exactly 'combat' and 'condition'.

I tried with this
Code:
--Default properties for spell
local condition = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 3000)
condition:setFormula(0.3, 0, 0.3, 0) -- speed increase values

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(condition)

--properties for spell with BOH on
local condition2 = Condition(CONDITION_HASTE)
condition:setParameter(CONDITION_PARAM_TICKS, 10000)
condition:setFormula(0.3, 0, 0.3, 0) -- speed increase values

local combat2 = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
combat:setCondition(condition2)

And for test , on the end of the script I put

Code:
    combat = combat --Default properties
    return combat:execute(player, variant)

With this, it correctly gives the haste for 3 seconds (for all cases - default time)

But..
Code:
    combat = combat2 --BOH properties
    return combat:execute(player, variant)

The spell isn't working. (Thereare no errors, the spell just isnt working - just only text msg 'utani hur')

Seems like the lua function by default is able to has the only one combat and condition thing. :(
 
my bad i forgot to change condition/combat words
Code:
local defaultCondition = Condition(CONDITION_HASTE)
defaultCondition:setParameter(CONDITION_PARAM_TICKS, 1000)
defaultCondition:setFormula(0.3, 0, 0.3, 0)

local defaultCombat = Combat()
defaultCombat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
defaultCombat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
defaultCombat:setCondition(defaultCondition)

local bohCondition = Condition(CONDITION_HASTE)
bohCondition:setParameter(CONDITION_PARAM_TICKS, 10000)
bohCondition:setFormula(0.3, 0, 0.3, 0)

local bohCombat = Combat()
bohCombat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
bohCombat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
bohCombat:setCondition(bohCondition)

function onCastSpell(creature, variant)
    local player = creature:getPlayer() -- get player userdata from creature if possible

    -- make sure player exists, if not then return false
    if not player then
        return false
    end

    local bootsItem = player:getSlotItem(CONST_SLOT_FEET)
    if not bootsItem then
        return false
    end
    -- If itemid is 2195, use bohCombat; else use defaultCombat
    local combat = (bootsItem.itemid == 2195) and bohCombat or defaultCombat
  
    return combat:execute(player, variant)
end
your second combat doesnt work because you created a new combat object for combat2 but never used combat2:setParameter, you used combat:setParameter
which set multiple parameters to your old combat instead of the new combat object
 
Solution
Yeah, it works now. This solution is enough for my needs. I would like to Thank you for your support and your time you spent on me. :)
 
Back
Top