Dynamic Choose

7

Attached Files

The following files have been attached to this tutorial:

.capx

dynamicchoose.capx

Download now 175.8 KB

Stats

3,316 visits, 4,338 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

The Choose function is awesome!

...but mine here is better.

Say you wanted to pick a random greeting so when the game starts a character says either "Hi", "Hello", "Hi there" or "Greetings" or "What's Up?"

You could just have it say choose("Hi", "Hello", "Hi there", "Greetings" ,"What's Up?" ) and it will automatically pick one of those randomly.

With this you can call random functions, pick random animations, make random sentences or responses easily from a list by hard coding as many parameters into the choose function as you want.

There's just one problem:

What if you want that list to change at run-time? What if you want instead of a list of parameters... have it pick from a single comma delimited string?

Like choose("hi,what's up,hi there,hello,greetings, salutations")

I made a dynamic choose function that does just that simply by using the tokenCount() and tokenAt() functions. Not only can you use a dynamic list that can change all it wants at run time, but you can pick your delimiter too so it doesn't just have to be a comma.

Choose (delimited list) can be accomplished manually by using:

TokenAt( ListString , Int(Random(TokenCount(ListString, Delimeter))),Delimeter)

To break that down TokenAt gives you the item from a list separated (delimited) by commas or spaces or whatever the Delimiter you give it is. The item is at the given number so if your list has 3 items, so tokenAt("1,2,3", 2, ",") will give you "2."

The Joe Thought About The Prawn.

So I made a function where you can just send it a string with a list delimited however you want, and a simple demo. You can add more to the list or take some away and it will work. Hit next to see a dumb sentence generator when you press SHUFFLE.

Just add the Group with the Function

To use the function in your own programs, copy and paste the CHOOSE group into your own project. I like using local variables for my parameters just for my own sanity I don't like typing function.param(1) anywhere in my code especially in complex functions way down. But you can actually just use the event action without any function it's just a system TokenAt function.

Anyhow enjoy.

How cool will this be when we can create object by name in v3?

.CAPX

dynamicchoose.capx

Download now 175.8 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Thank you a lot! As a beginer I spent several days for searching how could I choose random coordinate from the list that changes from time to time and these tokenat and tokencount triks literally blowed my mind! It works really nice and I thank you very much.