GML Two Variables Can't Be The Same

J

JVGameDev

Guest
Hi! I want to make it where when the enemy object is created, either movingLeft or movingRight is equal to 1. If one is equal to one, the other is equal to zero. They can't both be zero, or both be one. I want it to be random. How do I do this?
 
A

arirish

Guest
You use one variable, called maybe dir (for direction). -1 = left, 0 = neither, 1 = right.
 
J

JVGameDev

Guest
movingLeft = irandom(1);
movingRight = !movingLeft;
I did...

Code:
//Create
dir = irandom(1);


//Step
//Move Left and Right
if dir = 0 && obj_enemy.y = 64
{
    x = x - 1;
}

if dir = 1 && obj_enemy.y = 64
{
    x = x + 1;
}
But the enemy always goes to the right.
 

Ziberteck

Member
I did...

Code:
//Create
dir = irandom(1);


//Step
//Move Left and Right
if dir = 0 && obj_enemy.y = 64
{
    x = x - 1;
}

if dir = 1 && obj_enemy.y = 64
{
    x = x + 1;
}
But the enemy always goes to the right.
I recommend making reverse movement -1 and change dir = round(random_range(-1,1));
This will set dir to a random number from -1 to 1, including 0, rounding it will make it an integer(otherwise it will have decimals).
 
A

arirish

Guest
Also make sure you call randomize() at the start of your game, otherwise it will always be the same.
 
J

JVGameDev

Guest
Ok, Update:

This is my Code:

Code:
//create 
dir = round(random_range(-1,1));

//Step
//Get Enemy Foward
if (obj_enemy.y < 64)
{
    y = y+1
}
//Move Left and Right
if dir = -1
{
    x = x - 1;
}

if dir = 1 
{
    x = x + 1;
}
//Change Directions!
if (place_meeting(obj_moveLeft.x, y, obj_moveLeft))
{
    dir = -1
}

if (place_meeting(obj_moveRight.x, y, obj_moveRight))
{
    dir = 1
}
In the step, the first part basically makes my enemy fly foward into the screen, after that thye should randomly either go left or right. Once they collide with one of the objects which are places on either side of my room, they are supposed to change direction

My problem: The enemy ALWAYS goes right and then once my enemy collides with the object they don't switch directions, they just fly out of the room.
 
T

tagomago

Guest
when writing the if statements, you are using single '='. Have you tried if dir == 0 / if dir == 1?
 
E

Edmanbosch

Guest
Ok, Update:

This is my Code:

Code:
//create
dir = round(random_range(-1,1));

//Step
//Get Enemy Foward
if (obj_enemy.y < 64)
{
    y = y+1
}
//Move Left and Right
if dir = -1
{
    x = x - 1;
}

if dir = 1
{
    x = x + 1;
}
//Change Directions!
if (place_meeting(obj_moveLeft.x, y, obj_moveLeft))
{
    dir = -1
}

if (place_meeting(obj_moveRight.x, y, obj_moveRight))
{
    dir = 1
}
In the step, the first part basically makes my enemy fly foward into the screen, after that thye should randomly either go left or right. Once they collide with one of the objects which are places on either side of my room, they are supposed to change direction

My problem: The enemy ALWAYS goes right and then once my enemy collides with the object they don't switch directions, they just fly out of the room.
The reason the enemy is moving right is because game maker always uses the same seed for every run of your game, meaning that it will always use the same value for anything random, like what you are doing here. In order to deactivate this, before all your code in the enemy create event, put the function randomize();.
 
J

JVGameDev

Guest
Well this is now my code
Code:
//create
randomize();
dir = round(random_range(-1,1));

//Step
/Get Enemy Forward
if (obj_enemy.y < 64)
{
    y = y+1
}
//Move Left and Right
if dir == -1 && obj_enemy.y > 64
{
    x = x - 2;
}

if dir == 1 && obj_enemy.y > 64
{
    x = x + 2;
}
//Change Directions!
if (place_meeting(obj_moveLeft.x, y, obj_moveLeft))
{
    dir = -1
}

if (place_meeting(obj_moveRight.x, y, obj_moveRight))
{
    dir = 1
}
Now the enemy literally goes forward and stays there. I know that has to do with the
Code:
 if dir == -1 && obj_enemy.y > 64
line, but I can say that even before that the character STILL only goes right and still won't switch directions when in contact with my switch directions object.
 

chamaeleon

Member
I have a hard time figuring out the intent behind place_meeting(obj_moveRight.x, y, obj_moveRight).. Are you sure you don't want plain x instead of obj_moveRight.x?
 
Top