Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: There's got to be an easier way to do this...  (Read 3894 times)

0 Members and 1 Guest are viewing this topic.
There's got to be an easier way to do this...
« on: September 23, 2010, 09:05:48 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 41
Check attached picture.

Any ideas?

Edit: Well not easier but simpler
« Last Edit: September 23, 2010, 09:45:40 pm by requiem »
Logged
Re: There's got to be an easier way to do this.....
« Reply #1 on: September 23, 2010, 10:47:39 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Not with D&D, I'd say there's an easier way with code though.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: There's got to be an easier way to do this.....
« Reply #2 on: September 23, 2010, 11:43:18 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
I'm pretty sure this would be your code equivalent to it:
Code: [Select]
if (floor(random(2)) == 0)
{
    if (floor(random(2)) == 0)
    {
        sound_play(snd_playerAtk_1);
    }
    else
    {
        sound_play(snd_playerAtk_2);
    }
}
else
{
    if (floor(random(2)) == 0)
    {
        sound_play(snd_playerAtk_3);
    }
    else
    {
        sound_play(snd_playerAtk_4);
    }
}

However I don't exactly remember how the random function works, so you may have to piddle !@#$% with it.
Logged
  • https://colbydude.com
Re: There's got to be an easier way to do this.....
« Reply #3 on: September 23, 2010, 11:59:08 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
This works(I am assuming you want Link's attack yell to be random):
(It should work)

Code: [Select]
atksound = choose(0, 1, 2,3);

switch (atksound)
{
case 0:

sound_play(snd_atk_1);
break;

case 1:
sound_play(snd_atk_2);
break;

case 2:
sound_play(snd_atk_3);
break;

case 3:
sound_play(snd_atk_4);
break;

}

I cannot remember your sound file names so I made those up.  This is assuming you have a Press (insert key) event.  If not then it would go under if keyboard_check_pressed && attacking = 0 or something like that in your step event.
Logged
  • Super Fan Gamers!

DJvenom

super-sage
Re: There's got to be an easier way to do this.....
« Reply #4 on: September 24, 2010, 06:23:17 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
Code: [Select]
atk[0]=snd_atk1
atk[1]=snd_atk2
atk[2]=snd_atk3
atk[3]=snd_atk4
stp=atk[floor(random(4))]
sound_play(stp)
Logged
I do art
I ermmm... DID do art
I do art
Re: There's got to be an easier way to do this.....
« Reply #5 on: September 24, 2010, 06:33:41 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Code: [Select]
atk[0]=snd_atk1
atk[1]=snd_atk2
atk[2]=snd_atk3
atk[3]=snd_atk4
stp=atk[floor(random(4))]
sound_play(stp)
Sorry, that won't work.

Anytime you call a straight random(x), it does decimals. IE the random number could be 1.034348.  Pisses me off all the time.
You would have to add code to get the full number/round up/round down.
Logged
  • Super Fan Gamers!
Re: There's got to be an easier way to do this.....
« Reply #6 on: September 24, 2010, 06:42:50 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 29
Code: [Select]
atk[0]=snd_atk1
atk[1]=snd_atk2
atk[2]=snd_atk3
atk[3]=snd_atk4
stp=atk[floor(random(4))]
sound_play(stp)
Sorry, that won't work.

Anytime you call a straight random(x), it does decimals. IE the random number could be 1.034348.  Pisses me off all the time.
You would have to add code to get the full number/round up/round down.

That's exactly why he did "floor(random(4))"...

The correct way to fix his post would have been to inform him that (floor(random(4))) can still hit 4 (extremely rarely) and rounded down that is just 4 again, which would mess up the code because the array would be out of bounds. But instead you obviously didn't even read the part where he utilizes the random function.

Sir "DJveno" (what is that last symbol??), your suggestion should work; However, please add a copy of the third index (with a "4" instead!) to accomodate for this rare occurence of hitting the highest-possible number.
Logged
Re: There's got to be an easier way to do this.....
« Reply #7 on: September 24, 2010, 02:28:25 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
No, random() doesn't work that way.  It returns a number between 0 and x which is >= 0 and < x.  So in this case, 0 to 3.99~.  Floor() on the other hand, rounds DOWN.  4 will never come out.
« Last Edit: September 24, 2010, 02:30:53 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: There's got to be an easier way to do this.....
« Reply #8 on: September 24, 2010, 10:12:35 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 29
No, random() doesn't work that way.  It returns a number between 0 and x which is >= 0 and < x.  So in this case, 0 to 3.99~.  Floor() on the other hand, rounds DOWN.  4 will never come out.

Yes, of course. My mistake. I assumed it worked similarly to the way it sometimes outputs a 0. I tested it and you're right, 4 never comes up, regardless of the seed. I suppose I've gotten too used to the irandom_range() function.
Logged

Antidote

>.>
Re: There's got to be an easier way to do this.....
« Reply #9 on: September 24, 2010, 11:58:21 pm »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
yes, the random Function is a weird little guy, it goes from 0 to seed - 0.001 or something similar to that.
Logged
  • Axiomatic Data Laboratories
Re: There's got to be an easier way to do this.....
« Reply #10 on: September 25, 2010, 06:39:21 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 41
So since I have 4 different sounds I would want to use floor(random(5))?
Logged
Re: There's got to be an easier way to do this.....
« Reply #11 on: September 25, 2010, 06:55:28 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 29
So since I have 4 different sounds I would want to use floor(random(5))?
No. Most/all of the suggestions here will work fine. Some good suggestions here.

Array indexes start at 0, that's why "DJveno" seemed to end his/her array at 3, because it still totalled 4 (that is to say, 0, 1, 2, 3). floor(random(4)) should never bring up 4, that was a mistakeful assumption on my part based on how it 0 can sometimes occur during things like ceil(random(1)) --- and as mentioned above, I quickly dispelled that notion by running a for-loop in Game Maker and checking that it would never come across a 4.

So, by "DJveno"'s suggestion, you can do this:

Code: [Select]
atk[0] = snd_playerAtk_1;
atk[1] = snd_playerAtk_2;
atk[2] = snd_playerAtk_3;
atk[3] = snd_playerAtk_4;
sound_play(atk[ floor(random(4)) ]); // Play one of the 4 "Atk" sounds randomly.

(NOTE: spaces aren't required, nor are the semi-colons and the comment. Remove if neccessary.)

Tell us how it goes.
Logged

Mirby

Drifter
Re: There's got to be an easier way to do this.....
« Reply #12 on: September 25, 2010, 07:01:17 am »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
Yeah, that's exactly what DJvenom was saying. Even my basic knowledge of GML can see that. 0 means 1, 1 is 2, 2 is 3, and 3 is 4. It still gets to 4.

His solution is actually quite elegant in its simplicity, imo.
« Last Edit: September 26, 2010, 01:21:03 am by Mirby »
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios
Re: There's got to be an easier way to do this.....
« Reply #13 on: September 25, 2010, 09:18:43 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 41
Ok so DjVenom's method works just like my D&D version though I'd much rather be using code, thanks for all the help.

But is there a way I can make sure that the same sound doesn't play two times in a row?

Edit: On second thought I'd rather it work like so.

If random = 1, next sounds will be 2-3-4
If random = 2, next sounds will be 1-3-4

And so on, and so forth...

However if the user doesn't attack again for, let's say 2 seconds, then the pattern resets to next strike being random.
« Last Edit: September 25, 2010, 09:23:23 pm by requiem »
Logged

Antidote

>.>
Re: There's got to be an easier way to do this.....
« Reply #14 on: September 25, 2010, 10:08:50 pm »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
@Mirby: it's actually easier to say "index+1" :P

@Requiem: not exactly, since the number is "random" you can even have the same sound play several times in a row, this is because of the small seed (which is necessary).

Why would you want it to do that? o.0
It works this way in OoT.
« Last Edit: September 26, 2010, 12:33:59 am by Antidote »
Logged
  • Axiomatic Data Laboratories

Mirby

Drifter
Re: There's got to be an easier way to do this.....
« Reply #15 on: September 26, 2010, 01:21:30 am »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
Like I said, basic knowledge. :P
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios

DJvenom

super-sage
Re: There's got to be an easier way to do this.....
« Reply #16 on: September 28, 2010, 10:54:25 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
my_num=floor(random(4))
while my_num != old_num {
atk[0]=snd_atk1
atk[1]=snd_atk2
atk[2]=snd_atk3
atk[3]=snd_atk4
stp=atk[my_num]
sound_play(stp)
old_num = my_num;
}

Not 100% sure why but it sporadically doesn't play sounds >_>

I'm a sloppy coder btw
Logged
I do art
I ermmm... DID do art
I do art
Re: There's got to be an easier way to do this.....
« Reply #17 on: September 28, 2010, 11:17:20 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
This code will either play 2 sounds (1 of them being old_num) or 1 sound. That is because playing the sound is inside the while loop. There is also the problem of starvation, when my_num is never old_num.

Code: [Select]
//initialization
atk[0]=snd_atk1
atk[1]=snd_atk2
atk[2]=snd_atk3
atk[3]=snd_atk4

//code
var my_num;
var tries;
tries = 0;
do{
    my_num=floor(random(4));
    tries += 1;
} while( my_num == old_num && tries <= 10)
old_num = my_num;
sound_play(atk[my_num]) ;
The tries prevent starvation, but the problem is that there is still a small chance that the same sound is played twice.

But my suggestion would be:
Code: [Select]
//initialization
atk[0]=snd_atk1;
atk[1]=snd_atk2;
atk[2]=snd_atk3;
atk[3]=snd_atk4;
index = 0;

//code when playing
index = (index + irandom_range(1,3)) % 4; // <<--- Note that irandom_range is a GM8 function, otherwise use floor(random(3))+1 in GM7
sound_play(index);
This does not starve nor repeats played sounds twice.
« Last Edit: September 28, 2010, 12:27:00 pm by Niek »
Logged

DJvenom

super-sage
Re: There's got to be an easier way to do this.....
« Reply #18 on: September 30, 2010, 05:08:38 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
Thanks for the clarification Niek :) Also, thanks for pointing out irandom() in GM! I had no idea :D
Logged
I do art
I ermmm... DID do art
I do art
Re: There's got to be an easier way to do this.....
« Reply #19 on: September 30, 2010, 04:41:46 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
No prob. it's a new feature in GM8.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.026 seconds with 74 queries.