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

RevScripts How to register creature event dynamically

nsanee

Member
Senator
Joined
Apr 13, 2008
Messages
669
Reaction score
20
I've just recently started converting my old scripts to TFS 1.3's revscriptsys and for the life of mine I can't figure out how to register an onDeath event for a newly created monster. I know I could do it this way with TFS 0.4 (although the event had to be registered in xml).

Anyone knows how to do it properly? I'd like to take advantage of the ability to have everything in .lua and avoid registering events in monsters.

There are no errors in the console, just that onDeath doesn't fire when I kill these mobs.

Lua:
local storage = G_storage.svargrond_arena


boss_death = CreatureEvent('arena_boss_death')
boss_death:type('death')

function boss_death.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    print('on death')
    setCanEnterNextRoom(mostdamagekiller, true)
    return true
end


local start_tp = MoveEvent()
start_tp:type('stepin')

for k, v in pairs(arena_monsters) do start_tp:aid(k) end




-- Enter next arena teleport
function start_tp.onStepIn(creature, item, position, fromPosition)
    if not creature:isPlayer() then
        return false
    end
    if not canEnterNextRoom(creature) then
        creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to kill the boss first!")
        return false
    end
   
    if not isArenaRoomFree(item.actionid) then
        creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Someone is fighting on the other side. You have to wait.")
        return false
    end
   
    local arena_level = creature:getStorageValue(storage['did_arena_level'])
    arena_level = arena_level < 1 and 0 or math.min(arena_level, 2)
   
    local level_offset = arena_level * 10 + item.actionid
   
    local tile = Item(item.actionid) -- a bit confusing, but tp's AID = UID of the tile in the next room
   
    if not tile then -- means that's the last tp so the arena is complete
        creature:teleportTo(arena_kick_pos)
        creature:setStorageValue(storage['did_arena_level'], arena_level + 1)
        creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations! You have completed the ".. getArenaLevelName(arena_level) .." arena!")
        return true
    end
   
    local next_pos = tile:getPosition()

    local monster = Monster(Game.getStorageValue(level_offset))
    if monster and monster:isMonster() then
        print(monster:getName() .. ' is alive. Killing now!')
        monster:remove()
    end
   
    monster = Game.createMonster(arena_monsters[level_offset], Position(next_pos.x - 1, next_pos.y - 1, next_pos.z))
    Game.setStorageValue(level_offset, monster)
   
   
    -- tried to register each one separetely or both but still onDeath doesn't fire
    monster:getType():registerEvent('arena_boss_death')
    monster:registerEvent('arena_boss_death')

    creature:teleportTo(next_pos)
    setCanEnterNextRoom(creature, false)
    return true
   
end

local login = CreatureEvent('arena_login')
login:type('login')

function login.onLogin(player)
    setCanEnterNextRoom(player, true)
    return true
end

login:register()
boss_death:register()
start_tp:register()
 
<script><event name="arena_boss_death" /></script>
in monster xml file, there is no new way of registering it with revscript :/

edit: or you can use @Delusion 's OnSpawn event to register it on creature spawn, and even better you could use it for other things as well
 
<script><event name="arena_boss_death" /></script>
in monster xml file, there is no new way of registering it with revscript :/

edit: or you can use @Delusion 's OnSpawn event to register it on creature spawn, and even better you could use it for other things as well
Well that's a bummer if true. As far as I remember official wiki said it can be registered in .lua.

Anyways I could still register the onKill event for the player involved, so I don't really have to mess with xml - seems like it's the next best thing at the moment.
 
The problem is easily explained, you have to be carefull about the registration order.
Currently you basicly try to register a monstertype an event which isn't registered itself at that point of the script, you register it currently at the end of the file.
Move boss_death:register() directly under the end of function boss_death.onDeath(...) and it should be good to go.
 
The problem is easily explained, you have to be carefull about the registration order.
Currently you basicly try to register a monstertype an event which isn't registered itself at that point of the script, you register it currently at the end of the file.
Move boss_death:register() directly under the end of function boss_death.onDeath(...) and it should be good to go.

This alone didn't work. However then it dawned on me that it might have something to do with me killing those mobs with a god character. So I've checked groups and after setting <flag notgenerateloot="0" /> it worked! Now I guess there might be a reason why this flag blocks onDeath from executing, but it's not very intuitive as the creature is still technically dying, just without loot.
 
This alone didn't work. However then it dawned on me that it might have something to do with me killing those mobs with a god character. So I've checked groups and after setting <flag notgenerateloot="0" /> it worked! Now I guess there might be a reason why this flag blocks onDeath from executing, but it's not very intuitive as the creature is still technically dying, just without loot.
ok that's absolutely weird, it's good to know that such a problem exists, feel free to write a bug report on github about it.
 
Back
Top