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.

call class method?

Do i have to create an object of a class before i can reach the methods?

I just tried this:
class Atest
def testmethod(qId)
p qId
end
end

call it like this:
Atest::testmethod(15)

But i get errors saying the method does not exist.
 
MeisMe said:
You create a class using it's .new method, as seen here:
Code:
atest = Atest.new
atest.testmethod()
All objects have this method, so you don't need to create it.

"new" creates an object of a class. Not the class itself. From what i have read. You can pull methods from RGPXP other classes like:
RGP::method.

I would be very surprised if you can´t just call the methods from the class. Because in almost all languages you can do that.
 
MeisMe said:
Okay. You want a class within a class or module. Take the RPG module, for example. RPG::Actor is the class that hold a database actor.
I replaced the "class" with the "module". But i can´t get it to work. I Call the method with the module::method but i get that the method does not exists.
 
Just use "Class.new.method".

And to call a module's method from another class, the method has to be defined as "self.method_name". For example:

Code:
module Testing
  def self.test
    p("Testing!")
  end
end

class MyClass
  def initialize
    Testing.test
  end
end
 
Daniel said:
Just use "Class.new.method".

And to call a module's method from another class, the method has to be defined as "self.method_name". For example:

Code:
module Testing
  def self.test
    p("Testing!")
  end
end

class MyClass
  def initialize
    Testing.test
  end
end

This is not for an acutal project. It´s my way of exploring the Ruby Game Scripting. And i must say i found it a little strange that you have to create an object of a class do reach another method.

It all comes down to that you HAVE to...sooner or later create an object.

Why i feel this is strange is because it´s not necessary in some cases. In all other languages i worked in you can call a class method whitout creating and object first.

Strange OOP methology. But i guess that RGSS is not the whole Ruby scripting but a modified version of Ruby Scripting.

Thanx for the answers. Now i know.
 
MeisMe said:
You can use a module.
Code:
module Test
  def self.method(argument)
    p argument
  end
end
Test.method(15)  # Prints 15

Yeah, that was what i looked for. the "self" did it. And from what i can see there are no new objects created. Just a call to a class method whitout creating an object. Cool THANX!
 
frankie2000 said:
Yeah, that was what i looked for. the "self" did it. And from what i can see there are no new objects created. Just a call to a class method whitout creating an object. Cool THANX!
You know I posted the exact same thing right?

And your original question was to call a class method, so I'm guessing you can't do that without creating an instance of the class.
 

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