Check container for max items

tbritton
06 Oct 2013, 22:46
I've added a put verb and as part of the ifs need to check if a limited container is at max items. Been through the functions and can't seem to find anything. Obviously there is though since the put command makes this test.

HegemonKhan
06 Oct 2013, 23:49
Here's how I found this out:

I started up a new game, created an Object (name: box) in the default Room Object (name: room).

room (Object, Type: Room) -> Objects (Tab) -> Add ->

name: box

box (Object) -> Container (Tab) -> Container Type -> Choose: Limited Container -> save file~game -> Code View mode (button at the top of the screen, a horizontal bar with a bunch of buttons, it looks like a notepaper, it's between the "play" and ?~help" buttons, press it to go into Code View mode, and within Code View mode, simply press the button again to go back to the GUI~Editor mode, as it is a toggle button between the two modes)

Maximum number of objects box: 1

Maximum volume of objects box: 100

in Code View mode (or just in Code):

<object name="box">
<inherit name="editor_object" />
<inherit name="container_limited" />
<maxobjects type="int">1</maxobjects>
<maxvolume type="int">100</maxvolume>
</object>


so, here are the names of the attributes (and their Type: Integer ~ int) that you want:

maxobjects
maxvolume

so, in your verb, you do this:

Run as a script -> Add a~new script -> Scripts -> If... ->

if (script) {
-> // then do this script
}

-> Add a~new script -> Variables -> Set a variable or attribute ->

Object.Attribute=Value
name_of_object.name_of_attribute=your_amount

so, it'd look like this:

Run as a script -> Add a~new script -> Scripts -> If... ->
-> Add a~new script -> Variables -> Set a variable or attribute ->

so, for example (using mine), in code it'd look like this:

if (box.maxobjects=50) {
-> // whatever script
}

and~or

if (box.maxvolume=50) {
-> // whatever script
}

type in "name_of_object.name_of_attribute" to the left of the equal sign, and type in "your_amount" to the right of the equal sign, do NOT type in the "=" sign, as it is already there within~from~on the window itself.

so for my example, I'd type in "box.maxobjects" (and~or with another added script: "box.maxvolume") on the left side and "50" on the right side. You can use any amount you want, I chose "50". Also, don't type in the quatation marks, I just use them to "highlight" the name or code syntax that you type in from the rest of the sentence.

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

you can actually do them in the same script too (you don't have to do~add two scripts):

in GUI~Editor:

Run as a script -> Add a~new script -> Scripts -> If... ->

if (script) {
-> // then, do this script
}

-> Add a~new script -> Variables -> Set a variable or attribute ->

box.maxobjects=25 and box.maxvolume=50

left side of the window's equal sign: box.maxobjects
right side of the window's equal sign: 25 and box.maxvolume=50

~OR~

box.maxobjects=25 or box.maxvolume=50

left side of the window's equal sign: box.maxobjects
right side of the window's equal sign: 25 or box.maxvolume=50

in Code:

if (box.maxobjects=25 and box.maxvolume=50) { // if both are equal to their amounts,
-> // then do this script...
}

~OR~

if (box.maxobjects=25 or box.maxvolume=50) { // if either one equals its amount,
-> // then do this script...
}

also note for applying "not", just type in "not" before the object, for one example:

if (not box.maxobjects=25 and box.maxvolume=50) { // if maxobjects does NOT equal 25 and if maxvolume equals 50,
-> // then do this script...
}

another example:

if (not box.maxobjects=25 and not box.maxvolume=50) { // if maxobjects does NOT equal 25 and if maxvolume does NOT equals 50,
-> // then do this script...
}

do note, this is the other combination possible as well:

if (box.maxobjects=25 and not box.maxvolume=50) { // if maxobjects equal 25 and if maxvolume does NOT equals 50,
-> // then do this script...
}

and then, there's the use of "or" with the "3" (if you're using only 2 Object.Attribute=Value 's, you can use as many as you want... though that's going to be one long line... lol) combinations of "not", too.

here's a quick example:

if (global_data_object.month_string = january or global_data_object.month_string = march or global_data_object.month_string = may or global_data_object.month_string = july or global_data_object.month_string = august or global_data_object.month_string = october or global_data_object.month_string = december) {
global_data_object.days_in_the_month=31
}


it'd be nice if it could recognize multiple results, lol:

if (global_data_object.month_string = january or march or may or july or august or october or december) {
global_data_object.days_in_the_month=31
}


well... you could probably make a stringlist (of all 12 months), and then do, "foreach -> if -> contains", I think, but heh... lol

tbritton
07 Oct 2013, 01:37
Thanks for the detailed response, I think it gets me half way there. Let me explain the issue in more detail since I'm not trying to set max objects, but determine if the number of objects in the container equals max objects.

I have an apple an orange and a bowl. The bowl is a container and has max objects set at 1. I've added a put verb for the orange and apple. The problem is the links (verb) will allow me to put two items in a container that has max objects set at 1 (not surprising since I'm not keeping it from doing that in the verb script).

