How to create a random number generator to summon monsters?
MikeOmega
25 Aug 2012, 23:44Hey, I'm back again with another question! Is there a way to create a random number generator in a function? And set it to where if a certain number is picked, it spawns a monster and moves it to the room the player is in?
Say I have three possible monsters that could spawn in a given room, perhaps a wolf, a bear, and a lion, I would like the game to essentially roll a random number of 1-3, and if it rolls 1, the wolf spawns. If 2, the bear spawns, and if 3, the lion spawns. Is there a way to make this happen?
And to expand upon that, perhaps there would be a way to include any random dice rolling function to the game? Say if I wanted to include some dice-based gambling?
This question actually has another part. I need a general code to apply to a function for specific rooms, so when the player in the rooms, a function is called which could potentially apply to all rooms. To better explain, I would like the game to check if the room has a monster in it or not when the player enters. In my game, monsters inherit an object type called "enemy". So what I'm trying to get to happen is when the player enters the room, the script checks to see if there is any object in the room with the inherited object type "enemy". If there is not, then it summons a monster. The monster should remain in the room until slain, even if the player leaves the room. And when the monster is slain and the player leaves the room, it is removed or destroyed. Is this even possible?
Say I have three possible monsters that could spawn in a given room, perhaps a wolf, a bear, and a lion, I would like the game to essentially roll a random number of 1-3, and if it rolls 1, the wolf spawns. If 2, the bear spawns, and if 3, the lion spawns. Is there a way to make this happen?
And to expand upon that, perhaps there would be a way to include any random dice rolling function to the game? Say if I wanted to include some dice-based gambling?
This question actually has another part. I need a general code to apply to a function for specific rooms, so when the player in the rooms, a function is called which could potentially apply to all rooms. To better explain, I would like the game to check if the room has a monster in it or not when the player enters. In my game, monsters inherit an object type called "enemy". So what I'm trying to get to happen is when the player enters the room, the script checks to see if there is any object in the room with the inherited object type "enemy". If there is not, then it summons a monster. The monster should remain in the room until slain, even if the player leaves the room. And when the monster is slain and the player leaves the room, it is removed or destroyed. Is this even possible?

Pertex
26 Aug 2012, 11:42Sure, you could use http://quest5.net/wiki/RandomChance, something like
if (RandomChance(10) ){
//spawn monster
moveobject (monster, player.parent)
}
MikeOmega
26 Aug 2012, 13:07Pertex wrote:Sure, you could use http://quest5.net/wiki/RandomChance, something likeif (RandomChance(10) ){
//spawn monster
moveobject (monster, player.parent)
}
Thanks, Pertex, I don't think this is quite what I meant. I should clarify.
Say I have three possible monsters that could spawn in a given room, perhaps a wolf, a bear, and a lion, I would like the game to essentially roll a random number of 1-3, and if it rolls 1, the wolf spawns. If 2, the bear spawns, and if 3, the lion spawns. Is there a way to make this happen?
And to expand upon that, perhaps there would be a way to include any random dice rolling function to the game? Say if I wanted to include some dice-based gambling?
This question actually has another part. I need a general code to apply to a function for specific rooms, so when the player in the rooms, a function is called which could potentially apply to all rooms. To better explain, I would like the game to check if the room has a monster in it or not when the player enters. In my game, monsters inherit an object type called "enemy". So what I'm trying to get to happen is when the player enters the room, the script checks to see if there is any object in the room with the inherited object type "enemy". If there is not, then it summons a monster. The monster should remain in the room until slain, even if the player leaves the room. And when the monster is slain and the player leaves the room, it is removed or destroyed. Is this even possible?

Pertex
26 Aug 2012, 16:00Aha, you mean something like this:
x=GetRandomInt ( 1,3 )
switch (x) {
case (1) {
//spawn monster 1
}
case (2) {
//spawn monster 2
}
case (3) {
//spawn monster 3
}
}
MikeOmega
26 Aug 2012, 17:06Yes, I think it would definitely be something like that, but is there a way to set it up in Quest's scripting interface? And to have the game check for the presence of a monster in a room?
MikeOmega
27 Aug 2012, 01:43The good news is I have a functioning dice roller thanks to Pertex! The bad news is I still don't know how to have a turnscript which calls a function to check if any given room has an enemy object...
This is as far as I have gotten, but I have no idea how to make the function check for an object which inherits the object type "enemy", otherwise, this would work. The problem is that it keeps repeating "object."
This is as far as I have gotten, but I have no idea how to make the function check for an object which inherits the object type "enemy", otherwise, this would work. The problem is that it keeps repeating "object."
<turnscript name="check_room">
<enabled />
<script>
foreach (object, AllObjects()) {
if (DoesInherit ( object , "editor_room" ) ) {
if (not Contains (object,object)) {
if (player.parent = object) {
dice
}
}
}
}
</script>
</turnscript>

