Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Redefining operators like += and -= for certain objects?

Good day everybody!

After updating certain scripts, there's something in my personal Window Movable module that I've always wanted to do. Now, to move a window, I can use either of these commands, which do the same thing (then 'update' recalls the method continuously until the self.x == @new_x)

Code:
@window.x_to(320)
@window.new_x = 320

But then, I've created other methods like x_plus and x_minus, which is something like this

Code:
def x_plus(n)
  @new_x = self.x + n
end

But, what if I wanted to enhance the @new_x (or @new_y or whatever) so that I could do the same thing via...

Code:
@window.new_x += 320 # Translates to @new_x = self.x + 320

Is there a way I can do that? Basically, I need to modify a property's operator methods, like @new_x += ? and @new_y -= ? and stuff like that. In other words, I'm trying to get rid of the dependancy on the x_to(), y_to(), fade_opacity() type methods, since all they do is set instances (and share the same @delay instance for move speed), and just redefine their operators to mimic my example.
 
Have you tried
Code:
def new_x+=(n)
  # your code here
end
def new_x-=(n)
  # your code here
end
I've never tried it before, except for the =, so I'm not suree if these 2 methods will work.
But the = method will also take + and - into consideration.
 

Zeriab

Sponsor

Code:
object += value # Is the same as
object = object + value
You just have to define +(value) methods for the class the object is an instance of.

Here's an code example which hopefully can help you understand how to do it:
Code:
class Foo
  def initialize
    @bar = 0
  end
  
  def +(val)
    @bar += val
    return self
  end
  
  def -(val)
    @bar -= val
    return self
  end
  
  def inspect
    "Foo = " + @bar.to_s
  end
end

class Bar
  attr_accessor :foo
  def initialize
     @foo = Foo.new
  end
end


a = Foo.new
p a
a += 4
p a
a -= 24
p a

b = Bar.new
p b
b.foo += 2
p b
b.foo -= 4
p b

*hugs*
- Zeriab
 
How exactly do I tell which instance variables are being += or -= from? Theres only 5 or so instances that I need this behavior for, because they're special to the Window Movable script, but they're all Window-class properties, so I can't be doing self.width + 320 because that would be self.width = self.width + 320 and thats not the kind of behavior I want all the variables to do, just certain ones.

The 'movable' instances on the other hand will follow their own rules for the += / -= command though, because self.new_x += 5 would mean that it would slide over 5 pixels from its current spot, or self.new_o -= 160 means that opacity would fade from current to minus 160. I don't know how to do it only for @new_x, @new_y, @new_o, @new_co and @new_z.
 

e

Sponsor

You can specify singleton methods on objects like this :
Code:
def myObject.method_name(arg1, arg2, ..., argN)
  puts "I am SINGLETOR!"
end

That method would be available only the object myObject.

In your case, it could be:

Code:
def @windowA.+(aNumber)
  self.width += aNumber
end

Or whatever. I didn't read the whole topic. Just Google Ruby Singleton Methods for more.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top