Page 1 of 1

random

PostPosted: Fri Nov 16, 2012 5:45 pm
by BogdansB
hey.

i want a random actor light up (change animation) , so out of 64 acotrs they need to be chosen a random one after a timer ends. no idea how could work..


tried :


Code: Select all
ChangeAnimation("button.rand(63)", "button_on", FORWARD);


but the problem is he reads it like a string... :(

Re: random

PostPosted: Fri Nov 16, 2012 7:37 pm
by skydereign
You've got the right idea, you just need to convert that rand into the actual string. For that you can use sprintf. Another thing is that C casts to int by removing the floating point, meaning it doesn't round up. So, you would use rand(64) because rand returns values between 0 and the number specified (not inclusive).
Code: Select all
char cname[30];
sprintf(cname, "button.%d", rand(64));
ChangeAnimation(cname, "button_on", FORWARD);

Re: random

PostPosted: Fri Nov 16, 2012 7:54 pm
by BogdansB
im not understanding the code fully :S why %d ...

but anyways where do i need to put the code? in draw `? couse when i do it , just the button.0 lightens up... i have also a timer . i want every 3 seconds to light up a button....

Re: random

PostPosted: Fri Nov 16, 2012 8:03 pm
by skydereign
Forgot to actually cast the int. This should work.
Code: Select all
char cname[30];
sprintf(cname, "button.%d", (int)rand(64));
ChangeAnimation(cname, "button_on", FORWARD);


BogdansB wrote:why %d ...

You need to know how the printf functions work. http://www.cplusplus.com/reference/clibrary/cstdio/printf/
Pretty much %d means replace it with a number (which are the arguments after the string format).

BogdansB wrote:but anyways where do i need to put the code? in draw `? couse when i do it , just the button.0 lightens up... i have also a timer . i want every 3 seconds to light up a button....

This is an odd question. I think you can answer this one out yourself.

Re: random

PostPosted: Fri Nov 16, 2012 10:11 pm
by BogdansB
timer...

when i use it , the half of the button light up instead of just one

Re: random

PostPosted: Fri Nov 16, 2012 11:14 pm
by skydereign
Here is an example of it working. The only difference is I changed the function into ChangeTransparency (that way there isn't a data directory) and button is now called test. It must be something wrong with your timer.

Re: random

PostPosted: Fri Nov 16, 2012 11:23 pm
by BogdansB
ok thanks . i figured out where the problem was.

the timer was created by buttons on buttons so the timer was created 64 times... now i created on view and it works perfectly :D

Re: random

PostPosted: Mon Nov 19, 2012 7:59 pm
by BogdansB
so i was thinking about making the timer getting every 500 ms , 100 ms faster so instead of lightening up everi 3000ms it lights up every 2900 and that repeats...

i tried with

Code: Select all
while(i!=0){

i=i*0.99;

}

but too fast...

and i tried
-> in create actor
Code: Select all
CreateTimer("Event Actor", "light", milliseconds);


and then if the timer ends it destroy and then it creates again with the time milliseconds wich gets smaller...

... but didnt got faster or didnt worked at all... dont know how to do it...

Re: random

PostPosted: Mon Nov 19, 2012 8:06 pm
by skydereign
BogdansB wrote:so i was thinking about making the timer getting every 500 ms , 100 ms faster so instead of lightening up everi 3000ms it lights up every 2900 and that repeats...

If that is what you want, then do just that. The only trick to it is something you already know (using the variable for the timer length). I'm going to call it timer since it's shorter than milliseconds. I'm also using another variable count.

Set timer to initial value (timer=3000).
Create the timer (no repeat) using timer for the length.
Add the following code.
Code: Select all
count++;
if(count>=5)
{
    count=0;
    timer = max(timer-500, 500);
}
// add the actual code you want the timer event to trigger
CreateTimer("Event Actor", "whatever_this_timer_is_called", timer);