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

C++ onSpawn (bug?!?!)

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
HI! I'm using this onSpawn code, creatred by @Delusion (Feature - [TFS 1.2] Monster:onSpawn(position, startup, artificial) (https://otland.net/threads/tfs-1-2-monster-onspawn-position-startup-artificial.257140/))

but it have one BUG....
My server have orshabaal respaw, and i put % chance to "spawn" ghazbaran, if orshaball dies.
The problem is when "ghazbaran" spawn, orshabaal spawn too (they are stacked in the same place).
wanted to modify, so if the boss was born, the other monster would not be born until the boss is killed
Lua:
function Monster:onSpawn(position, startup, artificial)
    if self:getName() == "Orshabaal" then
        if math.random(100) <= 5 then
            Game.createMonster("Ghazbaran", position, true, true)
        end
    end
   return true
end
 
All you have to do is return false if you don't want the original monster to spawn.
Add it below where you create Ghazbaran.
 
Solution
hmm, but if he return false, lets say that spawn time is 60 seconds, after 60 seconds the spawn will not try to respawn again the creature? how he should deal with that?
 
@Liften He can just storage when this boss is appear and return false when it's true with spawn orshaball, on kill boss change storage and this is all.
Or check the tiles around the spawn(if boss is around then return false).
 
@Liften He can just storage when this boss is appear and return false when it's true with spawn orshaball, on kill boss change storage and this is all.
Or check the tiles around the spawn(if boss is around then return false).

For a single boss ok, but the most common use for this function i think will be something like what people do in pokemons, do you know? every creature has like for example: 1,1000 chance to spawn a different one, in this ocasion it will be not usefull, will be only usefull if its a single boss..
 
I'm not sure why I didn't write it to just return false, maybe I had a reason but now looking at it, it looks weird.
Try this instead:
C++:
bool Spawn::spawnMonster(uint32_t spawnId, MonsterType* mType, const Position& pos, Direction dir, bool startup /*= false*/)
{
    std::unique_ptr<Monster> monster_ptr(new Monster(mType));
    
    if (!g_events->eventMonsterOnSpawn(monster_ptr.get(), pos, startup, false)) {
        return false;
    }

    if (startup) {
        //No need to send out events to the surrounding since there is no one out there to listen!
        if (!g_game.internalPlaceCreature(monster_ptr.get(), pos, true)) {
            return false;
        }
    } else {
        if (!g_game.placeCreature(monster_ptr.get(), pos, false, true)) {
            return false;
        }
    }

    Monster* monster = monster_ptr.release();
    monster->setDirection(dir);
    monster->setSpawn(this);
    monster->setMasterPos(pos);
    monster->incrementReferenceCounter();

    spawnedMap.insert(spawned_pair(spawnId, monster));
    spawnMap[spawnId].lastSpawn = OTSYS_TIME();
    return true;
}
 
Back
Top