Setting Min and Max?

BackUpAndDownBackUpAndDown Member Posts: 685
edited November -1 in Working with GS (Mac)
Hey could someone explain how to use the "min" and "max" commands?

Thanks ^_^

Comments

  • frariofrario Member Posts: 164
    It tells you the lower or higher number between the submitted numbers, expressions, attributes: min(67,11) = 11
  • BackUpAndDownBackUpAndDown Member Posts: 685
    So if I wanted to make a rule to make something happen if the players X position didn't equal any numbers in the range of 50to150 I would put...

    if attribute self.positionX doesn't equal min(50,max(0,150)) then...

    Right?
  • firemaplegamesfiremaplegames Member Posts: 3,211
    They allow you to set limits programmatically. And generally allow you to use less Rules, thus helping performance.

    They work very simply like this:

    min(number1, number2) will compare number1 and number2 and return to you the smaller one.
    max(number1, number2) will compare number1 and number2 and return to you the larger one.

    That doesn't seem all that useful, so here is how you would use it:

    Let's say you want to be able to drag an actor on the X axis away from a certain point. Like a slingshot.
    But you don't want to be able to drag the Actor more than 300 pixels.

    You could just set up your dragging code normally, and then put a Rule that checks if the distance is more than 300. If it is more than 300, then Constrain it to 300.

    That would look like this:

    self.maxDistance = 300

    Rule
    When Touch is Pressed
    .....Change Attribute: self.dragging To True
    Otherwise
    .....Change Attribute: self.dragging To False

    Rule
    When self.dragging = true
    .....Constrain Attribute: self.Position.X To: Mouse.X

    Rule
    When self.maxDistance < (game.slingshotBaseX - self.Position.X )
    .....Constrain Attribute: self.Position.X To: (game.slingshotBaseX - self.Position.X )

    Which will work. But now you have an extra Rule and an extra Constrain. Both of which are processor intensive.

    A better way would be:

    self.maxDistance = 300

    Rule
    When Touch is Pressed
    .....Change Attribute: self.dragging To True
    Otherwise
    .....Change Attribute: self.dragging To False

    Rule
    When self.dragging = true
    .....Constrain Attribute: self.Position.X To: min(Mouse.X,(game.slingshotBaseX - self.Position.X ))

    You can also combine min and max together like this:

    Let's say you have a slider that controls the volume in the game. You could set it up like this:

    Rule
    When Touch is Pressed
    .....Change Attribute: self.dragging To True
    Otherwise
    .....Change Attribute: self.dragging To False

    Rule
    When self.dragging = true
    .....Constrain Attribute: self.Position.X To: max(self.leftLimit,min(self.rightLimit,Mouse.X))

    That code will allow to drag a slider without using other actors as physical walls, or extraneous Rules to correct the position.

    Similar code can be used to eliminate invisible, unmovable Actors set up as walls on the sides of your game to keep Actors within a certain area. That's 4 less Actors you'll need in the physics system, freeing up more resources.
  • BackUpAndDownBackUpAndDown Member Posts: 685
    Thanks FireMaple! That explained it perfectly (and then some). ^_^
  • frariofrario Member Posts: 164
    Thanks firmaple, very useful!
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    It's great to be in FMG's virtual classroom once again! I've missed it!
  • UtopianGamesUtopianGames Member Posts: 5,692
    Excellent as always Joe!

    Daren.
Sign In or Register to comment.