If statement for whether a container is empty or not

jdpjdpjdp
I'm sure there's some really obvious way to do this that I'm just missing, but I need to set up an "if" statement based on whether or not a container object is empty or has anything in it. It's not a limited container, so the player can put any object and/or multiple objects into it and take them back out again. It doesn't matter what they put in it or how many things, I just need to differentiate between two states: totally empty and not.

And I'm stumped. Help would be greatly appreciated.

Marzipan
I can't test it out right now, but I'm guessing you'd have to get convoluted about it and create an integer attribute on the container object that gets increased whenever an item goes in and decreased when one gets taken out. i think you can go in the library and tack a script onto the 'put' command, though I don't know whether it'd be simpler to just override the whole thing or use verbs or what. Probably depends on how many containers you have too.

Then just have the game check if that integer is set to zero or not to tell if it's empty.

...I swear that all made sense in my head, but no idea if it survived the transition through my keyboard to the screen. :P Hopefully someone with a better idea of what they're talking about can chime in soon.

jdpjdpjdp
I basically did that, with an object counter. It worked on starting empty, and it worked when you added something, but it returned a complete blank after you emptied it again. No error message or anything. What that means I haven't a clue.

HegemonKhan
here:

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

http://docs.textadventures.co.uk/quest/types/null.html

http://docs.textadventures.co.uk/quest/ ... count.html
if (ListCount (Object_name.List_name) = 0) { script } else if (ListCount (Object_name.List_name) > 0) {script}

if you need help, let me know!

-------

and you can take a look at this code, which uses Lists and Dictionaries (it's my old code work, so it's got some bad~poor coding, but it's functional), and study it if you want (and ask if you need any help or got questions):

HK's 'explore' and 'travel' code:

<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>
<start type="script">
msg ("Important Note:")
msg ("Type in: help")
</start>
</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>

Marzipan
Functions, the bane of my existence! :evil:

HegemonKhan
Once, you get them, you'll love them ;)

all Functions are is Macros~Batches (a 'script~scripting basket', aka supports multiple lines of scripting: script blocks), which can be called upon (in code, literally: the_Function's_name), and can also 'Return' Values~Expressions too.

jdpjdpjdp
See, I get all that, but I don't know how to populate the list in the first place. I need to make the list consist of "whatever is in object_name", but how do I do that? If I can clear that one hurdle, I think I can solve the rest...

HegemonKhan
ah...

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

http://docs.textadventures.co.uk/quest/ ... ripts.html
http://docs.textadventures.co.uk/quest/ ... aries.html

basically, you need a List (Stringlist or ObjectList) or also you can use dictionaries too (Stringdictionary, Objectdictionary, or Scriptdictionary):

also, look at the 'Scopes' and the 'Alls' in the wiki ( http://docs.textadventures.co.uk/quest/ ... tions.html )

ah, here is what you want directly:
http://docs.textadventures.co.uk/quest/ ... ldren.html
http://docs.textadventures.co.uk/quest/ ... jects.html
(then use the 'foreach' example below)

game.color_list = split ("red;blue;yellow", ";")
game.my_list = NewStringList ()
foreach (item_x, game.color_list) {
list add (item_x, game.my_list)
}
on ready {
foreach (item_xx, game.my_list) {
msg (item_xx)
// outputs: red
// outputs: blue
// outputs: yellow
}
}

jdpjdpjdp
GetDirectChildren!!!! That's what I needed, and now it's working perfectly! Thank you, thank you, thank you! That was gonna keep me up all night...

HegemonKhan
for generating lists~dictionaries:

'Scopes', 'Alls', 'Gets', 'foreach', 'for', etc etc etc

Marzipan
jdp3, Don't suppose you could upload a sample file for my 'things I'll need to know how to do some day' collection?

I actually understood the function this time, it was the 'foreach' bit that lost me!

HegemonKhan
'foreach' (ALL=EVERY=*EACH*) thing in the list~dictionary, do this script

deja vu.. I've got at least one other post (if not more lol), on exactly this, but it's more detailed than I'm doing this post, lol. But, it'd take too long for me to find it, so I'm writing this post... laughs.

here's a great analogy example:

team = split ("HK;Mars;Jp", ";")
foreach (team_member, team) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Mars.run_laps
-> // outputs: Jp.run_laps
}

'for' ~ too lazy to look it up, so my format~syntax is slightly off... meh

team = split ("HK;Mars;Jp", ";")
for (team, 1, 3) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Mars.run_laps
-> // outputs: Jp.run_laps
}

team = split ("HK;Mars;Jp", ";")
for (team, 1, 2) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Mars.run_laps
}

team = split ("HK;Mars;Jp", ";")
for (team, 1, 1) {
-> team_member.run_laps
-> // outputs: HK.run_laps
}

team = split ("HK;Mars;Jp", ";")
for (team, 3, 3) {
-> team.run_laps
-> // outputs: Jp.run_laps
}

team = split ("HK;Mars;Jp", ";")
for (team, 1, 3, 2) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Jp.run_laps
}

team = split ("HK;Mars;Jp;Bill;Bob", ";")
for (team, 1, 5, 3) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Bill.run_laps
}

team = split ("HK;Mars;Jp;Bill;Bob", ";")
for (team, 1, 5, 4) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Bob.run_laps
}

team = split ("HK;Mars;Jp;Bill;Bob", ";")
for (team, 1, 5, 1) {
-> team_member.run_laps
-> // outputs: HK.run_laps
-> // outputs: Mars.run_laps
-> // outputs: Jp.run_laps
-> // outputs: Bill.run_laps
-> // outputs: Bob.run_laps

jdpjdpjdp
Here. I altered it a bit to be generic, BTW. Don't want to ruin my game before it comes out...

box_list = GetDirectChildren(box)
if (ListCount (box_list) = 0) {
msg ("Here's a message for when the box is empty.")
}
else if (ListCount (box_list) > 0) {
msg ("Here's a message for when the box has stuff in it.")
}


The first line creates a list, named "box_list", and populates it with whatever is inside the object named "box". The If/Then statement covers the two cases I needed -- nothing in the box vs. something in the box -- by checking against the list we just made. I put generic messages as the output for both results, but obviously you can put in any kind of script you'd like.

I didn't delve into the foreach stuff simply because my particular scenario only needed two outcomes, which an if/then statement is plenty good for. If you want multiple different cases, see HK's stuff above.

Hope that helps! And thank you again to HK!

HegemonKhan
with that set up, you can simplify it to this, if you prefer:

box_list = GetDirectChildren(box)
if (ListCount (box_list) = 0) {
msg ("Here's a message for when the box is empty.")
}
else {
msg ("Here's a message for when the box has stuff in it.")
}


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

'foreach' and specified 'for', is for (lol pun) getting stuff from a list~dictionary and doing a script(s) for that stuff

foreach (book, library_all_books) {
-> book.read // read all books in the library
}

foreach (book, library_book_A) {
-> book.read // read all books starting with 'A'
}

--------

'for' ~ specify what things in the list by a range or what things in the list by a range by interval count

again, my syntax~format for 'for' is off, but meh... too lazy to look up how it's done correctly.

1 to 3: Books A
4 to 6: Books B

for (library, 1, 6) {
-> // read all books A and all books B
}

for (library, 1, 4) {
-> // read all books A and 1 book B (#4 book)
}

for (library, 1, 6, 2) {
-> // read books: 1:A, 3:A, 5:B
}

for (library, 1, 6, 3) {
-> // read books: 1A, 4:B
}

for (library, 2, 6, 3) {
-> // read books: 2A, 5:B
}

for (library, 2, 6, 2) {
-> // read books: 2A, 4:B, 6;B

Marzipan
This is a good thread, thanks guys. :)

OurJud
HegemonKhan wrote:[...] all Functions are is Macros~Batches (a 'script~scripting basket', aka supports multiple lines of scripting: script blocks) [...]

Aaaah! Macros~Batches... a 'script~scripting basket', aka supports multiple lines of scripting: script blocks... I thought as much :shock:

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

Support

Forums