• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Will any if relative work in GML in GMS 2?

GML stands for Game Maker Language



GMS stands for Game Maker Studio



Here is a working if relative GML example:
Code:
Create Event
hp = 12; //variable hp is equal to 12
hp_divided_by = 1; //will be used to help divide (reverse [opposite] of multiply) hp
hp_minus = 0; //will be used to help decrease hp
hp_plus = 0; //will be used to help increase hp
hp_times_by = 1; //will be used to help multiply hp



Step Event
if (hp_times_by <> 1) //if variable hp_times_by is less than 1 or greater than 1
{
  hp *= hp_times_by; //variable hp gets multiplied by the amount of whatever variable hp_times_by is equal to
  hp_times_by = 1; //variable hp_times_by is equal to 1
}
if (hp_plus <> 0) //if variable hp_plus is less than 0 or greater than 0
{
  hp += hp_plus; //variable hp gets increased by whatever variable hp_plus is equal to
  hp_plus = 0; //variable hp_plus is equal to 0
}
if (hp_minus <> 0) //if variable hp_plus is less than 0 or greater than 0
{
  hp -= hp_minus; //variable hp gets decreased by whatever variable hp_minus is equal to
  hp_minus = 0; //variable hp_minus is equal to 0
}
if (hp_divided_by <> 1) //if variable hp_divided_by is less than 1 or greater than 1
{
  hp_divided_by != 0; //variable hp_divided_by is not equal to 0 to prevent errors
  hp /= hp_divided_by; //variable hp gets divided by whatever variable hp_divided_by is equal to
  hp_divided_by = 1; //variable hp_divided_by is now equal to 1
}
if (keyboard_check_pressed(ord("D"))) //if D button is pressed (pushed)
{
  hp_divided_by = 2; //variable hp_divided_by is equal to 2
}
if (keyboard_check_pressed(ord("M"))) //if M button is pressed
{
  hp_minus = 2; //variable hp_minus is equal to 2
}
if (keyboard_check_pressed(ord("P"))) //if P button is pressed
{
  hp_plus = 2; //variable hp_plus is equal to 2
}
if (keyboard_check_pressed(ord("T"))) //if T button is pressed
{
  hp_times_by = 2; //variable hp_times_by is equal to 2
}
if (keyboard_check_pressed(ord("Z"))) //if Z button is pressed
{
  show_message("x: "+string(x)+"#"+"y: "+string(y)+"#"+"hp: "+string(hp)); //tells x and y position and hp amount # each on a different line of text
}
if (hp_times_by == 2) //if variable hp_times_by is equal to 2
{
  x -= 2; //go left by two pixels
}
if (hp_plus == 2) //if variable hp_plus is equal to 2
{
  x += 2; //go right by two pixels
}
if (hp_minus == 2) //if variable hp_minus is equal to 2
{
  y -= 2; //go up by two pixels
}
if (hp_divided_by == 2) //if variable hp_divided_by is equal to 2
{
  y += 2; //go down by two pixels
}


In GMS 2 would
Code:
if (hp += 17) //if variable hp is increased by 17
{
  hspeed *= 38; //variable hspeed (horizontal speed) gets multiplied by 38 times
}
work well or no?
 

kburkhart84

Firehammer Games
I'm not 100% what that last part would do. In C languages, IIRC, doing operations as conditions automatically returns true since it is assumed that the operation is successful. In GML, I don't know if it would work or not, but it doesn't really have a place. If you want to increase hp by 17, you can just do it and not worry about if it was successful or not. Most basic variable operations work like that, there is no need to verify if they were successful or not.

However, if what you are trying to do is verify if the variable's value had been increased since last time you checked(not do it, but check if it was already done), then you need to use a secondary variable and store the value there, finally comparing the old to the new to see if it had increased by the amount you care about(17 in this case). I mention this just in case that's what you are worried about.

I don't know exactly what the purpose of what you are doing is. I might be able to better help if I knew more detail on exactly what you are wanting.
 

FrostyCat

Redemption Seeker
This sort of "if relative" rookie pipedream syntax has never worked, and it never will. The problem has to do with what it is relative to. The value from last step? From when the instance was created? From when the game was started?

Save the old value in another variable first, then you can compare the current value to it later. For example:
GML:
var base_hp = hp;

/* Apply changes to hp here */

if (hp >= base_hp+17) {
    /* 17 or more points have been added to hp */
}
 
Top