Counting objects

Ilmiasu
Hello, I'm new to these forums and to Quest. I have done the tutorial, and spent some time becoming familiar, with the program, but there are still a lot I don't know. Now I have a problem, which I'm not sure how to solve or have found a solution to, from this forum.

Basically I'm trying to have the player fight the enemy objects inside a room, without being able to leave until the enemies are defeated. I know how to stop the player from moving by adjusting the "go" command and I have a script for the fighting.

The problem is how do I make the fighting script recognise, that all the enemies in the room are defeated? So I need a script that checks how many enemies are present in the room. How do I do that? Suggestions and advise would be most appreciated.

HegemonKhan
That's great that you went through the tutorial (it really does help a lot).

A good next step in learning, is to get fully competent in the:

character creation ( http://docs.textadventures.co.uk/quest/ ... ation.html )

after that, then slowly start on the various other guides, and get familiar with these links too:

Quest's 'Code Bible' links:

http://docs.textadventures.co.uk/quest/

http://docs.textadventures.co.uk/quest/ ... notes.html

http://docs.textadventures.co.uk/quest/tutorial/
http://docs.textadventures.co.uk/quest/guides/
viewforum.php?f=18

http://docs.textadventures.co.uk/quest/ ... quest.html
http://docs.textadventures.co.uk/quest/ ... ments.html

http://docs.textadventures.co.uk/quest/elements/
http://docs.textadventures.co.uk/quest/types/

http://docs.textadventures.co.uk/quest/functions/ (category order)
http://docs.textadventures.co.uk/quest/ ... tions.html (alphabetical order)
http://docs.textadventures.co.uk/quest/scripts/

http://docs.textadventures.co.uk/quest/ ... lists.html
http://docs.textadventures.co.uk/quest/ ... aries.html
http://docs.textadventures.co.uk/quest/ ... reach.html
http://docs.textadventures.co.uk/quest/scripts/for.html

http://docs.textadventures.co.uk/quest/scopes.html
under the Function section (see links above), the: 'Alls', 'Gets', 'Scopes', and 'Contains'

-----------

as for what you want specifically:

Ilmiasu wrote:Hello, I'm new to these forums and to Quest. I have done the tutorial, and spent some time becoming familiar, with the program, but there are still a lot I don't know. Now I have a problem, which I'm not sure how to solve or have found a solution to, from this forum.

Basically I'm trying to have the player fight the enemy objects inside a room, without being able to leave until the enemies are defeated. I know how to stop the player from moving by adjusting the "go" command and I have a script for the fighting.

The problem is how do I make the fighting script recognise, that all the enemies in the room are defeated? So I need a script that checks how many enemies are present in the room. How do I do that? Suggestions and advise would be most appreciated.


(there's many ways to do what you want, here's hopefully the best~simpliest way for you, lol ~ I'm still a noob myself at coding)

Conceptually:

1. you need a way for quest to identify your 'enemy' Objects, vs the 'non-enemy' Objects
2. to check if you got all of the 'enemy' Objects killed, in order to (or before able to) leave the room

------------

for #1:

(for these examples, I'll be using my own labeling system~convention, you can of course call~name~label the stuff however you want)

if you want a simple and static 'monster~enemy vs npc~townsperson' system, then we can just use a Boolean (a true~false flag) Attribute

--- if you want something else, such as dynamic system (they can change between friend and foe), then let me know!

'whatever' Object -> Attributes Tab -> Attributes (NOT Status Attributes) -> Add -> (set it up, see below, repeat as needed for each Object and~or Attributes)

An Enemy Object:
(Object name: whatever)
Attribute Name: hostile_boolean
Attribute Type: boolean
Attribute Value: true

A Non-Enemy Object:
(Object name: whatever)
Attribute Name: hostile_boolean
Attribute Type: boolean
Attribute Value: false

------

in-code, it'd look like this (an example):

Object_name.Attribute_name = Value_or_Expression

non-enemy:

inn_keeper.hostile_boolean = false

enemy:

orc.hostile_boolean = true

-------

for #2:

http://docs.textadventures.co.uk/quest/ ... arent.html

Object_name.Attribute_name = Value_or_Expression
(moving Object).parent = (destination Object)
example: player.parent = room

http://docs.textadventures.co.uk/quest/ ... reach.html
http://docs.textadventures.co.uk/quest/ ... ldren.html
http://docs.textadventures.co.uk/quest/ ... olean.html
http://docs.textadventures.co.uk/quest/ ... t_add.html
http://docs.textadventures.co.uk/quest/ ... count.html

and this will be in code (ask if you need help with implementing the code into your game), as I'm not familiar with using the GUI~Editor...

(also, this code below, requires that you either remove the 'enemy' Object when it's dead from the room or to change it's Boolean Attribute's 'hostile_boolean' Value to being 'false', otehrwise, if you want we can implement a 'dead' condition, either as a String Attribute or a Boolean Attribute, and change the code for it)

(the below though is jsut a sample code, we can change it to what+how you want done in your game, and I will gladly explain and help you through it's implementation into your game)

// A Scripting location:
// check_for_zero_enemies_in_order_to_leave_room_function (player.parent)

<function name="check_for_zero_enemies_in_order_to_leave_room_function" parameters="room_parameter" type="boolean">
foreach (object_variable, GetDirectChildren (room_parameter) ) {
if (GetBoolean (object_variable, "hostile_boolean") {
list add (enemy_objectlist_variable, object_variable)
}
}
on ready {
if (ListCount (enemy_objectlist_variable) = 0) {
// script that enables you to leave the room
} else {
// script that prevents you from leaving the room
}
}
</function>


this is a bit advancd if you're new to coding, and~or to quest's coding.

------------

Conceptually what is going on (what the code is doing):

------------

check_for_zero_enemies_in_order_to_leave_room_function (player.parent)
<function name="check_for_zero_enemies_in_order_to_leave_room_function" parameters="room_parameter" type="boolean">
foreach (object_variable, GetDirectChildren (room_parameter) ) {


it's going through all of the Objects in the specified room ( 'player.parent' ===> 'room_parameter' ), and will apply the following scripts below to each~every~all of the Objects (object_variable) in the specified room

-----------

    if (GetBoolean (object_variable, "hostile_boolean") {
list add (enemy_objectlist_variable, object_variable)
}


if any of the Objects (object_variable), have the 'hostile_boolean' Boolean Attribute AND it's Value is 'true', then adds it to a new (temporary) objectlist (enemy_objectlist_variable).

-----------

if (ListCount (enemy_objectlist_variable) = 0) {
// script that enables you to leave the room
} else {
// script that prevents you from leaving the room
}


this checks the quantity of Objects in the temporary objectlist (enemy_object_variable), and if it is 0~ZERO, then it'll run the script needed to let you leave the room, else, it runs a script preventing you from leaving the room.

Silver
Set an integer reflecting the amount of enemies in the room.

roomname.enemies = 5


Then when an enemy is defeated add to that script

roomname.enemies = roomname.enemies - 1


Then you probably need a room specific turn script firing checking on the state of battle with something like

if (roomname.enemies = 0) {
// run all enemies defeated script
}

Ilmiasu
Thank you HegemonKhan for comprehensive answer and the example code. After some trial and error I managed to implement the code to my work in progress.I hadn't even noticed that object lists existed before this just string lists.

I did know about the Quest 'Code Bible'. It's useful, but I wasn't sure what I was looking for or how to put it in action.

Thanks also to Silver for the suggestion, I actually considered it as a solution before posting, but I wanted a more dynamic system, in case I want to move the enemy objects around.

HegemonKhan
amazing job getting it to work for your game!, considering I've got quite a few mistakes and unfinished code in my post, HK looks the other way and whistles... didn't spot them until now... meh... I was tired... didn't catch all of my mistakes, lol...

-----

if you like~need, I can help with crafting code that is more specific to your game and what you want, as I was just posting a generalized example, but you'd use a bit different code design, depending on exactly how and what you want done in your game. As there's many many many many different code designs~methods~ways of~for doing things.

though, I'd need you to give those details and~or also need to see your entire game code, to tailor a code specifically for what and how you want it for your game.

-----

and ya...

for Lists:

String Lists
Object Lists

for Dictionaries:

String Dictionaries
Object Dictionaries
Script Dictionaries

----

and ya...

Ilmiasu wrote:I did know about the Quest 'Code Bible'. It's useful, but I wasn't sure what I was looking for or how to put it in action.


that is the problem... hard to find what you want, when you don't know what you want or what it is that you need to find, hehe

what can kinda help... just look through everything (usually the 'Functions' or 'Scripts' for any actions you want done), and look for anything that sounds~seems like it may be what you need. That's what I do... when I don't know what I want~need, just try looking through everything, and seeing if anything seems like it may be what you need, and if it isn't, you at least are slowly learning all of the things that quest can do, which you may need to use later on for something else, hehe.

otherwise, just post~ask us for help, as we're all happy to help with any questions!

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums