Castlevania Dungeon Forums

Off Topic => Off Topic => Topic started by: TheouAegis on May 17, 2012, 06:31:48 PM

Title: Possibly very useful important tip for Game Maker users
Post by: TheouAegis on May 17, 2012, 06:31:48 PM
This might not be new to some people here, or maybe it's new to all of us here. I know it's in other languages, I think, like C or something, since I think it's where I got the idea from.

So I was thinking today, you can put a function like instance_create() inside parentheses in order to use it as an operand in conditionals or formulas. For example, if (instance_create(x,y,current_weapon)).object_index==obj_HexMiss is a valid conditional. The simple question is, why does that work and not if instance_create(x,y,current_weapon).object_index==obj_HexMiss and the answer is just as simple: the parentheses tell GM what operation to perform first. This is a computer, so instance_create() is still technically a mathematical operation and it therefor follows that parentheses are needed to tell it to be performed before the rest of the operation.

So I asked myself, why can't the same be true for a conditional like x<4? So I ran a test. My hypothesis was if x<4 was the same as if 1 if indeed x was less than 4; therefor (x<4) should also be the same as 1 or 0 any time it was used. So to test this, I ran in debug mode and created the global variable a. I then set a=-1. Then I watched the expression 4^$FF*(a>-1). Now, since a was set to -1, the expression, if it was indeed valid, should return 4^0, which is 4. Sure enough, that was the case with no error displayed. So in theory, setting a=1 should change the result to $FB. That indeed was the case.

So in short summary, you can use conditionals, which return 0 or 1, or any function/script which returns a value (most built-in functions in GM do in fact return a value) within any mathematical operation in GM.

respect += (respect>35)