Login
Remember
IF Answers
Ask a Question

# Placing an object at random

0 votes
55 views
asked Jul 29 in Authoring by anonymous

I've created my adventure world. I can see how to place the objects but I want to have two of them placed in random locations. How do I do this?

- quest

## 1 Answer

0 votes
answered Jul 31 by hegemonkhan (161 points)
edited Jul 31 by hegemonkhan

what you're asking for is quite advanced, and will be difficult for people new to quest, and especially new to coding. However, if you feel you're ready to take on trying to understand this stuff (or you already know programming~coding well), then here it is:

basically, you need a List~Dictionary Attribute to hold your places, so that you can randomly select one of those places to put your Objects into that place.

once you got your List~Dictionary and added your places to it, then you randomly select one of the places within the list, and finally move your Objects into that randomly selected place.

Quest has lots of options for generating such List~Dictionary Attributes:

creating your own List~Dictionary Attribute and adding your Objects (as their names) to them, or using a built-in List Attribute.

useful links~references:

http://docs.textadventures.co.uk/quest/
http://docs.textadventures.co.uk/quest/tutorial/
http://docs.textadventures.co.uk/quest/guides/
http://forum.textadventures.co.uk/viewforum.php?f=18 (more guides: libraries and code samples)

http://docs.textadventures.co.uk/quest/guides/using_lists.html
http://docs.textadventures.co.uk/quest/using_dictionaries.html

http://docs.textadventures.co.uk/quest/functions/string/split.html
http://docs.textadventures.co.uk/quest/functions/newstringlist.html
http://docs.textadventures.co.uk/quest/functions/newobjectlist.html
http://docs.textadventures.co.uk/quest/functions/stringlistitem.html
http://docs.textadventures.co.uk/quest/functions/objectlistitem.html
http://docs.textadventures.co.uk/quest/scripts/list_add.html
http://docs.textadventures.co.uk/quest/scripts/list_remove.html
http://docs.textadventures.co.uk/quest/functions/listcontains.html
http://docs.textadventures.co.uk/quest/functions/listcount.html

http://docs.textadventures.co.uk/quest/functions/newstringdictionary.html
http://docs.textadventures.co.uk/quest/functions/newobjectdictionary.html
http://docs.textadventures.co.uk/quest/functions/newscriptdictionary.html
http://docs.textadventures.co.uk/quest/scripts/dictionary_add.html
http://docs.textadventures.co.uk/quest/scripts/dictionary_remove.html
http://docs.textadventures.co.uk/quest/functions/dictionarycontains.html
http://docs.textadventures.co.uk/quest/functions/dictionarycount.html

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

http://docs.textadventures.co.uk/quest/functions/ (categorical order)
http://docs.textadventures.co.uk/quest/functions/index_allfunctions.html (alphabetical order)
http://docs.textadventures.co.uk/quest/scopes.html
http://docs.textadventures.co.uk/quest/functions/getallchildobjects.html
http://docs.textadventures.co.uk/quest/functions/getdirectchildren.html

and the very lazy way (lol):

http://docs.textadventures.co.uk/quest/functions/allobjects.html

Randomization Methods:

http://docs.textadventures.co.uk/quest/functions/getrandomint.html
http://docs.textadventures.co.uk/quest/functions/getrandomdouble.html
http://docs.textadventures.co.uk/quest/functions/corelibrary/diceroll.html
http://docs.textadventures.co.uk/quest/functions/corelibrary/randomchance.html

A super simple example way (out of infinite ways) of doing it :

create a new Text Adventure game

'Objects' -> 'Objects' Tab -> Add -> (see below, repeat as needed)

    Object Name: red_ball
    Object Type: Object

    Object Name: blue_ball
    Object Type: Object

    Object Name: room2
    Object Type: Room Object

    Object Name: room3
    Object Type: Room Object

    Object Name: room4
    Object Type: Room Object

'game' Game Object -> 'Attributes' Tab -> Attributes -> Add -> (see below)

    (Object Name: game)
    Attribute Name: random_places_list
    Attribute Type: stringlist
    Attribute Value (add, repeat as needed): (see below)

    room2
    room3
    room4

'game' Game Object -> 'scripts' Tab -> 'Start' Script -> (see below)

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

    set variable red_ball.parent = [expression] GetObject (StringDictionaryItem (game.random_places_list, GetRandomInt (0, ListCount (game.random_places_list) - 1)))

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

    set variable blue_ball.parent = [expression] red_ball.parent

if you wanted to place more than two Objects, then I'd put them into a List, and use 'foreach', shown in quasi-code, as it's quick:

    game.objects_list = split ("red_ball;blue_ball;yellow_ball;white_ball;black_ball", ";")

    foreach (object_variable, game.objects_list) {
      object_variable.parent = GetObject (StringDictionaryItem (game.random_places_list, GetRandomInt (0, ListCount (game.random_places_list) - 1)))
    }

actually, this isn't correct, as it'll move each Object to a random room, instead of moving all the objects to the same random room, but that would take more work to show, so meh, lol.

commented Aug 2 by jaynabonne (141 points)
Actually, it would be a trivial change:
game.objects_list = split ("red_ball;blue_ball;yellow_ball;white_ball;black_ball", ";")
parent = GetObject (StringDictionaryItem (game.random_places_list, GetRandomInt (0, ListCount (game.random_places_list) - 1)))
foreach (object_variable, game.objects_list) {
object_variable.parent = parent
}

## Related questions

- How do I give an object to a character and receive something back?
- How can I use an object count to determine a door's destination
- I want to make a verb that requires to things at once. How do I do this this? [internet version]
- How to show unexplored map nodes at the start?
- How does the {random} script work?

...