|
Post by Noadi on Nov 21, 2003 0:47:20 GMT -5
Simple example and script made with 5.0. When you download the example just watch the little butterfly. Pretty simple but it's something virtually all rpgs need. members.lycos.co.uk/diona/stuff/wander.zipHere's the script, put it in the step event. I have speed set to 11 because that's what my sprites look good moving at, you should change it depending on your game. This should work fine with depth scripts and for colisions with solid objects (well it works the way I do solid objects). //random movement script arguments //argument0 = Up Sprite //argument1 = Right Sprite //argument2 = Down Sprite //argument3 = Left Sprite //argument4 = Max Steps //movement to be put in step Dir=ceil(random(4)) Steps=ceil(random(argument4)) if(Steps==argument4) { if(Dir==1) { sprite_index=argument0 direction=90 speed=11 } if(Dir==2) { sprite_index=argument1 direction=0 speed=11 } if(Dir==3) { sprite_index=argument2 direction=270 speed=11 } if(Dir==4) { sprite_index=argument3 direction=180 speed=11 } }
|
|
|
Post by IsmAvatar on Nov 21, 2003 16:20:05 GMT -5
you shouldn't use the ceil function, because it has a chance of returning zero. of course the chance is very small, but it can still happen. you should instead use floor()+1
anywhom, it's a great script. I aught to release my grid AI random movement script, too.
|
|
|
Post by Noadi on Nov 21, 2003 17:24:08 GMT -5
Actually it isn't really a problem if it returns 0 (all it does is just keep moving in the same direction for a little longer until the max number comes up again) it won't mess it up at all and is just easier to deal with.
The part that says Steps=ceil(random(argument4)) it doesn't need to be ciel(), it could be floor()+1 or just round(), or could be replaced by an alarm, any bit of code that will produce a way for the direction to change will work, I just piced the first one I though of.
|
|
|
Post by Antman on Nov 21, 2003 17:46:02 GMT -5
I decided to test this to see how small the chance is... I started a new project and created a new object. I added this in the step event: tries += 1; i = ceil(random(2)); if i = 0 { show_message('After ' + string(tries) + ' tries, ceil(random(2)) returned zero.'); game_end(); } I added this in the draw event: draw_text(50,50,'Tries: ' + string(tries));
I made a new room and set it's speed to 999. I ran the project, and so far, it's tried about 2,000,000 times and it hasn't returned 0. So either the chance is very, very, very small, or it never happens. Unlss i've done something wrong.
|
|
|
Post by IsmAvatar on Nov 21, 2003 18:42:16 GMT -5
Oh, let's not start this argument again. Like I said, the chance is very small. It has to be exactly 0, not 0.001, but 0.0000 exactly. I'll calculate the chances of getting 0 in a second. hang on.
Just tried it out, and it's changed a bit since 5.0, appearently. 5.0 used to have about 7 digits or so. 5.1 only has 2 digits. And sometimes it rolls a 1, in which the floor of the number still returns 0. i guess what it's doing now is it rounds the original number up, to 2 decimal place accuracy. I can no longer calculate what the chances of rolling zero are, but i think there may still be a chance. unless perhaps the new 5.1 uses ceil as it should, and rounds a random 0 up to 1. I'll see what I can get in 5.0.
oddly, 5.0 has it too. I'm not sure where I got the idea, possibly 4.3, but i didn't use it for too long. oh well. I think there is still a chance. It's just like 1 to 10000000 or something.
|
|
|
Post by Noadi on Nov 21, 2003 21:09:21 GMT -5
Whether it does or not it won't effect the way the script runs so it isn't a problem.
|
|
|
Post by sirthaddeus on Nov 21, 2003 21:50:24 GMT -5
I really like the wandering example. It will help me because I have lots of npcs that need to wander, lol. Anyways keep up the good work.
modified by IA: fixed up the spelling.
|
|
|
Post by AltiusBimm on Dec 19, 2003 3:55:50 GMT -5
Not bad at all, but I personally prefer to have all rotations in the same sprite. (Yes that can get hard to keep track of, but not really too much.) ;D
|
|
Lupus
Active Newbie
Wolf Warrior
Posts: 5
|
Post by Lupus on Dec 22, 2003 22:35:08 GMT -5
Thanks, this is useful. It's better to watch "Alive" than "Dead" NPCs.
|
|
|
Post by JunkMan on Jan 6, 2004 6:54:52 GMT -5
Naodi, Can you make that script so it won't go any further then lets say 100 pixels from the start position? That would be great, 'cause I have a few very important NPC's, and with this script, they sometimes run off
|
|
|
Post by IsmAvatar on Jan 6, 2004 15:38:59 GMT -5
just use point_distance and start_x and start_y, along with an if statement (duh)
|
|