random

Talk about making games.

random

Postby BogdansB » Fri Nov 16, 2012 5:45 pm

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... :(
BogdansB
 
Posts: 94
Joined: Tue Oct 25, 2011 2:30 pm
Score: 0 Give a positive score

Re: random

Postby skydereign » Fri Nov 16, 2012 7:37 pm

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);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: random

Postby BogdansB » Fri Nov 16, 2012 7:54 pm

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....
BogdansB
 
Posts: 94
Joined: Tue Oct 25, 2011 2:30 pm
Score: 0 Give a positive score

Re: random

Postby skydereign » Fri Nov 16, 2012 8:03 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: random

Postby BogdansB » Fri Nov 16, 2012 10:11 pm

timer...

when i use it , the half of the button light up instead of just one
BogdansB
 
Posts: 94
Joined: Tue Oct 25, 2011 2:30 pm
Score: 0 Give a positive score

Re: random

Postby skydereign » Fri Nov 16, 2012 11:14 pm

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.
Attachments
random_actor.ged
(8.12 KiB) Downloaded 122 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: random

Postby BogdansB » Fri Nov 16, 2012 11:23 pm

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
BogdansB
 
Posts: 94
Joined: Tue Oct 25, 2011 2:30 pm
Score: 0 Give a positive score

Re: random

Postby BogdansB » Mon Nov 19, 2012 7:59 pm

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...
BogdansB
 
Posts: 94
Joined: Tue Oct 25, 2011 2:30 pm
Score: 0 Give a positive score

Re: random

Postby skydereign » Mon Nov 19, 2012 8:06 pm

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);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest