0 Favourites

Use random function with variable

  • Hello.

    I am using this: floor(random(1,maxCarrots)) to get a number.

    maxCarrots is global variable.

    The thing is, I want to change its value by clicking on an object.

    But it doesn't happend, and it stays on its initial value.

    How do I set it?

    postimage.org/image/v58hns5ln

  • Hello dino.

    When you setup the global variable, maxCarrots, did you check the 'constant' box? Double-check to make sure it's not something that's fixed. Also, do you have any events that are re-setting the variables or making maxCarrots reset during the game?

    Based on the screenshot, it looks fine, but it's hard to tell if anything is interfering with the event. Make sure there's no other events interfering with the maxCarrots variable.

    I was pulling hair out over a similar problem and eventually discovered that I had another event that was triggering when I clicked on something and it also modified the same variable.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Maybe you just need to update the text in the field ?

    Variable may change but text should be updated too.

  • space Ape: Yes, I'm pretty sure. Constant is unchecked. I double-checked my event sheet for any other actions that use it, but couldn't find any. Here's my project:

    dl.dropbox.com/u/68624493/Counting.rar

    @Grafix4d: I don't have the text box for that. Thanks

  • Hey dino. I'm assuming you want the # of carrots left to change as the player puts carrots in the box, right?

    You'll need a separate variable to track the difference between the # of carrots needed and the # in the box, re-calculating whenever a carrot is put in the box (or every tick, if that's easier). Right now, you don't have a variable that tracks the changing "# of carrots needed".

    You'll also need to change the # that's displayed to match the number of carrots left. Because you use images, that could be a a lot of work; you are moving images into and out of the layout during the game. It might be better to put all the # images into a single animation and just change the animation frame to match the # of carrots remaining.

    I can see how that was frustrating! It took me a while to locate the problem; sometimes we are blind to our own work.

    Take care, dino, and let me know how it goes!

    Edit: Let me know if you'd like some help and I'll work on making the changes suggested. I'm using the latest build (r111, I think).

  • Those are some very good advices and I will apply them to my project.

    But I obviously didn't explain my problem well.

    The thing is, when the Menu shows up, user can pick to play with max. of 5 or 10 carrots. If he pick 5 carrots, then variable maxCarrots should be set to 5 and etc, but it isn't set.

    Thank you for your effort. It means a lot to the beginner.

  • Here is a capx sample i made for you.Click on the 5 or 10 box to set the global and drop the amount you set into the bin.did not have time to explain everything within the capx but it could help.Did the capx with R110.2

    Download example capx

  • dino000

    Your problem comes from your "canSet" variable of your rabbit sprite. It is set to true on the start of the layout, so the global variable "number" get set immediately to "floor(random(1,maxCarrots))", even before the user select anything in the menu. Since "maxCarrots" has a default value of 10, you always get a random number between 1 and 10 !

  • Ah, okay. I think I misunderstood the question, then. In that case, what Magistross said is what you're looking for.

  • As a programming FYI

    Reference to C

    technoburst.blogspot.ca/2011/07/difference-between-const-and-static-in.html

    and from Scirra manual

    scirra.com/manual/83/variables

    ------------

    CONSTANT are variable that are initialized ONCE at the beginning. They are supposed to hold unchanging values.

    STATIC are variables that persist in a class or function between calls, but allow for the variable content to change.

    ------------

    Since maxCarrots can change he should be setting the it's toggle to static not const. Will this fix your problem? no idea, but it's what you should be doing to your maxCarrots variable as you need it to persist and change.

    static = on

    const = off

  • Thank you all guys! Great forum crew. Problem solved.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
random bugs and suggestions
90 4,515
Tom's avatar
Tom Construct Team Founder
Unread hot topic
76 9,273
Pandadoor's avatar
Pandadoor
Unread hot topic
0 Favourites
Rep is very random...
73 17,906
allavett's avatar
allavett