#Searching within a dictionary and return all possible results and randomly selecting one

58 messages · Page 1 of 1 (latest)

gray comet
#

Let's say I have the following Dictionary (name, rarity)

var characters = { ["Foo", "common"], ["Bar", "common"], ["Baz", "rare"], ["Quux", "epic"], ["Quuz", "legendary"] }
 
And I want to randomly spawn an enemy based on rarity:
I define a random value between 0 and 1:

var roll = randf()

And then I limitate the probability

if roll < 0.8: return "Any Common monster" elif roll < 0.90: return "Any Rare Monster" elif roll < 0.99: return "Any Epic Monster" else: return "Any Legendary Monster"

What I want to do is replace return "Common" for a code that searches in my dictionary, filters all possible monsters by that rarity, and then randomly select one of them:

Example:

  1. Randf() returns 0.5 --> Common
  2. Filter Dictionary by Common = Foo, Bar
  3. Randomly choose between all options (1 or 2) --> 1
  4. Add Foo to a new dictionary

Hope I have been clear enough, as I am failing to understand how to do this from the documentation 😦

primal dawn
#
var characters = [
    ["Foo", "common"],

Btw your current code you have an array of arrays here

#

There is a difference of [] and {} in this context.

gray comet
#

Oh yeah, because I was trying to do it this way, it actually comes from a JSON, so definitely a Dictionary 🙂, fixed thanks!

primal dawn
#

Not sure if that is a typo or your actual code is like that.

#

Now you have a Dictionary with an Array inside which seems off also. Since the keys of a Dictionary should be String.

#
var characters = {
    ["Foo", "common"],
    ["Bar", "common"],
#

I guess the question is do you have an array of dictionaries, or do you have one dictionary and the creature names themselves only map to rarity and nothing else.

#

At a high level, I think I would change it so that it is more like so that my rarity is an enum of increasing int values:

[
    {"name": "Wolf", "rarity": 1},
    {"name": "Demon", "rarity": 5},
]

That way I could also add "hp" or whatever as an attribute in the future. But maybe you already have that but just did not share your whole JSON.

#

Then now technically you could just keep generating random number until list[random_number]["rarity"] <= random_rarity

gray comet
#

My JSON looks like this:

gray comet
primal dawn
gray comet
#

I'll try it

gray comet
#

will it work if do this?

primal dawn
#

That would work too.

gray comet
#

Why would you list instead of get the dictionary? (educational purpose only)

primal dawn
#

It would make this particular use case easier

#

Well, joke's aside

#

Having it be {"name": "Wolf"} instead of "Wolf": { } I think helps more clearly define the attributes of something.

#

It could help with translation in the future too maybe.

gray comet
#

Invalid get index 'list' (on base: 'Dictionary'). 😦

primal dawn
#

drop the .list

#

and you want keys() in your way instead of my way

gray comet
#

oh

primal dawn
#

because my code assumes an array

gray comet
#

like this? return characters[random_number]["rarityindex"].keys() <= random_rarity

primal dawn
#

No, keys() returns an array which you can't compare with an int

#
return characters[characters.keys()[random_number]]["rarityIndex"] <= random_rarity
#

This assumes random_rarity is generated based on your desired probability.

#

and that "5" only comes up 1% of the time or whatever you wanted

#

Though probability is something that can be tweaked. Perhaps you could first confirm you are "randomly generating characters".

gray comet
#

I must be really dumb or something, I'm still getting the Index issue 😦

primal dawn
#

It still says index 3 though?

gray comet
#

no, rarityIndex now

primal dawn
#

Oh

#

yeah, that's my fault

#

I wrote rarityIndex

#

but you put rarityindex

#

So the code I gave you was wrong due to capitalization of i

gray comet
#

oh no, I tried both ways

primal dawn
#
print(characters[characters.keys()[random_number]])
return characters[characters.keys()[random_number]]["rarityindex"] <= random_rarity
#

Do that and see what is printed

#

It suggests rarityindex could not be found.

#

So we need to see what is in the obj

gray comet
#

same error 😦

#

Oh got it by uncommenting the return

#

my rarityindex is not there

#

that's why

#

OMG I didnt's save the file haahah

#

sorry 😦

#

THANKS 🙂 many many thanks for the patience

primal dawn
#

So your problem is resolved?