GML -> check for a colliding instance's variable, then do action - game-maker

I've been trying to do some GML scripting but got totally stuck at some point.
I want enemies to attack my main character but without overlapping. So, I would say.
//enemy is moving left-to-right...
if place_meeting(x+1, y, enemy){ //if there's a collision with another enemy
if (other enemy).is_attacking{ // ???
// checks if the colliding enemy is attacking, if true, it should attack as well...
is_attacking=true;
}else{
//walks
}
This is an image that describes what I'm trying to get (note that enemies know they should be attacking even though they're not in direct contact with the main character, just because an enemy besides is attacking)

I was finally able to do it thanks to the instance place function.
I will post the code in case someone need somethin similar
if place_meeting(x+5, y, malo){ //malo means enemy on spanish, i use to write multilingual code, lol :P
var inst;
inst=instance_place(x+15,y,malo); //x varies on sprite size. it basically returns the unique id of the object that's 15 pixels on the right of self (malo)
with (inst){
if (is_attacking){
other.is_attacking=true; //if collided enemy is attacking, then this(other) enemy should attack too. search more about the width statement if you don't catch this part
}else{
other.is_attacking=false;
hspeed=1;
}
}
}else if place_meeting(x+3, y, character){
is_attacking=true;
}else{
is_attacking=false;
hspeed=1;
}
and the result

Related

Hi, I need an algorithm to tell a sprite to change the image when the in-game text finishes appearing (in GameMaker studio 1.4)

I need an algorithm to tell a sprite to end as soon as the text finishes appearing, basically I need to make a cutscene which describes the beginning of a story within the game, but since I'm using gamemaker, I don't know how to do it, can someone help me?
For cutscenes and automated image sequences you usually have some sort of variables, that handle the states and counters for displaying sprite's sub-images.
But firstly I like to use mainly two main time-related variables in my controller object usually named sys (counter and counterTime):
c = 0; // and for stepEvent: c += 1; // so it goes +1 per step
ct = 0; // and for stepEvent: ct+= 1 / room_speed; // so it goes +1 per second
During the cutscene you might want to stop everything else that moves:
is_cutscene = false; // objects might not be able to move when this is true
// you still have to implement that by yourself
// player can't move if is_cutscene, nothing happens!!
(except cutscene buttons etc...)
So now when the player gets to a 'cutscene'y object and triggers some cutscene_1 = true eg. you can have the image/text/sound/animation object come to life/create/active... That object might have in the create event:
duration = 6; // 6 seconds the scene lasts
start_time = sys.ct // and in Step: if sys.ct > (start_time + duration)
// then -> cutscene advance/destroy
Ofcourse - this is super simple and now you could only say implement:
walk close to a pop-up object
show an image for 6 seconds
it dissappears
And it might not be big trouble to implement it... BUT THIS may also be the foundation for more advanced multi-step sequence and cuts.

Enemies Overlapping in Game Maker: Studio, How Do I Fix This?

The AI of my enemies that I made for my game is simple. They just follow the player (more precisely, they look in the direction of the player and go forward)
Step Event:
if (instance_exists(obj_player)){
direction = point_direction(x,y,obj_player.x,obj_player.y);
}
speed = spd;
But they keep on overlapping each other and go on top of the player. I've tried researching but all the forums said was to use place_free() and xprevious & yprevious, but I have no idea how to use them. How do I fix this?
Thanks :)
You can read about this on the gamemaker documentation : https://docs.yoyogames.com/source/dadiospice/002_reference/movement%20and%20collisions/collisions/place_free.html
basically, what you want to do is avoid moving your instance if that means causing a collision. x_previous and y_previous will be used to cancel the move by going back to the previous position.
But I think it's better to check the place before moving, so I would add at the end of you script :
if (place_free(x+hspeed, y+vspeed)) speed = spd;
else speed = 0;
that way, the ennemy will stop instead of stepping above an other instance.
A little upgrade would be the following : if there is a collision detected, check if you can move along a single axis instead (x or y) and do it.

