How can I spawn a monster randomly?
How can I spawn a monster randomly?
I need to spawn the same monster type randomly, can some one help me with the script/solution?
If we do not end the war, the war will end us.
- cromcrom
- Posts: 549
- Joined: Tue Sep 11, 2012 7:16 am
- Location: Chateauroux in a socialist s#!$*&% formerly known as "France"
Re: How can I spawn a monster randomly?
What kind of random, time or location ?
A trip of a thousand leagues starts with a step.
Re: How can I spawn a monster randomly?
Time and location if possiblecromcrom wrote:What kind of random, time or location ?
If we do not end the war, the war will end us.
- cromcrom
- Posts: 549
- Joined: Tue Sep 11, 2012 7:16 am
- Location: Chateauroux in a socialist s#!$*&% formerly known as "France"
Re: How can I spawn a monster randomly?
try this for location:
Tie it to a timer, choose the check() function
What it does is that it spawns a monster (objectToSpawn) on a random tile that is within a + or - range (Xrange, Yrange) of the tile this script_entity is located at. Beware of the spawn level.
I am sure it is easy to improve it.
As for time, I have not checked this issue yet, but out of my mind, you could set a percentage chance that this script fires every "timer second", thus making the trick (example, timer seconds = 10, if percentage = 50%, the monster might or might not spawn every 10 seconds. You could reduce the percentage for less spawning chance. Well, you got the figure...). I must go to work now, I will check this stuff in a while,
Code: Select all
function spawnInRange(objectToSpawn,Xrange,Yrange)
local coordinateX = self.x
local coordinateY = self.y
local basecoordinateX = coordinateX-Xrange
local basecoordinateY = coordinateY-Yrange
local maxSpawnX = Xrange*2
local maxSpawnY = Yrange*2
local randomCoordinateX = math.random(0,maxSpawnX)
local randomCoordinateY = math.random(0,maxSpawnY)
local finalCoordinateX = basecoordinateX + randomCoordinateX
local finalCoordinateY = basecoordinateY + randomCoordinateY
spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0)
print(finalCoordinateX,finalCoordinateY)
end
function check()
spawnInRange("snail",3,3)
endWhat it does is that it spawns a monster (objectToSpawn) on a random tile that is within a + or - range (Xrange, Yrange) of the tile this script_entity is located at. Beware of the spawn level.
I am sure it is easy to improve it.
As for time, I have not checked this issue yet, but out of my mind, you could set a percentage chance that this script fires every "timer second", thus making the trick (example, timer seconds = 10, if percentage = 50%, the monster might or might not spawn every 10 seconds. You could reduce the percentage for less spawning chance. Well, you got the figure...). I must go to work now, I will check this stuff in a while,
A trip of a thousand leagues starts with a step.
Re: How can I spawn a monster randomly?
This seems hard to me... seems simple, but i cannot figure out it. Can i send you a PM with the specifics? And maybe mailing you the dungeon with the description of the level?cromcrom wrote:try this for location:
Tie it to a timer, choose the check() functionCode: Select all
function spawnInRange(objectToSpawn,Xrange,Yrange) local coordinateX = self.x local coordinateY = self.y local basecoordinateX = coordinateX-Xrange local basecoordinateY = coordinateY-Yrange local maxSpawnX = Xrange*2 local maxSpawnY = Yrange*2 local randomCoordinateX = math.random(0,maxSpawnX) local randomCoordinateY = math.random(0,maxSpawnY) local finalCoordinateX = basecoordinateX + randomCoordinateX local finalCoordinateY = basecoordinateY + randomCoordinateY spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0) print(finalCoordinateX,finalCoordinateY) end function check() spawnInRange("snail",3,3) end
What it does is that it spawns a monster (objectToSpawn) on a random tile that is within a + or - range (Xrange, Yrange) of the tile this script_entity is located at. Beware of the spawn level.
I am sure it is easy to improve it.
As for time, I have not checked this issue yet, but out of my mind, you could set a percentage chance that this script fires every "timer second", thus making the trick (example, timer seconds = 10, if percentage = 50%, the monster might or might not spawn every 10 seconds. You could reduce the percentage for less spawning chance. Well, you got the figure...). I must go to work now, I will check this stuff in a while,
If we do not end the war, the war will end us.
- cromcrom
- Posts: 549
- Joined: Tue Sep 11, 2012 7:16 am
- Location: Chateauroux in a socialist s#!$*&% formerly known as "France"
Re: How can I spawn a monster randomly?
Ask your questions, I might or might not have the answer, and it might benefit others 
A trip of a thousand leagues starts with a step.
Re: How can I spawn a monster randomly?
I had some random spawning in my mod. You can load the source files here: http://www.grimrock.net/extras/rotten_place_source.zip
I basically went through all the spawner-entities (with allEntities-iterator) in the level and used a random chance to determine with each of them if they are activated.
I basically went through all the spawner-entities (with allEntities-iterator) in the level and used a random chance to determine with each of them if they are activated.
Steven Seagal of gaming industry
- cromcrom
- Posts: 549
- Joined: Tue Sep 11, 2012 7:16 am
- Location: Chateauroux in a socialist s#!$*&% formerly known as "France"
Re: How can I spawn a monster randomly?
It seems a very galant way to do this random spawn indeed 
I post the modified function anyways, if it might help:
How to use:
1- copy paste this code in a script_entity on the level to check.
2- in the check function, change the spawnInRandomRangeChanceTime variables to whatever you want.
3- add a timer, set it to "XX_seconds", and link it to the script_entity, choose the "check" function
In the example above, it will have 15% chance to spawn a snail within 1 tile of the script_entity every "XX_seconds"
I post the modified function anyways, if it might help:
Code: Select all
function spawnInRandomRangeChanceTime(objectToSpawn,Xrange,Yrange,ChanceTime)
local coordinateX = self.x
local coordinateY = self.y
local basecoordinateX = coordinateX-Xrange
local basecoordinateY = coordinateY-Yrange
local maxSpawnX = Xrange*2
local maxSpawnY = Yrange*2
local randomCoordinateX = math.random(0,maxSpawnX)
local randomCoordinateY = math.random(0,maxSpawnY)
local finalCoordinateX = basecoordinateX + randomCoordinateX
local finalCoordinateY = basecoordinateY + randomCoordinateY
local chancetospawn = math.random(1,100)
if chancetospawn < ChanceTime then
spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0)
--print(finalCoordinateX,finalCoordinateY)
end
end
function check()
spawnInRandomRangeChanceTime("snail",1,1,15)
end1- copy paste this code in a script_entity on the level to check.
2- in the check function, change the spawnInRandomRangeChanceTime variables to whatever you want.
3- add a timer, set it to "XX_seconds", and link it to the script_entity, choose the "check" function
In the example above, it will have 15% chance to spawn a snail within 1 tile of the script_entity every "XX_seconds"
A trip of a thousand leagues starts with a step.
Re: How can I spawn a monster randomly?
how to avoid random spawning inside the walls?
Finished Dungeons - complete mods to play
Re: How can I spawn a monster randomly?
Oooooh Ooooooh!!! Setup!!!Komag wrote:how to avoid random spawning inside the walls?
They're coming outta the goddamn walls!
Daniel.
A gently fried snail slice is absolutely delicious with a pat of butter...

