GameMaker: Studio

GameMaker: Studio

Find, play, and make games easily
Discover, rate, and download the best player-created games made in GameMaker: Studio for free. Or try making your own and share with the community. Click here to learn more.
Bean Burrito 17 Feb, 2015 @ 4:10pm
D&D to GML: Chance and Movement of Object
I've got an enemy:
Create (Event)
Piece of code (action)
Move Free ((random360),2) //starts out heading in a random direction (action)

Step (Event)
Chance of 20 (action)
Start Block (action)
Move Free ((random360),2) //changes heading in a different random direction (action)
End Block (action)

With this my enemy changes direction periodically, now if I try to do the same in GML:

Create (Event)
Piece of code (action)
motion_set(random360),2) //starts out heading in a random direction

Step (Event)
Piece of code (action)
action_if_dice(20){
motion_set(random(360),2); //changes heading in a different random direction (action)
}

The same thing, right? But the little guy spazes out! I'm guessing it's because it's running the thing every step, but even if I change the action_if_dice to 2000 it still goes nuts! What piece am I missing? I was thinking of putting an alarm in there to run at room_speed and then running this random, but I'd like to keep this all in one piece of code. Also, how can I create alarms inside code without adding a new event? Is it possible?
< >
Showing 1-5 of 5 comments
melon king 4 18 Feb, 2015 @ 1:31am 
Is that a direct copy paste? If so your Create Event's code is wrong ( "motion_set(random360),2)", missing bracket.)

EDIT: (May I also suggest using a variable as a timer? I.E. :
Create Event Code a = 0; //The variable will be used to count up Step Event Code a += 1; //This will add 1 every frame if a > 60 { //This is checking if it is above 60 (assuming your game is running at 30 fps (the default), this should be 2 seconds) motion_set(random(360),2); //move a = 0; //This is important as it resets the variable back to 0 (you don't want it to keep performing motion_set every frame! lol) }

You can add me if you want more help or something
Last edited by melon king; 18 Feb, 2015 @ 5:20pm
Bean Burrito 18 Feb, 2015 @ 5:25am 
Thank you for the input. I looked at the pdf that has all the D&D actions and the move free and move fixed were both motion_set(direction,speed) but when I put that code it was goign nuts. I think that missing brace was a typo, as I could run it. I've got two follow up questions if you'd be so inclined:

You've created the timing variable in the create event, but I'm using a script to run my movements for my enemies. Here's how it's layed out:
I have 26 enemies, in the Step Event I have excute script > scr_enemy_movement
Inside that script I have a switch statement that calls another script so
switch(object_index)
{
case(obj_enemya):
scr_zigzag();
break;

case(obj_enemyb):
scr_attack();
break;
}

This way all the movements are right there in one file. My question is can I still put the timing variable in the custom script (like scr_zigzag), and not in the create event? (I'm still learning about how these customvariable/global variables interact with the objects).

My next question is about move again. If I'm using the D&D move_fixed, it has a direction pad so I can, for example, click all four directions and create an alarm to run it again and the enemy will choose and move in one of those four direction each time the alarm triggers. BUT, it's just motion_set(direction, speed)! Where's the code for the choose a direction from the 4 provided?
melon king 4 18 Feb, 2015 @ 5:15pm 
Hmmm I think you'll have to add the "a"(or whatever you decide to rename it) = 0; in all the enemies' Create Events. A bit of copy-pasting will work haha. I can think of at least one way of not declaring the variable inside the script, but my teachers etc. think it's kinda improper.

For the second question, there isn't a function that only does the cardinal directions, but you can make another script (or just extend the code, but script would be better/simpler) that "rolls a 4 sided die" and picks one of 4 directions (90 degrees, 180 deg. etc). You could then replace all the motion_set(random(360),2) in your code/scripts with the new script.

Okay I realised that in this case, you can do an extremely simple "irandom(3)*90" as the direction so:
(I forgot to use [code] formatting in my last post)
motion_set(irandom(3)*90,2) //irandom will pick a random integer between 0 and 3, i.e. 0, 1, 2, or 3 (at least I'm pretty sure of it)

EDIT: I forgot to times the random integer by 90
Last edited by melon king; 18 Feb, 2015 @ 5:17pm
Bean Burrito 19 Feb, 2015 @ 10:12am 
Sweet. I can make all sorts of motions with that! I've also learned a bit about pathfinding, but while it's a strong tool, but I'm not sure how everything else will interact with it (like if I have a script where the enemy moves toward the player if the player gets too close).

Three more questions, if you're up for it!

I've got an object that when Left Pressed Event runs code
instance_change(obj_touchpurp,0); //Changes the object into a colorful explosion

And in the obj_touchpurp
Step Event
image_blend = make_colour_hsv(random(254),255,255); //Here I'm trying to change the hue to be a random color so the playing sprite will be different for each enemy clicked
image_speed = .75; //plays animation a little slower
image_xscale += .03; //slowly increases size
image_yscale += .03; //slowly increases size

Animation End Event
instance_destroy();

My two questions are:
1. If I set the make_color_hsv Saturation and Value to 255, does that mean it will not change? (I only want to hue value to vary, and the sat and val to not change)

2. In the step event means that image_blend is running all the time, and I get a psychedelic trip. I want it to pick a new hue, play the animation, and destroy. I can't put it in a create event because it's already created! *edit I figured out that instance_change allows me to change the preferences so that it will include a create, so I tossed the image_blend code in there and it seems to work, but I think the saturations and value are messed up.

Lastly, is there a way to make the enemies not stack on each other when they're following the player? I know of mp_potential_step(obj_player.x,obj_player.y,1,0); but just when they're wandering or doing their thing. (Maybe this is what I use? I don't know!)

I'm also going to be working on my player AI; I really want my player to automatically avoid the enemies. This means that I'll use the mp_potential_step, but set the speed to negative. The downside is that the player isn't that smart and lots of times ends up in the corner trapped!
Last edited by Bean Burrito; 19 Feb, 2015 @ 8:40pm
Bean Burrito 23 Feb, 2015 @ 1:55pm 
I've abandoned the make_color_hsv as there's no way to leave the Saturation and Value alone. The colors are washed out, or just ugly. So I just make the same sprite and adjusted the hue, saved it, and made more objects. Then when the object is clicked I just instance_change it.

I still dont' know how to not make them stack though, but I guess I can live with it for now.
< >
Showing 1-5 of 5 comments
Per page: 1530 50