How to make car add points/laps as it goes over finish line?

I am programming a simple top-down car game on game-maker where the purpose of the game is to get as many points/laps by driving the car around the oval shaped track.
The car drives with speed = 5 and changes direction with the code direction = direction + 2;
image_angle = direction;
If you couldn't tell already, i'm new to coding however i have been searching for a solution for at least a fortnight and haven't found anything- How can i make the game add a point every time that the car goes through the finish line? I suppose there would have to be a collision event between the car and the finish line but i do not understand the code that would take place in order to add a point and if I did code the game to add a point i anticipate that there will be a bug where by several points will be added (instead of 1) when the car goes over the finish line as it is "colliding" the whole way as it goes over.
How can I make a point be added every time the car goes around the track?
If you need any further information i'd be glad to help. Thank-you.
With Kake_Fisk's help i set up 3 midway lines around the track and created a collision event between the car and the midway lines: prntscr.com/c4uecl - the problem is that add_point doesn't actually do anything and i would be glad to have some help. Another problem i am encountering is that he car was originally meant to turn with the mouse button 1 but when i tried that the car only turned when i aimed on it if you get what i'm saying, do you have a solution for that also?
obj_car:
creation event:
points = 0; //instance variable
step event:
if(place_meeting(x,y,obj_line)) {
points += 1;
}
if(points == 3) {
room_goto(rm_win);
}
First, you have an other problem you need to think about. What stops the player from just driving forth and back over the goal line? You would need to add invisible lines throughout the track. If all these lines have been passed, you know the player has driven the car through the whole track.
You want to trigger some code once, when the car hits the goal line. By utilizing the previous system, this can be done quite easily. In collision with goal line, use something like this:
// If all the invisible lines in the track has been visited
if (midway_lines == 3)
{
// Sets the variable back to 0 so this piece of code only gets executed once
midway_lines = 0;
add_point();
}

How do I restart the current room to what it was when the player entered it?

While making a game, I had set it up so when the player dies, the game resets. Feeling this was a bit too harsh, I did some research and found the room_restart() code, which is meant to restart the current room. But, when I input it and triggered it via dying, it did not reset the room to how it was. How do I reset it?
{
room_restart()
}
That is the code that triggers on collision with an enemy.
You would have to record the state somehow. Game Maker has a native state save function, but if you wanted a room to stay the same without changing the player's inventory or something, you'd have to manually record the state of every object in it so when the player leaves and comes back, everything resumes where it was.
You could try having each object record important variables to a file with sections for each room. For instance, you could use a JSON file with sections for each room, and each object would record their vital data into that section. For example:
{
"rmHouse": {
"Mom": {
"x": 64,
"y": 128,
"action": "lookingDown"
}
}
}
Check around for Game Maker JSON extensions. Here's one for GM8.1/Studio: http://gmc.yoyogames.com/index.php?showtopic=565659
room_goto(room);
That will send the room back to how it was when you first entered it, but room_restart should do the same.
Make sure that you do not have "persistent" checked in the room options. That's what sounds like it's probably the issue to me.
If you had
if(health = 0) {
instance_destroy();
}, then delete instance_destroy. Replace it with x = 32
y = 64
health = max_hp
restart_room();
so that the X position is set, the Y position is set, the health is back, and the room restarts.

Gravity algorithm help needed

I'm using Slick2D and I was wondering if anyone knew how to tell if the player collided with the ground or a wall. So far I have it where it checks if the player hit something solid and if so it stops. What do people normally do?
Well there's one easy way to create the object on the screen falling down.That's what I normally do.
fallingdown = true;
if(fallingdown){
imageY++; (or Y-- im not really sure about this..)
}
Okey, know our image is going down.Next thing we wanna is to collide when reach's the end of the screen.Let's say my Y of the screen is 200.
if(imageY >= 200){
imageY == 200;
}
Well...this is going to help you for the collision because it's not really easy to explain. http://www.youtube.com/watch?v=1qZk6dIW46Q

Resources