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

TFS 1.X+ onSpawn event issue

nivekzin

New Member
Joined
Sep 24, 2012
Messages
3
Reaction score
0
I'm trying to setup a Shiny spawn on my PokeTibia server, but I'm having some problems with this onSpawn callback.
I understood that when I summon mannually the monster it will pass "again" in the onSpawn for that new creature, so it seems a made a semi-infinite loop and I would like to avoid that that new creature prevent to pass through this loop or at least had a flag to prevent to create a new creature.

Is there any way I could solve this?

Lua:
local function isShiny()
chance = 400

  if math.random(1, 1000) <= chance then
    return true
  end

  return false
end

Lua:
ec.onSpawn = function(self, position, startup, artificial)
  if artificial then
    return true
  end

  if startup and self:isMonster() then
    if isShiny(self) then
      doRemoveCreature(self:getId())
      spawnedShiny = Game.createMonster("Shiny "..self:getName(), position, false, false)

      if spawnedShiny ~= nil then
        doRemoveCreature(spawnedShiny:getId())
        self = spawnedShiny
        Game.createMonster(self:getName(), position, false, false)
        print("A "..self:getName().." has spawned! Go check it!")
        print("POS: X -"..position.x.." Y - "..position.y.. " / Health: "..self:getHealth())
        return false
      end
    end
  end

  spawnedMob = Game.createMonster(self:getName(), position, false, false)
  self = spawnedMob
  doRemoveCreature(spawnedMob:getId())
  return true
end
 
By default, monsters generated through the createMonster method are marked as artificial

And since you have this conditional on your code, nothing will happen
Lua:
if artificial then
    return true
end

That being said, you cannot override the original spawn. That means if you spawn a shiny and you remove the original creature plus return false, the creature will spawn anyway (on the next spawn interval) since the shiny its not related to the spawn itself

As aside note, you cannot remove the spawned creature inside an onSpawn event unless you apply this commit to your sources and the way it should be done is through a timer

Lua:
addEvent(function(monsterId)

    local monster = Monster(monsterId)
    if not monster then
        return
    end

    monster:remove()
end, 0, self:getId())

Don't get me wrong tho, it's still possibe.
One solution would be to turn the spawned creature into a shiny by modifying its properties (name, damage, health, speed, outfit, buffed loot, catch chance etc), all this can be done through Lua in case you are avoiding C++ edits
 
By default, monsters generated through the createMonster method are marked as artificial

And since you have this conditional on your code, nothing will happen
Lua:
if artificial then
    return true
end

That being said, you cannot override the original spawn. That means if you spawn a shiny and you remove the original creature plus return false, the creature will spawn anyway (on the next spawn interval) since the shiny its not related to the spawn itself

As aside note, you cannot remove the spawned creature inside an onSpawn event unless you apply this commit to your sources and the way it should be done is through a timer

Lua:
addEvent(function(monsterId)

    local monster = Monster(monsterId)
    if not monster then
        return
    end

    monster:remove()
end, 0, self:getId())

Don't get me wrong tho, it's still possibe.
One solution would be to turn the spawned creature into a shiny by modifying its properties (name, damage, health, speed, outfit, buffed loot, catch chance etc), all this can be done through Lua in case you are avoiding C++ edits
I got what you said and I'm doing the related modifications on the code.
But let me understand one thing, it should already substitute the current monster to the "shiny" monster?
In the moment I do this:
Lua:
self = shinySpawned
Is it should not be enough to change the curernt monster? Or I need to change it manually?
Post automatically merged:

1700215415853.png

It seems I created an infinite loop when I took the artificial verification.
Besides that I changed the code to this and now the shinies spawn but the normal ones don't spawn.
And I would like to understand why I need to force the spawn of my creatures? Did I made anything wrong that the automatic respawn isn't working?

I sent the image, on the top left should have a normal venonat and didn't spawned.
1700215746280.png
 
Last edited:
Back
Top