[solved] How do I choose() from Json Variables

Get help using Construct 2

Post » Wed Feb 18, 2015 5:48 pm

Hi

I would like to combine choose() with Json. The variables should be in the json database and send to choose().

instead:
choose(1,2,3,4)

it should use something like that (but it's not possible) :( :
choose(json.value(0,0,"Test"))

has somebody experience or tried something like this before??

thx in advance
You do not have the required permissions to view the files attached to this post.
Last edited by edwardr on Thu Feb 19, 2015 11:39 pm, edited 2 times in total.
B
15
S
4
Posts: 77
Reputation: 1,030

Post » Thu Feb 19, 2015 12:39 pm

or is there another possibility to have a weighted average and get the info from a database (json))
e.g.
Product 1: 0,0,1,2,0,1,0,2,0,
Product 2: 0,2,1,0,2,2,0,1,2,0

with choose(), I can weight it by saying e.g. choose(0,0,1,2). That means there are 50%: 0, 25%: 1, 25%: 1

but I want to get the information (0,0,1,2) from the database, because I don't need to program extra variables and compares. Right now I get all the information from the database: cost, price, production time, name,... but not how many products could be sold within a day :(
B
15
S
4
Posts: 77
Reputation: 1,030

Post » Thu Feb 19, 2015 3:25 pm

ok, I found a way, which works but it is not nice....
Maybe somebody has a better idea:

tokenat(JSON.Value(0,1,"Test"),choose(0,1,2),",")

tokenat(source, position, separator)
source: is JSON database
position: the random effect is in the position choose(0,1,2)

The disadvantage is: that I have always to put the same quantity of numbers in the json value, otherwhise there will be an error (no number or it won't be choosen).
You do not have the required permissions to view the files attached to this post.
B
15
S
4
Posts: 77
Reputation: 1,030

Post » Thu Feb 19, 2015 8:31 pm

A more general solution to your issue would be this:

Code: Select all
tokenat(JSON.Value(0, 1, "Test"), floor(random(tokencount(JSON.Value(0, 1, "Test"), ","))),",")

Which will pick a random token no matter how many tokens you have, so long as you have at least 1 token.
Moderator
B
109
S
41
G
20
Posts: 1,586
Reputation: 18,996

Post » Thu Feb 19, 2015 11:39 pm

thx, this is the final solution for my Problem.
You do not have the required permissions to view the files attached to this post.
B
15
S
4
Posts: 77
Reputation: 1,030


Return to How do I....?

Who is online

Users browsing this forum: GenkiGenga, lamar, Trimeister and 9 guests