Pulling an object from a container at random

afrotoast
Hi guys! Stumbled into another little problem I can't quite wrap my head around.

I've got a "container" object, and I want to make it such that the player can "take from" the container, and will pull out one of "item1", "item2", or "item3" at random.

I have it set up to roll a number from 1 to 3, then pull an item depending on what it rolls.

That worked fine, until I realized that if you pulled "item1" on your first roll, and you rolled 1 again, you'd get the message that you're getting "item1", but because you already have it, nothing happens.

The problem is that the system i have makes it possible for you to land on a dice roll you've gotten before. I'm not sure how to get the container to "roll again" if the item doesn't exist.

I have a few ideas for how it might be fixed but I couldn't figure out if it was possible to do it in quest.

[list]1: have the script pick one item randomly from whatever is inside the container each time. Depending on what it picks it prints a corresponding message (you got item1!)
2: have the script roll again if the item doesn't exist in the container
3: instead of using a dice roll, the script just checks if it contains "item(wildcard)" and pulls one at random.[/list:u]

Again, I was just trying to think logically about how i could make it work, but I don't know if quest has any function like this.

If it matters, I have the container tied to a counter, and it deletes itself after rolling three times. Each time you roll it does "container.stacksize - 1" and if "container.stacksize = 1" when you roll it, it removes itself after giving you the last item.

/: maybe i've been staring at this too long but I can't tell if i'm missing something stupidly obvious here.

HegemonKhan
you can use this Function:

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

and these Functions too:

http://docs.textadventures.co.uk/quest/ ... ldren.html
http://docs.textadventures.co.uk/quest/ ... jects.html

and these Scripts too:

http://docs.textadventures.co.uk/quest/scripts/do.html
(you can use either one, generally they're the same thing)
http://docs.textadventures.co.uk/quest/ ... nvoke.html

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

for an example:

use these GUI~Editor Scripts to do code lines below:

run as script -> add new script -> output -> "print a message" Script -> print [MESSAGE] type_in_what_you_want

run as script -> add new script -> scripts -> 'if' Script -> if [EXPRESSION] type_in_the_rest___see_below*

* but it's too hard to explain what parenthesis to type in, and what parenthesis to not type in... hmm... this is what you'll type in:
not GetDirectChildren(chest1) = null
random_selection = GetRandomInt(1,3)
random_selection = 1
Contains(chest1, sword1)
etc etc etc

run as script -> add new script -> variables -> 'set a variable or attribute' Script -> (see below)

set variable type_in_the_rest___see_below = [EXPRESSION] type_in_the_rest___see_below

and... I'm not sure how'd you do the 'invoke' (nor 'do' ), in the GUI~Editor... hopefully you can figure this out...

// chest1
// -> sword1
// -> candy1
// -> potion1

// the 'chest' container Object's 'take' Verb's scriptings (add new scripts):

if (not GetDirectChildren(chest1) = null)
{
random_selection = GetRandomInt(1,3)
if (random_selection = 1)
{
if (Contains(chest1, sword1))
{
MoveObject(sword1, player)
msg ("You take the sword from the chest.")
}
else
{
msg ("The sword has already been taken from the chest.")
msg ("You try again to search the chest.")
invoke (chest1.take)
}
}
else if (random_selection = 2)
{
if (Contains(chest1, candy1))
{
MoveObject(candy1, player)
msg ("You take the candy from the chest.")
}
else
{
msg ("The candy has already been taken from the chest.")
msg ("You try again to search the chest.")
invoke (chest1.take)
}
}
else if (random_selection = 3)
{
if (Contains(chest1, potion))
{
MoveObject(potion, player)
msg ("You take the potion from the chest.")
}
else
{
msg ("The potion has already been taken from the chest.")
msg ("You try again to search the chest.")
invoke (chest1.take)
}
}
}
else
{
msg ("The chest is empty.")
}


this is not the best design... but let's not care about that... until we need to, you're just learning the basics right now... we'll deal with better design (code efficiency), later.

afrotoast
Hmm, will it be possible to have the script automatically roll again instead of printing a negative message? It seems weird for the player to "take from container" but then get a message that the item they grabbed has already been taken. If its not possible that's fine as well, I'm sure I can think of a better way to write the whole thing once i take some time away from staring at tiny words and numbers.

HegemonKhan
I just edited my previous post, sorry (I like to edit a lot... annoying for people reading my posts). I included what you just asked about now.

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

a better, but way more advanced, design, would use lists+dictionaries, which you can see an example of here:

be warned though, Lists are quite a step up in understanding (you need to understand Attributes and 'if' Scripting before you should try tackling lists), and dictionaries are even harder.

viewtopic.php?f=18&t=5138

(this is poor code, as this was me learning list+dictionary usage, and at that time I didn't code some parts well, as I over-complicated some parts, I've gotten better, but haven't went back and fixed up this code to be better coded yet)

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

and if you want to try to understand lists+dictionaries:

viewtopic.php?f=18&t=5137

The Pixie
Do this:
objectlist = GetDirectChildren (container)
if (ListCount(objectlist) = 0) {
msg("The container is empty")
}
else {
index = GetRandomInt(0, ListCount(objectlist) - 1)
obj = ObjectListItem(lst, index)
obj.parent = player
msg("You take " + obj.name + " from the container.")
}

First get the list of objects currently in the container. If there is nothing in the container, display a message, otherwise: Pick a number at random, and get that object. Move it to the player, and tell her it happened.

Note that if the player does it a second time, it will select from the contents of the container at that moment, so will only pick items not already removed. The player can add items, and those can be taken at random too.

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

Support

Forums