Using Advanced Random's Permutation Tables

12

Index

Features on these Courses

Attached Files

The following files have been attached to this tutorial:

.c3p

ar-permutation-table-card.c3p

Download now 80.19 KB

Contributors

Stats

4,374 visits, 9,061 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.

Published on 26 Aug, 2021. Last updated 5 Jan, 2023

There are only six events in this project so it shouldn't take much to set up. First up, we need a few things to happen when the layout starts:

Condition

System ▶︎ On Start of Layout

Action

AdvancedRandom ▶︎ Create permutation table with 52 values starting at 1

AJAX ▶︎ Request CardNames.json (tag "LoadArray")

FaceUpCard ▶︎ Tween "ShrinkWidth" property Width to 0 in 0.5 seconds (Default, Destroy: No, Loop: No, Ping Pong: No)

This set of actions creates our permutation table, starts off the AJAX events to load the card name data and turns over the first card.

Once the AJAX request is complete, we need to load that data into the Array and update the text object:

Condition

AJAX ▶︎ On "LoadArray" complete

Action

Array ▶︎ Load from json string AJAX.LastData

Text ▶︎ Set text to "Current Card: " & Array.At(AdvancedRandom.Permutation(CardNumber),0) & newline & "Cards left in deck: " & (51-CardNumber)

These two events start off the project, so next we need to make sure you can flip through the deck of cards. Progressing through the deck is done by clicking the NextCard sprite. Though we do need to add in a couple of extra conditions to make sure you can't spam through the deck and potentially mess it up.

Condition

Mouse ▶︎ On Left Button clicked on NextCard

FaceUpCard ▶︎ Is NOT playing any Tween

System ▶︎ CardNumber ≠ 51

Action

System ▶︎ Set FirstCard to False

FaceUpCard ▶︎ Tween "ShrinkWidth" property Width to 0 in 0.5 seconds (Default, Destroy: No, Loop: No, Ping Pong: No)

The second part of the animation and the updating of the card numbers are done when the first Tween ends – though different things happen depending on whether the FirstCard Boolean is true or not.

Condition

FaceUpCard ▶︎ On Tween "ShrinkWidth" finished

Sub-Event Condition

System ▶︎ Is FirstCard

Sub-Event Action

FaceUpCard ▶︎ Set animation frame to AdvancedRandom.Permutation(CardNumber)

FaceUpCard ▶︎ Tween "GrowWidth" property Width to 128 in 0.5 seconds (Default, Destroy: No, Loop: No, Ping Pong: No)

Sub-Event Condition

System ▶︎ Is NOT FirstCard

Sub-Event Action

System ▶︎ Add 1 to CardNumber

FaceUpCard ▶︎ Set animation frame to AdvancedRandom.Permutation(CardNumber)

FaceUpCard ▶︎ Tween "GrowWidth" property Width to 128 in 0.5 seconds (Default, Destroy: No, Loop: No, Ping Pong: No)

Text ▶︎ Set text to "Current Card: " & Array.At(AdvancedRandom.Permutation(CardNumber),0) & newline & "Cards left in deck: " & (51-CardNumber)

And that's all there is to the event sheet. The project itself may be small, but it shows how you can have a non-repeating sequence of numbers thanks to the Permutation Table and how to use it in a context like a deck of cards. You could use it for all sorts, including a non-repeating number list to use for quiz questions – simply call the question number in place of the card number in this example.

Enjoy making use of Permutation Tables!

  • 5 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hey, I want to create a set of numbers, and pick a random number from the set. Then I want to assign each number from the set to a sprite, so that if the table picks a number, then the sprite will pop up or do a action. How do I do that?

      • [-] [+]
      • 1
      • Laura_D's avatar
      • Laura_D
      • Construct Team Community Manager
      • 1 points
      • (0 children)

      These sorts of questions are best asked in the forums really, but as a basic example, you could create the permutation table on the start of the layout, then when a key is pressed you'd iterate through a 'For' loop the same size as the permutation table to spawn that many sprites and assign them an animation frame that matches the loopindex.

  • Hello, thanks for the tutorial it's super useful ! Is there any way to chose the animation of the FaceUpCard the same way we chose the frame ? For example, if I want to make a moving animation for each card, I would create multiple animations on the same object and just call the right one with the matching AdvancedRandom.Perutation(CardNumber), but it does not seem to be possible since changing the animation requires its name, and not a number. Is there a way to do that ? Thanks in advance

      • [-] [+]
      • 2
      • Laura_D's avatar
      • Laura_D
      • Construct Team Community Manager
      • 2 points
      • (0 children)

      There is a system expression to turn an integer or float into a string - try adding str() around the Advanced Random expression.

  • ótimo, obrigado