• 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 for one monster

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
How can you assign onSpawn function to one monster? Specifically in creaturescripts like this:

XML:
<script>
    <event name="myScript"/>
</script>

I have something similar in mind to onPrepareDeath, i.e. onPrepareSpawn. It checks if certain item is in the area, if so; prevents spawn of the monster
 
Solution
The spawning for lua monsters works exactly the same as for xml monsters, what he meant was, that you just have to edit the monsters.xml file for your rme and include the name of the monster there.
That way you can place it normaly in rme and it'll be spawned just correct in your server.
why not just use something like:

Lua:
if self:getName():lower() == 'demon' then
--code here
end

The current problem with onSpawn function is that it's going to affect all monsters, wouldn't it be better to call the function once with regards to performance? That is, if you have one unique monster on your server out of 15,000 total monsters.
 
The current problem with onSpawn function is that it's going to affect all monsters, wouldn't it be better to call the function once with regards to performance? That is, if you have one unique monster on your server out of 15,000 total monsters.

Then ignore using onSpawn.
Just use:

XML:
<script>
    <event name="myScript"/>
</script>

To call the script you want to use for this one monster.
 
Why not port your monster to revscript if you wanna use singular onSpawn with it from revscript?
onSpawn is an Event, so it cannot be bound to a single monster as a CreatureEvent.

However you could use onCreatureAppear in this case which does exactly what you want.
Take a look at data/monsters/lua/#example.lua to see what I mean.
 
Then ignore using onSpawn.
Just use:

XML:
<script>
    <event name="myScript"/>
</script>

To call the script you want to use for this one monster.

What would be the event type in creaturescripts? Every event you reference in monster XML spec has to be in creaturescripts folder, right?

Why not port your monster to revscript if you wanna use singular onSpawn with it from revscript?


🤔


onSpawn is an Event, so it cannot be bound to a single monster as a CreatureEvent.

However you could use onCreatureAppear in this case which does exactly what you want.
Take a look at data/monsters/lua/#example.lua to see what I mean.

That file is all in Lua, how do you make it to work with XML
🧐🧐
 
You don't, because that's how you create Monsters completly in lua instead of xml.

Wait what, how do you add the following to map editor then:

Lua:
local mType = Game.createMonsterType("Energy Cat")
local monster = {}
monster.description = "an energy cat"
monster.experience = 1
monster.outfit = {
    lookType = 37
}

monster.health = 99200
monster.maxHealth = monster.health
monster.race = "energy"
monster.corpse = 5995
monster.speed = 280
monster.maxSummons = 2

monster.changeTarget = {
    interval = 4*1000,
    chance = 20
}

monster.flags = {
    summonable = false,
    attackable = true,
    hostile = true,
    convinceable = false,
    illusionable = false,
    canPushItems = true,
    canPushCreatures = true,
    targetDistance = 1,
    staticAttackChance = 70
}

monster.summons = {
    {name = "smol cat", chance = 10, interval = 2*1000}
}

monster.voices = {
    interval = 5000,
    chance = 10,
    {text = "I'm an example", yell = false},
    {text = "You shall bow", yell = false}
}

monster.loot = {
    {id = "gold coin", chance = 60000, maxCount = 100},
    {id = "bag", chance = 60000,
        child = {
            {id = "platinum coin", chance = 60000, maxCount = 100},
            {id = "crystal coin", chance = 60000, maxCount = 100}
        }
    }
}

monster.attacks = {
    {name = "melee", attack = 130, skill = 70, effect = CONST_ME_DRAWBLOOD, interval = 2*1000},
    {name = "energy strike", range = 1, chance = 10, interval = 2*1000, minDamage = -210, maxDamage = -300, target = true},
    {name = "combat", type = COMBAT_MANADRAIN, chance = 10, interval = 2*1000, minDamage = 0, maxDamage = -120, target = true, range = 7, effect = CONST_ME_MAGIC_BLUE},
    {name = "combat", type = COMBAT_FIREDAMAGE, chance = 20, interval = 2*1000, minDamage = -150, maxDamage = -250, radius = 1, target = true, effect = CONST_ME_FIREAREA, shootEffect = CONST_ANI_FIRE},
    {name = "speed", chance = 15, interval = 2*1000, speed = -700, radius = 1, target = true, duration = 30*1000, effect = CONST_ME_MAGIC_RED},
    {name = "firefield", chance = 10, interval = 2*1000, range = 7, radius = 1, target = true, shootEffect = CONST_ANI_FIRE},
    {name = "combat", type = COMBAT_LIFEDRAIN, chance = 10, interval = 2*1000, length = 8, spread = 0, minDamage = -300, maxDamage = -490, effect = CONST_ME_PURPLEENERGY}
}

monster.defenses = {
    defense = 55,
    armor = 55,
    {name = "combat", type = COMBAT_HEALING, chance = 15, interval = 2*1000, minDamage = 180, maxDamage = 250, effect = CONST_ME_MAGIC_BLUE},
    {name = "speed", chance = 15, interval = 2*1000, speed = 320, effect = CONST_ME_MAGIC_RED}
}

monster.elements = {
    {type = COMBAT_PHYSICALDAMAGE, percent = 30},
    {type = COMBAT_DEATHDAMAGE, percent = 30},
    {type = COMBAT_ENERGYDAMAGE, percent = 50},
    {type = COMBAT_EARTHDAMAGE, percent = 40},
    {type = COMBAT_ICEDAMAGE, percent = -10},
    {type = COMBAT_HOLYDAMAGE, percent = -10}
}

monster.immunities = {
    {type = "fire", combat = true, condition = true},
    {type = "drown", condition = true},
    {type = "lifedrain", combat = true},
    {type = "paralyze", condition = true},
    {type = "invisible", condition = true}
}

mType.onThink = function(monster, interval)
    print("I'm thinking")
end

mType.onAppear = function(monster, creature)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId())
    end
end

mType.onDisappear = function(monster, creature)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId())
    end
end

mType.onMove = function(monster, creature, fromPosition, toPosition)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId(), fromPosition, toPosition)
    end
end

mType.onSay = function(monster, creature, type, message)
    print(monster:getId(), creature:getId(), type, message)
end

mType:register(monster)

Do you have to assign it to monsters.xml as well?
 
Wait what, how do you add the following to map editor then:

Lua:
--snip

Do you have to assign it to monsters.xml as well?
You can't, RME can't deal with Lua monsters yet. You'd have to make an XML file with just the monsters name & outfit if you wanted to import it in RME.
 
You can't, RME can't deal with Lua monsters yet. You'd have to make an XML file with just the monsters name & outfit if you wanted to import it in RME.

The reason I am asking is, this solution seems ok for individual monsters but for hundreds, it could be difficult to specify all the positions yourself. Also there is a case with the spawn point, in RME you can just set it and this is the zone for your monster. I have not seen it being applied here in this script. Then I would only assume you do not have to?
 
The spawning for lua monsters works exactly the same as for xml monsters, what he meant was, that you just have to edit the monsters.xml file for your rme and include the name of the monster there.
That way you can place it normaly in rme and it'll be spawned just correct in your server.
 
Solution
Back
Top