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

Solved GlobalEvent, attempt to spawn with check

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,484
Solutions
9
Reaction score
217
hello, I'm trying make a script to create a random boss in some position, I need check the bosses names to not respawn again the bosses, I need make that when is in the table run the code again to try spawn another, while spawn a different boss
here the script:
Code:
local config = {
    [1] = {bossName = "Name", bossPosition = Position()}
}
Bosses = {}
function onThink(interval, lastExecution, thinkInterval)
    math.randomseed(os.time())
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, name) then
        print('>> attempt to spawn '..name..' again.')
        return true
    end
    table.insert(Bosses, name)
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' is missing map.')
        return true
    end   
    print('>> The boss: ' ..monster:getName() .. ' spawn.')
    return true
end
of course the table "config" have more than 1 member
 
hello, I'm trying make a script to create a random boss in some position, I need check the bosses names to not respawn again the bosses, I need make that when is in the table run the code again to try spawn another, while spawn a different boss
here the script:
Code:
local config = {
    [1] = {bossName = "Name", bossPosition = Position()}
}
Bosses = {}
function onThink(interval, lastExecution, thinkInterval)
    math.randomseed(os.time())
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, name) then
        print('>> attempt to spawn '..name..' again.')
        return true
    end
    table.insert(Bosses, name)
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' is missing map.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' spawn.')
    return true
end
of course the table "config" have more than 1 member
Recursion is your friend, however this can/will result in an infinite loop :p
Code:
local c = {
    [1] = {bossName = "Name", bossPosition = Position()}
}
Bosses = {}

function spawnBoss(t)
    math.randomseed(os.time())
    local creature = t[math.random(#t)]
    if isInArray(Bosses, creature.bossName) then
        print('>> attempt to spawn '..name..' again.')
        spawnBoss(t) -- recursive, call again if name exist
    end
    -- update the global Bosses table with the creature name if it does not exist
    table.insert(Bosses, creature.bossName)
    -- return the local table's return value
    return creature
end

function onThink(interval, lastExecution, thinkInterval)
    local rand = spawnBoss(c)
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' is missing map.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' spawn.')
    return true
end
 
If resurcion causes an infinite loop that is because you made a mistake..

@silveralol
Set math.randomseed in global.lua. You should not set a random seed every time you call a function.

Code:
local bosses = {
    {bossName = "Boss 1", bossPosition = Position()}
    {bossName = "Boss 2", bossPosition = Position()}
}
Bosses = {}

function spawnBoss()
    if #Bosses >= #bosses then
        for i = #Bosses, 1, -1 do
            Bosses[i] = nil
        end
    end

    local creature = bosses[math.random(#bosses)]
    if isInArray(Bosses, creature.bossName) then
        -- print('>> attempt to spawn '..creature.bossName..' again.')
        creature = spawnBoss() -- recursive, call again if name exist
    else
        -- update the global Bosses table with the creature name if it does not exist
        table.insert(Bosses, creature.bossName)
    end
    -- return the local table's return value
    return creature
end

function onThink(interval, lastExecution, thinkInterval)
    local rand = spawnBoss()
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..rand.bossName..'.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' spawn.')
    return true
end
 
If resurcion causes an infinite loop that is because you made a mistake..
Indeed it is and I was aware of that, this is why I said it can/will result in an infinite loop, I just woke up and hadn't had my coffee yet :p
I was just providing an example not a solution :)
 
If resurcion causes an infinite loop that is because you made a mistake..

@silveralol
Set math.randomseed in global.lua. You should not set a random seed every time you call a function.

Code:
local bosses = {
    {bossName = "Boss 1", bossPosition = Position()}
    {bossName = "Boss 2", bossPosition = Position()}
}
Bosses = {}

function spawnBoss()
    if #Bosses >= #bosses then
        for i = #Bosses, 1, -1 do
            Bosses[i] = nil
        end
    end

    local creature = bosses[math.random(#bosses)]
    if isInArray(Bosses, creature.bossName) then
        -- print('>> attempt to spawn '..creature.bossName..' again.')
        creature = spawnBoss() -- recursive, call again if name exist
    else
        -- update the global Bosses table with the creature name if it does not exist
        table.insert(Bosses, creature.bossName)
    end
    -- return the local table's return value
    return creature
end

function onThink(interval, lastExecution, thinkInterval)
    local rand = spawnBoss()
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..rand.bossName..'.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' spawn.')
    return true
end
I use the math.randomseed to make it always randon, if I remove it, always will be the same boss, I'll test it, thank you, @Codex NG and @Summ

edit: oh god! thank you, Mans, if resolve my problem!
I have a little question, seems stupid but I don't know...
can explain this line?
Code:
for i = #Bosses, 1, -1 do
the "-1" ?
 
Last edited:
That's the loops step interval of the loop, default step is 1.
When iterating from a higher value to 1 you need to decrease your loop variable.
 
Back
Top