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

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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).

  • A more general solution to your issue would be this:

    tokenat(JSON.Value(0, 1, "Test"), floor(random(tokencount(JSON.Value(0, 1, "Test"), ","))),",")[/code:1vnqgw5v]
    Which will pick a random token no matter how many tokens you have, so long as you have at least 1 token.
  • thx, this is the final solution for my Problem.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)