But if you do it via the command line, after one object is put in and you try the second one it says the bowl is full. So clearly the command put is checking if the max objects has been already met and then not allowing anything else to be added to the bowl. I need to add the same condition to the verb.

As you explained I would do if (bowl.maxobjects = Quantity-Of-Objects-In-Bowl) { ... } else {...}. The problem is I can't figure out how to identify how many objects are currently in the bowl. I've been through all of the bowl attributes and the count doesn't appear there so I would assume its a function or script, but I've read through them all and can't find it.

Hope that makes more sense.

HegemonKhan
07 Oct 2013, 02:14
ah my bad, and your question is simple to answer (let's see if you can figure this out on your own, hehe), here's the other half of info that I didn't give you:

http://quest5.net/wiki/ListCount
http://quest5.net/wiki/Objectlist
http://quest5.net/wiki/Using_Lists
http://quest5.net/wiki/ObjectListItem
http://quest5.net/wiki/Category:Scopes

if, you need help, then please ask, and I'll help you! :D

---------

the "Quest Coding Bible" links:

http://quest5.net/wiki/Category:All_Fun ... t_Commands (page 1, range: A-S)

http://quest5.net/w/index.php?title=Cat ... h#mw-pages (page 2, range: S-Z)

seriously, this is extremely useful! :D

---------

and here's an excellent library for you:

Sora's Stackable Library ( viewtopic.php?f=18&t=3515 )

enjoy! :D

HegemonKhan
07 Oct 2013, 02:48
here's some code for you to look at (it uses strings ~ stringlists and stringdictionaries, but it has similiar features you want to use with your objects):

(go ahead and play it out, hopefully it works and is also my most up-to-date version, lol, and that it's not missing some stuff)

Exploration and Travel Feature Coding:

Get help, via typing in: help
(I forgot to add a message saying to type in "help", via creating~adding the "start" script in the Game Object. My apologies!)
Discover the areas first, via typing in: explore
Travel to the areas, via typing in: travel

<asl version="540">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="Testing Game Stuff">
<gameid>eef801a1-4e6b-4b0a-bdbf-8f3ecfa8389c</gameid>
<version>1.0</version>
<firstpublished>2013</firstpublished>
<turns type="int">0</turns>
<statusattributes type="simplestringdictionary">turns=</statusattributes>
</game>
<object name="homeland">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
</object>
<object name="grassland">
<inherit name="editor_room" />
</object>
<object name="plains">
<inherit name="editor_room" />
</object>
<object name="desert">
<inherit name="editor_room" />
</object>
<object name="tundra">
<inherit name="editor_room" />
</object>
<object name="swampland">
<inherit name="editor_room" />
</object>
<object name="mountains">
<inherit name="editor_room" />
</object>
<object name="forest">
<inherit name="editor_room" />
</object>
<object name="wasteland">
<inherit name="editor_room" />
</object>
<object name="coastland">
<inherit name="editor_room" />
</object>
<object name="hills">
<inherit name="editor_room" />
</object>
<command name="help_command">
<pattern>help</pattern>
<script>
help_function
</script>
</command>
<command name="explore_command">
<pattern>explore</pattern>
<script>
explore_function
</script>
</command>
<command name="travel_command">
<pattern>travel</pattern>
<script>
travel_function
</script>
</command>
<object name="data_object">
<inherit name="editor_object" />
<travel_string_list type="simplestringlist">homeland</travel_string_list>
<homeland_events_string_list type="simplestringlist">grassland_discovery;plains_discovery;desert_discovery;tundra_discovery;swampland_discovery;forest_discovery;mountains_discovery;hills_discovery;wasteland_discovery;coastland_discovery</homeland_events_string_list>
<homeland_events_script_dictionary type="scriptdictionary">
<item key="grassland_discovery">
list add (data_object.travel_string_list, "grassland")
msg ("You've discovered the grassland! Now, you can travel to the grassland and explore it!")
</item>
<item key="plains_discovery">
list add (data_object.travel_string_list, "plains")
msg ("You've discovered the plains! Now, you can travel to the plains and explore it!")
</item>
<item key="desert_discovery">
list add (data_object.travel_string_list, "desert")
msg ("You've discovered the desert! Now, you can travel to the desert and explore it!")
</item>
<item key="tundra_discovery">
list add (data_object.travel_string_list, "tundra")
msg ("You've discovered the tundra! Now, you can travel to the tundra and explore it!")
</item>
<item key="swampland_discovery">
list add (data_object.travel_string_list, "swampland")
msg ("You've discovered the swampland! Now, you can travel to the swampland and explore it!")
</item>
<item key="forest_discovery">
list add (data_object.travel_string_list, "forest")
msg ("You've discovered the forest! Now, you can travel to the forest and explore it!")
</item>
<item key="mountains_discovery">
list add (data_object.travel_string_list, "mountains")
msg ("You've discovered the mountains! Now, you can travel to the mountains and explore it!")
</item>
<item key="hills_discovery">
list add (data_object.travel_string_list, "hills")
msg ("You've discovered the hills! Now, you can travel to the hills and explore it!")
</item>
<item key="wasteland_discovery">
list add (data_object.travel_string_list, "wasteland")
msg ("You've discovered the wasteland! Now, you can travel to the wasteland and explore it!")
</item>
<item key="coastland_discovery">
list add (data_object.travel_string_list, "coastland")
msg ("You've discovered the coastland! Now, you can travel to the coastland and explore it!")
</item>
</homeland_events_script_dictionary>
</object>
<turnscript name="global_turnscript">
<enabled />
<script>
game.turns = game.turns + 1
</script>
</turnscript>
<function name="help_function">
msg ("Type 'explore' to explore your area.")
msg ("Type 'travel' to travel to different areas.")
</function>
<function name="explore_function"><![CDATA[
switch (game.pov.parent) {
case (homeland) {
result_1 = ListCount (data_object.homeland_events_string_list) - 1
if (result_1 >= 0) {
result_2 = StringListItem (data_object.homeland_events_string_list,GetRandomInt(0,result_1))
invoke (ScriptDictionaryItem (data_object.homeland_events_script_dictionary,result_2))
on ready {
foreach (item_x, split ("grassland_discovery;plains_discovery;desert_discovery;tundra_discovery;swampland_discovery;forest_discovery;mountains_discovery;hills_discovery;wasteland_discovery;coastland_discovery",";")) {
if (result_2 = item_x) {
list remove (data_object.homeland_events_string_list, result_2)
}
}
}
} else {
msg ("There seemingly is nothing left to explore in this area.")
}
}
}
]]></function>
<function name="travel_function">
show menu ("Where do you wish to travel?",data_object.travel_string_list,false) {
if (not game.pov.parent = GetObject (result)) {
game.pov.parent = GetObject (result)
} else {
msg ("You are already at this area.")
ask ("Try again?") {
if (result=true) {
travel_function
} else {
msg ("You realize that you need to discover a new area to travel to first, before you can travel to that place.")
}
}
}
}
</function>
</asl>

tbritton
07 Oct 2013, 03:16
Thanks HegemonKhan. I'll play with it in the next couple of days and let you know.

HegemonKhan
07 Oct 2013, 03:20
about the item count, you can also do this too:

in your "put" verb, just make an "adder" (addition) script:

(can you make a "GetObject" function and~or find it in the "put" Verb or Command for your "destination Object"?)
(if you're putting objects onto your Player Object, than you don't need a "GetObject" function)
(otherwise, you'll have to code~type in each individual Object's Verb script...)

name_of_your_destination_object.object_count = name_of_your_destination_object.object_count + 1

and create (add) the "object_count" (Type: integer, Value: 0 ~ or whatever start amount equal to objects) attribute for that destination object too of course.

------

<![CDATA[
-> if (name_of_your_destination_object.object_count < name_of_your_destination_object.maxobjects) {
->-> // MoveObject script
->-> name_of_your_destination_object.object_count = name_of_your_destination_object.object_count + 1
-> } else {
->-> msg ("the destination object has no more room for storage")
-> }
]]>

jaynabonne
07 Oct 2013, 08:51

The problem is I can't figure out how to identify how many objects are currently in the bowl. I've been through all of the bowl attributes and the count doesn't appear there so I would assume its a function or script, but I've read through them all and can't find it.



If you want to know how many objects a container has, then do this:

count = ListCount(GetDirectChildren(container))

Pertex
07 Oct 2013, 15:00
Could you post your put-verb here, especially the script?

tbritton
07 Oct 2013, 21:43
Thanks guys, should be able to get it working with the info provided. I'll post results here in case anyone else is interested.

Pertex, not really much in the object's put verb at the moment. Following is the info for the object. I haven't made any changes to the default put verb the system adds to verbs.

<put type="scriptdictionary">
<item key="red_bowl">
msg ("You put the apple in the red bowl.")
MoveObject (apple, red_bowl)
</item>
<item key="orange_bowl">
msg ("The apple doesn't belong in the orange bowl.")
</item>
</put>


I copied the put command into the game and added the following.

else if (object1 = apple and object2 = red_bowl) {
msg ("You put the apple in the red bowl.")
MoveObject (apple, red_bowl)
}
else if (object1 = apple and object2 = orange_bowl) {
msg ("The apple doesn't belong in the red bowl.")
}
else if (object1 = orange and object2 = orange_bowl) {
msg ("You put the orange in the orange bowl.")
MoveObject (orange, orange_bowl)
}
else if (object1 = orange and object2 = red_bowl) {
msg ("The orange doesn't belong in the red bowl.")
}

tbritton
07 Oct 2013, 23:04
Got it working. Thanks a lot for everyone's help. Following is the code in case anyone else has the same issue. It could be more optimized, but I have to copy and paste part of the script to the put command, but not all of it, since the put command checks for open/closed and maxobjects. This way makes that easier.

Am I the only one who finds containers and the put verb to be cumbersome. Wish they worked like use and give. It would also be a lot easier to cover all the" put object1 in object2" variations the user might come up with if the equivalent of giveanything and givetoanything where available for put. Anyways, I've now learned to limit my use of containers.

      <put type="scriptdictionary">
<item key="red_bowl">
if (HasAttribute(red_bowl, "maxobjects")) {
count = ListCount(GetDirectChildren(red_bowl))
max_count = red_bowl.maxobjects
}
else {
count = 1
max_count = 2
}
if (count = max_count) {
msg ("The bowl is full.")
}
else {
if (red_bowl.isopen) {
msg ("You put the apple in the red bowl.")
MoveObject (apple, red_bowl)
}
else {
msg ("The bowl isn't open.")
}
}
</item>
<item key="orange_bowl">
if (HasAttribute(orange_bowl, "maxobjects")) {
count = ListCount(GetDirectChildren(orange_bowl))
max_count = orange_bowl.maxobjects
}
else {
count = 1
max_count = 2
}
if (count = max_count) {
msg ("The bowl is full.")
}
else {
if (orange_bowl.isopen) {
msg ("You put the apple in the orange bowl.")
MoveObject (apple, orange_bowl)
}
else {
msg ("The bowl isn't open.")
}
}
</item>
</put>


In the attached file the red bowl is a limited container with maxobjects of 1 and the orange bowl is a standard container.