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.

[Resolved] Strange. It doesn't work.

Status
Not open for further replies.

Aran

Member

I was testing to see how I can set parameters to a script from an event, bot I got a No Method Error, and it kinda confuses me...

I put this in the script editor:

Code:
module Container
  
  data = []
  
  [COLOR=red][COLOR=blue]def[/COLOR] add(something)
[/COLOR]    data.push(something)
  end
  
end

And put this in the event:

Code:
Container::add("Mama")
print Container::data

and when i tested it, I got a no method error for the method "add", when it's right there in red.
 
* hits head *

Code:
module Container

  Constant = 15
  
  @data = []
  
  def self.add(something)
    @data.push(something)
  end

  def self.data
    return @data
  end
  
end

Code:
Container.add('something')
Container.add(1)

p Container.data  -> ['something', 1]
p Container::Constant  -> 15

You use :: to access a constant or submodule. You use . to access a method. If define a method with self.method_name in modules.
 
not quite true actually,

Code:
module Test
  module_function
  @data = []
  def data
    return @data
  end
end

p Test::data # returns []

It works. Even if there are arguments.
You shouldn't forget to put 'module_function' before your methods or name them 'self.method_name' though.
However, you can't call constants with a dot...

But you should still use . to call your methods. It makes it more clear for other people who'll read your scripts...
 
Ahh yes. The old 'module_function'. I guess I never really use that, since like you said, it's just more clear.

Since we are on the subject:
Code:
module Something
  def one
    return 1
  end
  def two
    return 2
  end
  module_function :one, :two
end

p Something::one, Something::two  -> 1, 2

You can set module_functions like this as well. Just module_function :method_name, :method_name, ...
 
yeah, module_function works like attr_

but you can also do it like this :
Code:
module Test
  @data = []
  def self::data
    return @data
  end
  def self::add(value)
    @data << value
  end
end

Test::add(1)
p Test::data # returns [1]

note that it's that way that you define [] and []= methods for modules...
 
And no-one but you too reply XP...

Adn erm.. I DO have a question, can't you just use instead of
p Something::eek:ne, Something::two -> 1, 2

Something.one ? I thought You could...
 

Aran

Member

Oooooh! I see now! the 'method_function' part still confuses me, but then again I'll just continue to read the post over and over again till I get it.

But why do we do self.method_name?
 

Anonymous

Guest

It makes the method a static module method. In other words, you do not need an instance of an object to call that method. Refrence the RGSS module Input, the static module methods #trigger?, #repeat?, etc. can be called using Input.trigger?, Input.repeat?, etc, without an object of type Input.
 
Status
Not open for further replies.

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