[Resolved] Color/bitmap hidden classes logic usage

Started by Apidcloud, August 19, 2011, 11:34:47 am

Previous topic - Next topic

Apidcloud

August 19, 2011, 11:34:47 am Last Edit: August 20, 2011, 12:05:49 pm by Apidcloud
Hey there, I'm with some problems to understand something on bitmap class.

There's a method on bitmap, named get_pixel.
As far as I know, this get_pixel returns a Color.new ( a different class ), something like this:
def get_pixel(x,y)
color=@bmp.get_rgba(@bmp.get_pixel(x,y))
return Color.new(color[0],color[1],color[2],color[3])
end


Even without this piece of code, I was almost sure that it was returned color.new, because after get_pixel we can use alpha, red, green and blue, that are Color class parameters.

Well, I figured that they can be easily called like this because in first place, it is called Color.new .
They used attr_reader or something to get the numbers, but I wanted to know how to access a method with this logic.

Imagine something like this:

def get_pixel(x, y)
 #code
 return AClass.new(arguments)
end


Then, when I'm calling get_pixel method, from any bitmap, I wanted to call a method from that AClass.
The reason that it has arguments is just because I want to change the value, so it will be used on the method that I wanna call.

self.bitmap.get_pixel(1, 2).another_method

Basically I want this another_method to use the AClass argument, but I can't call the damn method after get_pixel lol
I tried using def self.another_method and such, but it doesn't work =/

Any ideas of how to solve this? Sorry if I didn't explain well, but I don't know any other way of how to explain it xD
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

alias custom_get_pixel
def get_pixel(x, y)
  original = cutom_get_pixel(x, y)
  # Do what you need here with the "original" variable
  return modified
end

 
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

Maybe i didnt explain myself correctly XD

I need to call a method like this:

bitmap_method.method_that_i_want_to_call

I gave get_pixel.alpha as an example, but in this case, alpha is just a parameter, that is read.
All i want is a way to call a method instead of that parameter for example :P

Thanks :)
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

Why not just make a method in the bitmap class?

class Bitmap

  def my_method
    # do some stuff
    # return value if you_need_to
  end
end
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

I already did that, but I need to use a method from another class as well.

Example:(I will continue with the get_pixel)

self.bitmap.get_pixel(x, y).alpha

This returns the value of the alpha.
This happens because get_pixel(x, y) returns a Color.new . Alpha is a attr_reader from Color class, that's why it returns its value.

Now, I don't want that 'alpha' (just an example) to be an attr, I want to call a method from that class instead.
Imagine something like this:


class Color
  def method_to_use
    #stuff
  end
end


