Use random function with variable

Get help using Construct 2

Post » Sun Dec 09, 2012 6:44 pm

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?
http://postimage.org/image/v58hns5ln/
dino0002012-12-09 18:46:47
B
4
Posts: 14
Reputation: 510

Post » Sun Dec 09, 2012 6:55 pm

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.
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Sun Dec 09, 2012 6:59 pm

Maybe you just need to update the text in the field ?
Variable may change but text should be updated too.
B
5
Posts: 21
Reputation: 880

Post » Mon Dec 10, 2012 10:58 am

@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:

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

@Grafix4d: I don't have the text box for that. Thanksdino0002012-12-10 10:59:31
B
4
Posts: 14
Reputation: 510

Post » Mon Dec 10, 2012 4:53 pm

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).Space Ape2012-12-10 17:48:37
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Tue Dec 11, 2012 2:30 pm

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.dino0002012-12-11 14:31:07
B
4
Posts: 14
Reputation: 510

Post » Tue Dec 11, 2012 3:18 pm

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
B
22
S
3
G
6
Posts: 1,356
Reputation: 7,141

Post » Tue Dec 11, 2012 5:51 pm

@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 !
B
46
S
17
G
18
Posts: 847
Reputation: 13,802

Post » Tue Dec 11, 2012 7:51 pm

Ah, okay. I think I misunderstood the question, then. In that case, what Magistross said is what you're looking for.
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Tue Dec 11, 2012 9:35 pm

As a programming FYI

Reference to C
http://technoburst.blogspot.ca/2011/07/difference-between-const-and-static-in.html

and from Scirra manual
https://www.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
jayderyu2012-12-11 21:40:29
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 20 guests