• 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 [OTX 2.X] replace creature onSpawn

elnelson

Lunaria World Dev
Joined
Jun 20, 2009
Messages
580
Solutions
2
Reaction score
58
Location
México
Hello ,otlanders. ive been trying to do something with the function onSpawn, basically i want to modify the spawns of X creatures.
for example:
i have a cave full of rats, but i want to change the creature to a rat_elite onSpawn (after being killed/removed and pass X time), so there is a % chance to spawn a rat_elite instead of a rat (both has same name), i've been trying to make the script but i cant find a way to avoid duplicated spawn (the script its looping continues creating creatures every spawn timer)

this is the script so far:


Lua:
function onSpawn(cid)
    local pos = getCreaturePosition(cid)
    if getCreatureName(cid) == "Rat" then
        if doCreateMonster('Rat_elite', getClosestFreeTile(cid, pos) then
            print("success")
            doRemoveCreature(cid) --this suppose to remove the RAT and leave only the RAT_ELITE, but when the creature is removed it continues to loop the spawn until it get 9+ creatures on the same spot and spam errors on console
        else
            print("fail")
        end
    end
    return true
end

PD: i've tried placing RETURN FALSE on line 13 to terminate the onSpawn, but the rat still spawning.

Anyone could help me achieve this?

as title says im using OTX 2.X (v7.72)
 
Hello ,otlanders. ive been trying to do something with the function onSpawn, basically i want to modify the spawns of X creatures.
for example:
i have a cave full of rats, but i want to change the creature to a rat_elite onSpawn (after being killed/removed and pass X time), so there is a % chance to spawn a rat_elite instead of a rat (both has same name), i've been trying to make the script but i cant find a way to avoid duplicated spawn (the script its looping continues creating creatures every spawn timer)

this is the script so far:


Lua:
function onSpawn(cid)
    local pos = getCreaturePosition(cid)
    if getCreatureName(cid) == "Rat" then
        if doCreateMonster('Rat_elite', getClosestFreeTile(cid, pos) then
            print("success")
            doRemoveCreature(cid) --this suppose to remove the RAT and leave only the RAT_ELITE, but when the creature is removed it continues to loop the spawn until it get 9+ creatures on the same spot and spam errors on console
        else
            print("fail")
        end
    end
    return true
end

PD: i've tried placing RETURN FALSE on line 13 to terminate the onSpawn, but the rat still spawning.

Anyone could help me achieve this?

as title says im using OTX 2.X (v7.72)
Let the Rat spawn fully before removing and replacing it.
Lua:
local function swapCreature(cid, newMonster)
    local position = getCreaturePosition(cid)
    doRemoveCreature(cid)
    addEvent(doCreateMonster, 0, newMonster, position)
end

function onSpawn(cid)
    if getCreatureName(cid) == "Rat" then
        addEvent(swapCreature, 0, cid, "Rat_elite")
    end
    return true
end
 
Last edited:
Let the Rat spawn fully before removing and replacing it.
Lua:
local function swapCreature(cid, newMonster)
    local position = getCreaturePosition(cid)
    doRemoveCreature(cid)
    addEvent(doCreateMonster, 0, newMonster, position)
end

function onSpawn(cid)
    if getCreatureName(cid) == "Rat" then
        addEvent(swapCreature, 0, cid, "Rat_elite")
    end
    return true
end
i still get double spawn. also getting this error spammed when replacing creatures on startup, what could be the reason?:


C++:
[12/4/2021 16:55:7] [Error - CreatureScript Interface]
[12/4/2021 16:55:7] In a timer event called from:
[12/4/2021 16:55:7] data/creaturescripts/scripts/monsterSpawn.lua:onSpawn
[12/4/2021 16:55:7] Description:
[12/4/2021 16:55:7] (LuaInterface::luaDoCreateMonster) Cannot create monster: Rat_elite
Post automatically merged:

the double spawn still. but getting this error spammed when replacing creatures on startup what could be the reason:


C++:
[12/4/2021 16:55:7] [Error - CreatureScript Interface]
[12/4/2021 16:55:7] In a timer event called from:
[12/4/2021 16:55:7] data/creaturescripts/scripts/monsterSpawn.lua:onSpawn
[12/4/2021 16:55:7] Description:
[12/4/2021 16:55:7] (LuaInterface::luaDoCreateMonster) Cannot create monster: Rat_elite
PD: srry double posting got a lag spike
 

Attachments

Last edited:
i still get double spawn. also getting this error spammed when replacing creatures on startup, what could be the reason?:


C++:
[12/4/2021 16:55:7] [Error - CreatureScript Interface]
[12/4/2021 16:55:7] In a timer event called from:
[12/4/2021 16:55:7] data/creaturescripts/scripts/monsterSpawn.lua:onSpawn
[12/4/2021 16:55:7] Description:
[12/4/2021 16:55:7] (LuaInterface::luaDoCreateMonster) Cannot create monster: Rat_elite
Post automatically merged:


PD: srry double posting got a lag spike
Probably your Rat_elite spelling.

I don't think you can use ___ in the name.

for testing purposes, just change to cyclops or something else that definitely works.

If it starts working, then you know it's the naming scheme that is the issue.
 
Probably your Rat_elite spelling.

I don't think you can use ___ in the name.

for testing purposes, just change to cyclops or something else that definitely works.

If it starts working, then you know it's the naming scheme that is the issue.
well, the script work, it does spawn a rat_elite (it has the same name as a rat), but after respwn time, it will respawn again, and again, and again

i've tried with a cyclops and it worked, i guess its not the name scheme u.u
 
The problem is that eliminating the spawn immediately starts the countdown to its reappearance
You must propose another scheme for your purpose, maybe you can explain a little more about what you want and if someone is willing to do it for you, here you will surely find the answer
 
well, the script work, it does spawn a rat_elite (it has the same name as a rat), but after respwn time, it will respawn again, and again, and again

i've tried with a cyclops and it worked, i guess its not the name scheme u.u
omg right. you're trying to do 2 separate things.

The onSpawn thing isn't going to work in this scenario.

It'll spawn the Rat, -> remove Rat -> create Rat_Elite
Then it'll notice there is no Rat, and attempt to spawn another Rat.

Therefore, you get your loop issue.
---

Since there is no way to add/remove/change a spawn from rme.. (I don't think..?)
You'd have to think of another solution for this.
 
omg right. you're trying to do 2 separate things.

The onSpawn thing isn't going to work in this scenario.

It'll spawn the Rat, -> remove Rat -> create Rat_Elite
Then it'll notice there is no Rat, and attempt to spawn another Rat.

Therefore, you get your loop issue.
---

Since there is no way to add/remove/change a spawn from rme.. (I don't think..?)
You'd have to think of another solution for this.
The problem is that eliminating the spawn immediately starts the countdown to its reappearance
You must propose another scheme for your purpose, maybe you can explain a little more about what you want and if someone is willing to do it for you, here you will surely find the answer
well basically i just want to add random spanws of certain monster EG:
90% chance to spawn normal rat
10% chance to spawn rat_invis instead normal
5% chance to spawn rat_elite instead normal
 
well basically i just want to add random spanws of certain monster EG:
90% chance to spawn normal rat
10% chance to spawn rat_invis instead normal
5% chance to spawn rat_elite instead normal
local function swapCreature(cid, newMonster)
local position = getCreaturePosition(cid)
doRemoveCreature(cid)
addEvent(doCreateMonster, 0, newMonster, position)
end

function onSpawn(cid)
local rand = math.random(1,5)
if rand == 5 then
print("Spawned monster - Name: ".. getCreatureName(cid))
if getCreatureName(cid) == "Rat" then
addEvent(swapCreature, 0, cid, "Rat_elite")
end
return true
end
return true
end
Post automatically merged:

local function swapCreature(cid, newMonster)
local position = getCreaturePosition(cid)
doRemoveCreature(cid)
addEvent(doCreateMonster, 0, newMonster, position)
end

function onSpawn(cid)
local rand = math.random(1,5)
if rand == 5 then
print("Spawned monster - Name: ".. getCreatureName(cid))
if getCreatureName(cid) == "Rat" then
addEvent(swapCreature, 0, cid, "Rat_elite")
end
return true
end
return true
end
Try it
 
Back
Top