choose a number between aviable numbers

  • Hi guys,

    I'm having trouble to make something I thougt would be easy.

    I have a pool of aviable characters (around 20), and on the start of the layout, I want to choose some of them (around 10), only once each.

    Since its only a quick project I dont even want to be a clean code, I'm ok with some hardcoding if it's easier.

    Right now I have a 4 variables and a dictionnary in which I repeate the following code:

    Variable: RandomKey = number
    Variable: Used= string
    Variable: index= number
    Variable: count= number (is used to repeat the function 10 times)
    

    On start of the layout

    dictionnary add key (I add all the keys)

    every 0.5sec
    count <= 10 
    	> Function > call "test"
    
    Function on "test"
    -Set randomKey to > int(random(0,Dictionary.KeyCount))
    -Set index to > -1
    	Dictionnary For Each key
    	-Add 1 to > index
    		Index = randomKey
    		find(used,randomKey)=1
    			-Add 1 to > count
    			-set used to > used&","&randomKey
    

    So with that it find randomly some characters, but pick somethime the same multiple times and I dont know how to check if they where already picked.

    Have you any idea on how to do that?

    thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's very hard to read the unformatted code you posted..

    Since you don't need a clean solution, the easiest one is to add all 20 characters to the layout and do this:

    System For each Character Order by random(1)
    Loopindex>9	: Character destroy
    

    This will remove all but 10 random characters. If they are different sprites, add them to a family.

    You can also add all character numbers to an array, pick a random index and remove it from the array:

    Repeat 10 times
    	Set r to int(random(array.width))
    	Create character with animation frame r
    	array delete element r
    
  • Hi! Sorry for the unformatted code, I never used the option in the forum, I will try to fix this.

    Thanks for the solutions, I will check them out!

    Edit:

    (I did fixed it, I hope it's better)

  • Alright, your solusitons inspired me: I delete the dictionnary.currentKey at the end of the function and it works as I wanted.

    Thanks!

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