get_pixel(x, y), the bitmap method returns a Color.new right? Now, instead of using '.alpha' next to that(it is a attr_reader, that's why it is returned), I want to call method_to_use, that I created on Color class.
This method_to_use isn't an attr as the alpha...that's why I created the topic, to see if some1 could tell me how to access the method, just as I explained on the example :P

Also, I've to say that on get_pixel(x, y) I need it to return the class.new as well, so I can fill the parameters I need xD

Thanks a lot
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

You can do something like this.  Say you wanted to get the alpha at pixel, but didn't want to use a call on a call:

class Bitmap

  def alpha_at(x, y)
    return get_pixel(x, y).alpha
  end
end


attr_accessor, attr_reader, and attr_writer are merely shortcuts, they actually create methods. When you make an attr_accessor, you are actually defining two methods for the variable.

attr_accessor: :my_variable


...and...

def my_variable
  return @my_variable
end

def my_variable=(value)
  @my_variable = value
end


Are exactly the same thing.  The attr_accessor just saves you from having to write this out for everything.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

August 19, 2011, 06:07:53 pm #6 Last Edit: August 19, 2011, 07:29:29 pm by Apidcloud
I know that attr's are a shortcut but the method that I wanna call doesn't work as one xD
I just want to 'start' the method eheh

self.bitmap.method_in_bitmap.method_in_another_class

I want it to start the method_in_another_class, by returning the another_class.new on method_in_bitmap

I need it to return the another_class.new in the first place so I can transfer the arguments that i will use on another_class_method
If it was just one method, it'd be ok, but it doesnt need necessarily to be another_class_method, there will be plenty of them, that will use different arguments :P

At the moment im trying something like this, by not using arguments on the initialize

class Aclass
 def initialize
   @calpha = calpha
 end
 def method_to_call=(value)
   #code that uses @calpha array -> note that at this point, @calpha was changed into an array
   #branches to return true or false
 end
 attr_accessor(:calpha)
end

class Bitmap
 def method_in_bitmap(arg)
  #code
  call = Aclass.new
  call.calpha = alpha (calpha is an attr_accessor in Aclass class ; alpha is actually an array)
 end
end


When I want to use that Aclass method I do: if self.bitmap.method_in_bitmap(#an integer).method_to_call == 0 (example only)

Well, this raises an error of NoMethodError: method_to_call
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

You can use the "wildcard" symbol for the arguments.  It allows you to pass as few or as many arguments to a method.  Every argument will be stored in an array.  Here's an example:

class Bitmap

  def create_and_return_class_instance(klass, *args) 
    if args.empty?
      return klass.new
    else
      return klass.new(*args)
    end
  end
end


Passing an array with the wildcard symbol to an method/object will pass each item in the array as an argument.  You can do stuff like this:

arguments = [255, 160, 255]
color = Color.new(*arguments)
p color.green #  160
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

August 19, 2011, 07:28:28 pm #8 Last Edit: August 19, 2011, 07:37:08 pm by Apidcloud
I was wondering how to use 'wildcard' symbol for arguments but in my case how will that help? The alpha array is created with lots and lots of numbers that have nothing to do with the method argument xD
The problem here is the NoMethodError that is raised xD
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

InfinateX

Just pointing this out just in case it's a mistake but you klass.new

Just saying in case it's class.new
I made a signature!

Apidcloud

looooool'd xD

It doesn't matter xD it's just an example of the returned class :P
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

Its not a mistake, its an argument.  You pass the name of the class.  "class" is a reserved word in Ruby, and cannot be used as a name of an object. Believe it or not, I know Ruby inside and out pretty well.  Although I have no idea what it would be used for, the code above would allow you to do something like this:

b = Bitmap.new(32, 32)
object = b.create_and_return_class_instance(Game_Party)


b = Bitmap.new(32, 32)
object = b.create_and_return_class_instance(Color, 255, 160, 0, 24)


This would make object an instance of Game_Party. Tell me exactly what you want to do, and I will give you code on how to do it, and you can follow it from there.  Apparently all these abstract explanations are being lost on me.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

Can I send my code via pm to you? lol
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

ForeverZer0

Yeah, that's no problem.  Include a brief description, or some comments explaining what you need.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Apidcloud

August 20, 2011, 09:28:08 am #14 Last Edit: August 20, 2011, 12:01:40 pm by Apidcloud
I will work on that right now :P give me some minutes ^^

@Edit:

To everyone understand the problem here, I'm going to post a brief explanation of the result that I want.
I was trying new stuff, and I came across of this:

self.bitmap.get_pixel(x value, y value)


Well, get_pixel is a method from Bitmap class, an hidden class more exactly. It returns a Color, more or less like:

return Color.new(color[0],color[1],color[2],color[3])


Although, I saw a line in someone's script that had:

self.bitmap.get_pixel(x value, y value).red


After seeing this, I obviously tested, to see the result. The result was the value of the red colour from the pixel with the x and y defined on the previous arguments. I started wondering how the hell, from a bitmap method, I would be able to receive a parameter from another class, in this case from Color class, another hidden class.

The guess from the return of get_pixel was entirely correct, it returned a Color.new obviously.
After that, it was clearly easy to understand that it was the way of accessing the Color attr readers, which can be red, green, blue and alpha.

When I tried to simulate that process with another bitmap method, I was able to return exactly the values, because they were attr_readers.
Although, I wanted to get further than that, and I began trying to call a method instead of the attr_reader value.
That's where my problems started, it keeps raising an error of NoMethodError.
I already tried to make the method I want to call, from the class that was returned on the Bitmap method, in different ways...

When I started trying, I thought that I would be able to do it by using 'self.method_from_the_other_class' but didn't work :P

Basically, the method from bitmap that i created, generates an array that needs to be passed to the other class. Now you say, it can be easily passed by using arguments on the initialize. And this method just needs to return that class.new(arguments that I need to pass to this class)
Yeah, that's true, but I already tried that, besides, I already deleted all the code over and over again to remake it, so I wouldn't be stacked by trying to find the error. No results at all, always the same error...

Any ideas of how to solve this? xD

@Edit2:
Lol I found the error I guess.
the method I wanna call is 'def alpha=(number)'
I thought by using this I would be able to make a condition, like 'if alpha == 0'

I just know 1 writing method that approaches of that, it is.

def ==(oVal)
       if oVal.is_a?(Integer)
           #@value is  a variable defined in the class where this method is defined
           if(oVal == @value)
               return true
           else
               return false
           end
       end
   end


Well, this won't work because @value is set on the damn same class <.<
As this will return true or false due to the value...I'll use 'def method(number)' instead...

Basically, all this mess up was due to my epicness of setting 'nothing' on that method ._.

Anyways, it was worth because I learnt a new thing from array's and the 'wildcard' symbol thing :P All taught by ForeverZer0, so thanks a lot :)
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit