Dice rolling w variables

scjoye

  • Posts: 62
New to Stencyl, checked Stencylpedia, searched forum etc. did not find enough help... SO:

Similar to Risk board game dice rolling... I want to find a way to have two dice roll against each other (eventually multiples up to three based on previous scene info ala Risk) with visible dice rolling, come to random numbers and score a point for the winner between the two dice, highest number wins.

Have spent hours trying to figure this out and while I think I am kinda sorta close... I am not getting it done. Thanks for any help.

Meestar

  • Posts: 654
Dice rolling is simple.  Visible would be simple if your not expecting them to behave like real dice, just show 2D dice and randomly go through each side's animation quickly before setting your random number.

A 6 sided die would be set to a random number between 1 and 6.
PM me if you require help.  I'm always glad to help out!

scjoye

  • Posts: 62
Yes, I have 2d dice animated going through all 6 sides, etc.

Sorry - I am SUPER new at this and trying to get this to work for some class stuff coming up in a month or so (I'm a teacher...) - how would you go about assigning the random 1-6 number to each die? Would you then assign that number to a created attribute that you could then use in later events to find a winner?

Thx again,

Blob

  • *
  • Posts: 721
Setting a Number Attribute with the random number block would do it.



The random number block is in the Numbers & Text category.

Meestar

  • Posts: 654
Yes, I have 2d dice animated going through all 6 sides, etc.

Sorry - I am SUPER new at this and trying to get this to work for some class stuff coming up in a month or so (I'm a teacher...) - how would you go about assigning the random 1-6 number to each die? Would you then assign that number to a created attribute that you could then use in later events to find a winner?

Thx again,

I wish I had a teacher like you :P  Anyways, just as Blob said.  Also, there's a search bar where you can easily look up blocks provided that there is one.
PM me if you require help.  I'm always glad to help out!

scjoye

  • Posts: 62


Sorry - took me a while to figure out that I had to CREATE the new Number Attribute block... yeah I know. Anyways, so this is still not working even though I have been playing around for a while with it. Problems I am having:
A. How do I get the animation to stop / fall to the correct frame that corresponds to the number attribute? So if 4 is chosen for example then the actual dice animation would STOP on the frame showing the 4 on the die? I feel like my tiny piece of code should make this happen?
B. How do I get the number attribute block defined in this actors event to carry over to the scene? I imagine I need to do this so I can compare TWO (eventually more than two) die so that I can find a "winner" between the two dice ala Risk?

Blob - Thank you for the starting code!
Meestar - while I appreciate the teacher comment and the help (and man I am trying...) I still suck at Stencyl thus far! :)

Innes

  • *
  • Posts: 1960
This problem confused me when I started working with animations!


The issue is the difference between animations and frames... Animations always run to the last frame, or they loop if looping is specified.


What you need is 6 animations for your die, and to switch to the required animation when you know the result of the random number. So, you need one animation that has six frames representing the six sides of the die (the 'rolling' animation), and you need six more animations, each having one frame; a different side of the die.


Display the rolling die animation first, then switch to the appropriate animation when you know which number you need to display.


Or... An alternative solution, is to just have six animations for the die, each having one frame. You then repeatedly switch between the 6 animations and just stop switching between them when the random number is chosen. You the display the correct animation for the number.


I think the first solution is more efficient in terms of processor usage, but requires duplicate images, and the second animation is less efficient when it comes to processing, but uses fewer images. For a small game, the second solution won't cause any problems, and is probably the easier to implement.


I think your confusion has (understandably) arisen because we assume that an 'animation' has several frames; it can, but it doesn't have to! Also, with problems such as this, it is not immediately apparent that animations always run through to the last frame, even if we [switch to frame...]. This instruction block is only temporal; it doesn't 'lock' the animation on that frame. I would prefer a simple toggle along the lines of [stop/start animation on {frame}] but that doesn't exist (as far as I know, anyway), so we have to work with what we have!
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

flyingninja77

  • Posts: 30
For your comparing dice results situation, you're going to need to set up several things in your scene.  First, you need 1 number attribute for each die whose number you intend to keep track of.  In a gist, you want:

Number: MyDice1
Number: MyDice2
Number: MyDice3
Number: BadDice1
Number: BadDice2

I'm going with the Risk example, but the logic is similar for any number of dice.  So, second, we want some way that the dice on the screen roll these numbers.  Let's go with the easier way.  Let's make an equal number of Actor attributes.  These can be customized in each scene.

Actor: GoodDice1
Actor: GoodDice2
Actor: GoodDice3
Actor: EvilDice1
Attor: EvilDice2

It's not so important what you name them as you know which actor corresponds to which number.

NOTE: As you are reading this, I am in the process of writing up a visual of the logic code.

flyingninja77

  • Posts: 30
Alright, I uploaded a behavior to StencylForge called Scene Dice Roll Behavior.  You can download it and check it out.  It would probably be helpful to take a look at the logic.  If there's something you don't understand, let me know (or if there's a feature I missed--I'll get that).

Right now, it requires an actor behavior called Dice Roll to have an attribute called Roll Number, but I'll work on tweaking that in the near future.

scjoye

  • Posts: 62
Innes: OK - This ABSOLUTELY makes sense... I would have spent the next hundred hours thinking that I could stop an animation on a certain frame... knowing that I need six different actors for the six different die sides makes things MUCH more clear. Thank you.

flyingninja77: Awesome - will check out immediately!

I'll be back...

scjoye

  • Posts: 62


A sample of what the code looks like so far when running... I can click the roll again button and it will re-roll the dice and come up with a new winner... or a tie.



This is what some of the code looks like:



So there are basically TWO of the created actions like above, one for each die with a Chosen Number Attribute and a Chosen Number 2 attribute. I quickly photoshopped some dice and they are PNGs down to about 6-7kb each. I assume once all actors are added (6 per each die w 5 dice eventually needed) it should still run smoothly and not see a slow down?



This is what my simple code looks like to figure which die wins and posts the winning notice.

QUESTIONS:
1. Best way to go about adding MORE dice? Eventually I want 3 die vs 2 die as in Risk, but what about just 2 v 1? How would I go about telling the program to match up HIGHEST die to HIGHEST die?
2. How do I tell Stencyl to record wins / losses for the two players when I get the winner from each dice roll? I can figure out if it is just a one player type of game... that is pretty easy... but how do I get it to report out to add a point to one or the other side each time the variable dice roll?

THANK YOU FOR ALL THE HELP SO FAR - ALL HAVE BEEN EXCEEDINGLY GENEROUS IN COMING TO THE AID OF THE OLD NEWB!

scjoye

  • Posts: 62

Meestar

  • Posts: 654
Bump / Anybody?

I could have sworn I replied to this asking why you had 6 dice actors per die and told you how to do what you asked...

Anyways, you would need to compare each die to the other dice.

If dice 1 > dice 2
   Set high die 1 to dice 1
   if dice 2 > dice 3
     set high dice 2 to dice 2
    otherwise
        set high dice 2 to dice 3.

Also:
if player turn = 2
   add 1 point to Player 2
PM me if you require help.  I'm always glad to help out!

Innes

  • *
  • Posts: 1960
I could have sworn I replied to this asking why you had 6 dice actors


I wondered too! For clarification, to roll a die and stop on a specific number, you only need one actor. My suggestion was for the actor to have one animation with 6 frames (for showing the die rolling) and then 6 additional animations; one for each face of the die - one of which will be displayed when the random number is selected.


Another way would be to have one actor with six animations, each having one frame. To roll the die, show the animations consecutively and then just display the relevant animation when the random number is selected. I think this would require more programming though.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

flyingninja77

  • Posts: 30
1. Best way to go about adding MORE dice? Eventually I want 3 die vs 2 die as in Risk, but what about just 2 v 1? How would I go about telling the program to match up HIGHEST die to HIGHEST die?

Did you take a look at the code I posted on StencylForge?  It has a "pick the highest number" feature built into it.  But if you want to have it like Risk--i.e. 1, 2, or 3 dice versus 1 or 2 dice, it's a little more complicated, but not too difficult.  I think I could modify the behavior to make it work like that.  I'll let you know when I've gotten it up on the Forge.

Happy trails, Flyingninja77

EDIT--I'll post a sample game that simulates a Risk-style comparison so you can see the code in action.

« Last Edit: February 26, 2013, 10:15:04 am by flyingninja77 »