Pertex
27 Aug 2012, 06:47You are thinking a bit to complicated

foreach (object, ScopeVisibleForRoom (player.parent) ()) {
if (DoesInherit ( object , "enemy" ) ) {
dice
}
}
MikeOmega
27 Aug 2012, 10:50No, I don't think that's quite it. I'm not sure if I am saying this correctly.
Here is what I need the turn script to do.
When the player has entered a new room, it checks to see if there are any objects in the room which inherit the object type "enemy". If there are none, it calls the dice function to summon one. But if there is at least one "enemy", it does not call the function.
When I plugged that last one into the code, Pertex, it wasn't functioning.
Here is what I need the turn script to do.
When the player has entered a new room, it checks to see if there are any objects in the room which inherit the object type "enemy". If there are none, it calls the dice function to summon one. But if there is at least one "enemy", it does not call the function.
When I plugged that last one into the code, Pertex, it wasn't functioning.

Pertex
27 Aug 2012, 12:14Perhaps this is better
enemy_here = false
foreach (object, ScopeVisibleForRoom (player.parent)) {
if (DoesInherit ( object , "enemy" ) ) {
enemy_here = true
}
}
if (not enemy_here) {
dice
}
MikeOmega
27 Aug 2012, 13:18Pertex wrote:Perhaps this is betterenemy_here = false
foreach (object, ScopeVisibleForRoom (player.parent)) {
if (DoesInherit ( object , "enemy" ) ) {
enemy_here = true
}
}
if (not enemy_here) {
dice
}
Now this one works! Perfect job, thank you so very much!
However, I have one more problem. How do I set up a general function to remove the "enemy" object when the player leaves the room?

Pertex
27 Aug 2012, 14:48go to the player attributes and select "changedparent". Then click "Make Editable Copy" beneath of it. Now you can find and edit this script in your game file:
Add some code like this:
<changedparent type="script">
if (IsDefined("oldvalue")) {
OnEnterRoom (oldvalue)
}
else {
OnEnterRoom (null)
}
</changedparent>
Add some code like this:
<changedparent type="script">
if (IsDefined("oldvalue")) {
OnEnterRoom (oldvalue)
foreach (object, ScopeVisibleForRoom (oldvalue)) {
if (DoesInherit ( object , "enemy" ) ) {
destroy (object)
// or
MoveObject(object, junk)
}
}
}
else {
OnEnterRoom (null)
}
</changedparent>
}
MikeOmega
27 Aug 2012, 21:54This all seems to work perfectly! Thank you, Pertex!
MikeOmega
27 Aug 2012, 23:03Ok, wait a moment, maybe I was wrong. The code isn't working. When I play and then leave the room with the monster, I get this error...
Error running script: Error compiling expression 'object': RootExpressionElement: Cannot convert type 'Element' to expression result of 'String'
What is going on?
Error running script: Error compiling expression 'object': RootExpressionElement: Cannot convert type 'Element' to expression result of 'String'
What is going on?
sgreig
28 Aug 2012, 08:29I haven't tested this, but here's my idea. Have a turnscript that keeps track of the current room the player is in, then every turn check to see if the player's current room has changed. If it has, remove any objects with the type "enemy" from the previous room. Something along these lines:
in the game object attributes:
turnscript:
All you need to do at this point is create a room to store your monsters when they're not being used, and replace the "room_name_here" in the script with that room name.
in the game object attributes:
game.player_oldrooom = player.parent
turnscript:
if (game.player_oldroom <> player.parent) {
foreach (object, ScopeVisibleForRoom (game.player_oldroom)) {
if (DoesInherit (object, "enemy")) {
MoveObject (object, room_name_here)
}
game.player_oldroom = player.parent
}
}
All you need to do at this point is create a room to store your monsters when they're not being used, and replace the "room_name_here" in the script with that room name.