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.

Font Help (Still unsolved)

Since the first one I make it's gone.

How can I change the text font without getting an Error Message also without eventing.. I know it's in either Game_Temp or Main. But when I put it it's starts an error message :(
 

khmp

Sponsor

Neither. The module that defines this information or rather holds it is contained within Font. And I guess the font that is used within the various Windows in the game is set in the hidden class Window.

Code:
module Font
  begin
    Font.default_name = 'Arial'
    Font.default_size = 22
  end
end

Just place that code in an empty section above main in the script listing.
 
No thank for your help. I know the problem is

begin
  # Setting up Font settings
  Font.default_name = ["Times New Roman","Algerian"]
  Font.default_size = 17

These 4 codes are required or at least 3.. Thank you
 

khmp

Sponsor

Well there is a difference between setting Font.default_name to a single string, and setting it to an array of multiple strings. Although I'm not sure if the window makes the determination to use the first value in the array if it's not specified. What is the error message that you are given?
 
The determination is done at the moment of attribution. It´ll look at the Windows/Fonts for the fonts listed in the array, from the first to the last name in order (0...array.size). If it findsa valid font, it attributes the Font.default_name to that value, and if it does not find any valid font, raises an error.

Also, khmp, Font is not a module. It´s a class. And those begin/end tags seems useless unless you put a rescue there to continue evaluating. :P
 

khmp

Sponsor

Quite possibly the reason my code had no effect :P Thanks Linkin_T! :thumb:

Well begin and end blocks can be used for exception handling or you can put them in there as to be executed when reached code. So rather than having to explicitly say Font.whatevermethod as soon as it reaches that code it executes the block. Does it execute properly if those aren't there?
 
Right but without the so said exception handling methods it´ll do nothing less than executing a bunch of commands and still raise errors (if any), but not catching them. So, in this context, begin/end blocks themselves are useless. This
Code:
class Font
begin
    Font.default_name = 'Arial'
    Font.default_size = 22
  end
end
Is the exactly same thing as
Code:
class Font
  Font.default_name = 'Arial'
  Font.default_size = 22
end
But, you could make this to take care of exception handling
Code:
class Font
  begin
    Font.default_name = 'Arial'
    Font.default_size = 22
  rescue
    # This to catch errors, you could specify the type of the error after 'rescue'.
    # In this specific case, as there´s no specific error to catch, it´ll catch whatever error
    # that´s raised.
    p $!, $!.backtrace  # Displays error information with print
  ensure
    # This is used to ensure that a portion of code is executed after the begin
    # and/or rescue commands are executed. This will be executed in both cases,
    # after a normal execution or after the execution of the rescue block.
    print "Font default definitions done.\n"
  end
end
See? There´s much more to begin blocks than just an end. rescue and ensure are your friends ^^
 

khmp

Sponsor

Awesome I didn't know about ensure or the global/backtrace thing. I need to study up on those. Thanks for pointing out all those things. In the future I won't make the same mistake.

I know I shouldn't but could I burrow your brain on the matter of class Font for a few questions then? If it's a class how come there isn't an instantiation of the class? I guess its a ruby thing but shouldn't there be an object of type Font?
 
NP man ^^

Also, you could check here for a list of all globals defined by Ruby by default. Just scroll the page down, there´s a huge list there. Also, this is the front page of the manual. You should check it, it´s wonderful as a reference basis.

But i guess you still know about it ^^

EDIT: There´s a huge difference between a class and an instantiated object FROM a class. Class inherits from Module, and the two ARE objects too. This makes it possible to call Font.default_name, for example. The main difference here is the scope of the methods. default_name has class scope, so it´ll be possible to call it directly from the class object.
Code:
Font.default_name = "blah"
. To understand it better, think about the variables´scopes (you know, local, instance and class variables...) It´ll help you visualize everything.

For example, suppose we declare a class...
Code:
class Test
At the moment of declaration, we can expect that Ruby will create a new Class object (yes, looks confusing but Class is an object! Note that class != Class, because class is just a reserved word). If your code has no syntax errors, the Ruby interpreter will take all your code inside class Test/end and define a new constant called Test, and attribute to it a new class object with that code. Now, continuing our declaration:
Code:
class Test
  
  # Defining a class method. Will work on instances and on the declaring class
  def self.test_class
    # Note here the use of 'self', and how it recalls to the class object we´re creating.
    # as i´ve said, a class is merely an object. So, 'self' inside the class declaration
    # will work like you´re calling the new class.
    
    # This method is executed WITHIN the class, not within instances of this class.
    # This is why you can make Font.something without worrying with instantiations ^^
    print "Class OK"
  end
  
  # Another method to declare class methods...
  def Test.test_class_2
    # Same as test_class...
    print "Class 2 OK"
  end
  
  # Now to the instance methods... 
  # This method can be called only on instance scope, or by other means, within instances
  # of this class. You can´t call it from the class itself. 
  # Note that instance methods that call other
  # methods inside it will look for methods that are in their own scope. So, they´ll
  # look for instance methods first, and if they don´t find any method with the given
  # name, it´ll search for class methods until it finds nothing and raises a NoMethodError.
  def test_instance
    test_class_2
    print "Instance OK"
  end

end
Noe to the test...
Code:
t = Test.new
# Calling methods from the instance
t.test_instance    # ==> Class 2 OK
                        # ==> Instance OK

# Calling methods from the class
t.class.test_class # ==> Class OK
Test.test_class  # ==> Class OK
As you can see, it´s a basic Ruby feature. Rather confusing, but does the work pretty well ^^

Well, that´s it.. I hope you got it...
 

khmp

Sponsor

I had no idea classes had that kind of functionality. And I just recently learned that class was a child of module too, just didn't connect the dots I guess. This has been pretty enlightening and thanks for putting up with the questions. You did a terrific job clearing that up for me.  ;D
 

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