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.

Special Support - A great place to learn to Script

Everyone wants to learn to script, but no one really knows where to start. In my honest opinion, the best place to learn is just by reading the default scripts. I learned to script with the japanese RMXP, so I didn't have English comments at my disposal, so everyone of the new generation has that upperhand. Anyways, what I think the best thing to do to learn to script is read the default scripts, and understand each line.

So what I am offering here is support for anyone trying to learn to script. If there is a line of code you don't understand, ask me here. If there is something you want to know, like where in the script editor does something happen, ask here.
  • Ask about a certain line of code : What it means, does, etc.
  • Ask where the coding is for a certain function
PLEASE DO NOT ASK SOMETHING OTHER THAN EXISTING CODE, OR WHERE IN THE DEFAULT SCRIPTS TO FIND A CERTAIN BLOCK OF CODE. Your post will be deleted.

This is a Trial and Error topic. Hopefully, it can lead to more a use full FAQ for beginners.
 

Zeriab

Sponsor

Code:
def foo
  return
end
# Is the same as
def foo
  return nil
end

Every method returns and object when called or nil.
nil can be considered as the empty return or that nothing is return.

The difference between return and return nil is on the conceptual level.
Usually you use return nil when a value is expected from the method and return when a value is not expected.

To give you an idea of every method call returns an object or nil can be illustrated by the p method:
Code:
p p p 3

Try it out ^^

*hugs*
- Zeriab
 
Where do I find the exact definition of the objects created in the database and loaded in Scene_Title?
Code:
# Load database
    $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_troops        = load_data("Data/Troops.rxdata")
    ...

many of them I have already figured out by their use, but is there some documentation about them? $data_actors for example...
thanks
 
The editor itself saves the objects whenever you save your project to the rxdata files. It automatically dumps all the objects you set in the database editor into your rxdata files, as well as all the map data files from the map editor.

If you check the help file, under RPGXP-DataStructure, you can see all these classes instances. Basically, all the Data_Objects are a simple:
Code:
class DataObject
  attr_accessor :attribute
end

To be honest, there isn't even an initialize method (well, there is, but we can't see or, or modify it).

They are basically just simple instance-holding objects. Whatever you set in the database is what is held in these instances for each object.

All else fails, just use:
Code:
p $data_x[1].instance_methods

That will show you all the instance methods, granted, some will be from the higher level classes in there as well.
 

Zeriab

Sponsor

There is an initialize method. If you look at the Data_Objects in the help-file you can see their initialize method. You can also change it.
It doesn't really serve much purpose unless you intend to create new Data_Object since the initialize method is by default not called on any of the data objects.
 
Ahh.. Things are getting bad. Looks like all I try to make ends with an error. :(

Okay, let me try to ask here...
I am trying to fix a incomplete script made I asked OS. But also modifing the way it works. Whatever... Giving details of what I want to do won't help now.


I just want to know what's wrong with these lines:
Code:
def target
    if @jump_image?
    target = Sprite.new
    target.bitmap = Bitmap.new("Graphics/Pictures/target")
    target.x = @x
    target.y = @y
  else
    target = nil
    end
  end
end
The game crashes even before it initialize, pointing an error on the line "target = sprite.new".

Oh, as my knowledge on scripts is really taaaaaad, let I explain what I think I'm doing there. :P
I'm trying to make these lines mean:
If @jump_image is true
create an image(target)
else case
  delete the image(target)


If posting the full script at the moment helps, here it is:
Code:
#==========================#
# Game_Event Edit          #
# By OS                    #
# Version 1.2                               #
#==========================#
# Use:                     #
#                          ######################################
# Call Script: $game_map.events[id].jump_event!                 #
# Conditional Branch: Script: $game_map.events[id].jump_event?  #
# <> (Inside ConBranch) Self Switch/Switch                      #
# Page 2 with Jump Event Image and Jump Code                    #
#===============================================================#
class Game_Event
  def jump_event!
    @jump = true
  end
  
  def jump_image!
    @jump_image
    end
  
  alias os_updt update
  def update
    os_updt
    if jump_event?
      if (@x - $game_player.x <= 2) || ($game_player.x - @x <= 2) || (@y - $game_player.y <= 2) || ($game_player.y - @y <= 2)
        @jump_image = true
      else
        @jump_image = false
      end
    end
    return
  end
  
  def jump_event?
    if @jump != nil
      return @jump
    end
    return
  end

  def target
    if @jump_image?
    target = Sprite.new
    target.bitmap = Bitmap.new("Graphics/Pictures/target")
    target.x = @x
    target.y = @y
    end
  end
end
 

khmp

Sponsor

XDarknessBoyX":28jhcbvu said:
what does $ stand for?
and wat # stand for?

$ is the prefix to a Ruby global variable. Global variables can be accessed from anywhere within the program.
# is the precursor to a Ruby comment. A comment is a piece of code that is effectively skipped by the compiler. It's used to help the one reading the code better understand what the code is trying to accomplish.

Code:
# Return if the player is nil
return if $game_player.nil? # No need to do anymore work.
...

I have a question about RGSS though. If I have a sprite and I initialize it's bitmap. Does calling sprite.dispose also dispose of the bitmap contained within the sprite. Or do I still have to call sprite.bitmap.dispose then sprite.dispose?
 

Zeriab

Sponsor

It is relatively simple to find out khmp. Simply do something like this:
Code:
b = Bitmap.new(23,32)
s = Sprite.new
s.bitmap = b
p s.disposed?, b.disposed?
s.dispose
p s.disposed?, b.disposed?

That code will answer your question and the answer is that sprite.dispose does not dispose its bitmap.
This is useful if you want several sprites to have the same bitmap.
 
I figured this was the best place to ask as it's just a simple question, if it's the wrong place then sorry...

What is the opposite of .push?

For example if I want to remove something from an array, something like:

@array.take-out(chocolate)

as in the opposite of

@array.push(chocolate)
 

Zeriab

Sponsor

Array.shift if you want the array to be treated like a queue. First in is the first out.

Note that those commands are relatively slow for arrays. If you need a stack or a queue you should consider using a Linked-List instead of an Array. Adding and removing values run fast. Looking up values can however be slow.
This is very much a work in progress. The core functionality should be there. The documentation is almost non-existing.
The .push and .pop as well as .shift should work just fine. Here's a small example
Code:
l = Linked_List.new
l.push('dog') #Fast
p l[0] # Generally slow
p l.pop # Fast
p l[0] # Generally slow

Here's the code: (It will most likely appear in the MACL some time in the future)
Code:
#==============================================================================
# ** Classes.Linked_List (v0.9)                                       By Zeriab
#------------------------------------------------------------------------------
# Represents a linked list.
# Has a reference to the tail and the head of the list
#==============================================================================

class Linked_List
 #--------------------------------------------------------------------------
 # * Public Instance Variables
 #--------------------------------------------------------------------------
 attr_reader :size
 
 # A Linked List can be considered as a enumerable collection
 # The head is considered as the first element and the tail the last
 include Enumerable
 
 #--------------------------------------------------------------------------
 # * Name      : Object Initialization
 #   Info      : Initializes the list by clearing it
 #   Author    : Zeriab
 #   Call Info : No Arguments
 #--------------------------------------------------------------------------
 def initialize
   # Clears the list
   clear
 end
 #--------------------------------------------------------------------------
 # * Name      : Concatenation
 #   Info      : Returns a new array list by concatenating the two lists
 #               together to produce a third list.
 #   Author    : Zeriab
 #   Call Info : One Arguments - The list to concatenate with.
 #   Comments  : Slow compared to the dangerous concatenation (.concat!)
 #--------------------------------------------------------------------------
 def +(list)
   return self.dup.concat!(list.dup)
 end
 #--------------------------------------------------------------------------
 # * Name      : Equality
 #   Info      : Tells whether the lists are equal or not
 #   Author    : Zeriab
 #   Call Info : One Arguments - The list to concatenate with.
 #   Comments  : Two lists are equal if their heads and tails are equal on
 #               the Object level
 #--------------------------------------------------------------------------
 def ==(list)
   return self.head == list.head && self.tail == list.tail
 end
 #--------------------------------------------------------------------------
 # * Name      : Set nth element
 #   Info      : Sets the value of the nth element in the list
 #   Author    : Zeriab
 #   Call Info : Two Arguments - Numeric n for specifying which element
 #               Object value to replace the value of the nth element
 #   Comments  : Returns true if the change succeeds otherwise false
 #--------------------------------------------------------------------------
 def []=(n,value)
   element = get_element(n)
   # If nil is returned from get_element the element was not found
   if element.nil?
     return false
   else
     # Changes the value of the element
     element.value = value
     return true
   end
 end
 #--------------------------------------------------------------------------
 # * Name      : Add Value
 #   Info      : Adds a value to the list as the new head
 #   Author    : Zeriab
 #   Call Info : One Argument - Object value to be added
 #   Comments  : Returns the modified list.
 #               Values in the list does not have to be distinct
 #--------------------------------------------------------------------------
 def add(value)
   element = Linked_List_Element.new
   element.value = value
   insert_element(element)
   self.size += 1
   return self
 end
 # Synonyms
 def enqueue(value) add(value) end
 def insert(value) add(value) end
 def push(value) add(value) end
 def <<(value) add(value) end
   
 ##
 # Clears the list
 #
 def clear
   self.size = 0
   self.tail = nil
   self.head = nil
 end
 #--------------------------------------------------------------------------
 # * Name      : Concatenation!
 #   Info      : Adds the given list to the tail
 #   Author    : Zeriab
 #   Call Info : One Arguments - The list to concatenate with.
 #   Comments  : Alters the given list as well.
 #--------------------------------------------------------------------------
 def concat!(list)
   if list.size <= 0
#      list.tail = self.tail
#      list.head = self.head
#      list.size = self.size
     return self
   elsif self.size <= 0
     self.tail = list.tail
     self.head = list.head
     self.size = list.size
     return self
   end
   self.tail.next_element = list.head
   list.head.previous_element = self.tail
   self.size += list.size
   self.tail = list.tail
#    list.head = self.head
   return self
 end

 ##
 # Dups the list by creating a new instance of the same class and adding
 # the values in the same order as they are in this list.
 #
 def dup
   list = self.class.new
   self.each {|value| list.unshift(value)}
   return list
 end
 
 ##
 # Cloning a list includes cloning the elements. (Not the values)
 #
 def clone
   list = super
   list.clear
   self.each {|value| list.unshift(value)}
   return list
 end
 
 ##
 # Delete an object from the list
 # Return the deleted object
 # Return nil if the deletion has no effect on the list.
 #
 def delete(value)
   element = self.search(value)
   if element.nil?
     return nil
   else
     self.delete_element(element)
     self.size -= 1
     return element.value
   end
 end
 
 ##
 # Implements the each method as required by the Enumerable module
 #
 def each
   # Return if there are no elements in the list
   return if self.head.nil?
   element = self.head
   yield element.value
   while element != self.tail && element.next_element != nil
     element = element.next_element
     yield element.value
   end
 end
 
 ##
 # A list is empty if it has size 0
 #
 def empty?
   return self.size <= 0
 end
 
 ##
 # Returns the first value of the list (head)
 # If the list is empty -> returns nil
 #
 def first
   return nil if self.empty?
   return self.head.value
 end
 # Synonyms
 def peek() return first end
 
 ##
 # Get the object at the nth element in the list
 #
 def get(n)
   # Return nil if the list is empty
   element = get_element(n)
   if element.nil?
     return nil
   end
   return element.value
 end
 # Synonyms
 def [](n) get(n) end
 
 ##
 # Returns a string representation of the list
 #
 def inspect
   return self.join(" => ")
 end
 
 ##
 # Returns a string created by converting each value in the list to a
 # string, separated by aSepString.
 #
 def join(separator = "")
   self.to_a.join(separator)
 end

 ##
 # Returns the last value of the list. (tail)
 # If the list is empty -> returns nil
 #
 def last
   return nil if self.empty?
   return self.tail.value
 end
 
 # Synonym for size
 def length() return size end
   
 ##
 # Invokes block once for the value each element list, replacing the element's
 # value with the value returned by block.
 #
 def map!(&block)
   # Return if there are no elements in the list
   return if self.head.nil?
   element = self.head
   element.value = block.call element.value
   while element != self.tail && element.next_element != nil
     element = element.next_element
     element.value = block.call element.value
   end
   return self
 end
 # Synonym
 def collect!(&block) map!(&block) end
 
 ##
 # Finds the number of non-nil values in the list
 #
 def nitems
   number = 0
   # Return if there are no elements in the list
   return number if self.head.nil?
   element = self.head
   number += 1  if !element.value.nil?
   while element != self.tail && element.next_element != nil
     element = element.next_element
     number += 1  if !element.value.nil?
   end
   return number
 end
   
 ##
 # Delete and return the head of the list.
 #
 def pop
   # Return nil if the list is empty
   if self.head.nil?
     return nil
   end
   self.size -= 1
   return delete_element(self.head).value
 end
 # Synonyms
 def dequeue() return pop end
 
 ##
 # Replaces the list to have the contents of the given list
 #
 def replace(list)
   self.head = list.head
   self.tail = list.tail
   self.size = list.size
   return list
 end
 
 ##
 # Delete and return the tail of the list.
 #
 def shift
   # Return nil if the list is empty
   if self.head.nil?
     return nil
   end
   self.size -= 1
   return delete_element(self.tail).value
 end
 
 ##
 # Returns list.join
 #
 def to_s
   return join
 end
 
 ##
 # Add an object to the back of the list (TAIL)
 #
 def unshift(value)
   element = Linked_List_Element.new
   element.value = value
   insert_element_tail(element)
   self.size += 1
 end
 
 protected
 attr_accessor :head
 attr_accessor :tail
 
 ##
 # Sets the size of the list
 #
 def size=(value)
   @size = value if value >= 0
 end
 
 private
 
 ##
 # Insert an element into the list.
 # Assumes 'element' is a Linked_List_Element.
 #
 def insert_element(element)
   if head.nil?
     self.head = element
     self.tail = element
     return
   end
   element.next_element = self.head
   self.head.previous_element = element
   self.head = element
   element.previous_element = nil
 end
 
 ##
 # Insert an element into the list at the tail.
 # Assumes 'element' is a Linked_List_Element.
 #
 def insert_element_tail(element)
   if head.nil?
     self.head = element
     self.tail = element
     return
   end
   
   element.previous_element = self.tail
   self.tail.next_element = element
   self.tail = element
   element.next_element = nil
 end
 
 ##
 # Delete the given element from the list
 # Assumes 'element' is a Linked_List_Element.
 #
 def delete_element(element)
   if element.next_element.nil?
     self.tail = element.previous_element
   else
     element.next_element.previous_element = element.previous_element
   end
   
   if element.previous_element.nil?
     self.head = element.next_element
   else
     element.previous_element.next_element = element.next_element
   end
   
   return element
 end
 
 ##
 # Search for an element with the specified value.
 # Return the first element found with the corresponding value
 # Return nil if no element is found.
 #
 def search(value)
   # If the head is nil the list is empty
   if self.head.nil?
     return nil
   end
   # Start with the head
   element = self.head
   loop do
     # Check if the element has the correct value
     if element.value == value
       return element
     end
     # Return nil if the tail has been reached
     if element == self.tail
       return nil
     end
     # Look at the next element in the list
     element = element.next_element
   end
 end
 
 ##
 # Get the object at the nth element in the list
 #
 def get_element(n)
   # Return nil if the list is empty (or n is negative)
   if self.head.nil? || n < 0
     return nil
   end
   
   element = self.head
   for i in 0...n
     if self.tail == element
       return nil
     end
     element = element.next_element
   end
   
   return element
 end
 
 ##
 # Represents an element in the linked list.
 #
 class Linked_List_Element
   attr_accessor :value
   attr_accessor :previous_element
   attr_accessor :next_element
 end
end

*hugs*
- Zeriab
 
Hi. What's the best way to call a common event from a script?

Here's what i do in one of my scripts:

Code:
class Interpreter
  attr_accessor :depth
  attr_accessor :child_interpreter
end

And then when i want to call the common event #no

Code:
$game_system.map_interpreter.child_interpreter = Interpreter.new($game_system.map_interpreter.depth + 1)
$game_system.map_interpreter.child_interpreter.setup($data_common_events[no].list,no)

This works, but I'm not sure that it's the best way to do it...
 
That works, but you can just do this instead:
Code:
# If On Map : Replace id with common event id
Script : $game_temp.common_event_id = id
# If in Battle : Replace id with common event id
Script : common_event = $data_common_events[common_event_id]
$game_system.battle_interpreter.setup(common_event.list, 0)
 
There are plenty of ways to do if than statements. ^_^

Lets say if a is greater than b we add 5 to z, otherwise we subtract 3 from z.
Here's all the ways you can do that:
Code:
if a > b
  z += 5
else
  z -= 3
end

# or

if a > b then z += 5 else z -= 3 end

# or

if a > b ; z += 5 ; else ; z -= 3 ; end

# or

a > b ? z += 5 : z -= 3

# or most advanced

z += a > b ? 5 : -3

# of course, there are more

It's all logic. Let's take a look at all our logical keywords for Ruby (or at least the most common)
Code:
if <bool>
  <code>
elsif <bool2>
  <code2>
else
  <code3>
end

# if bool is true, code is executed
# if bool is false, and bool2 is true, code2 is executed
# if bool and bool2 are both false, code3 is execute
# you may have as many elsif statements as you want, but only one else statement

unless <bool>
  <code>
else
  <code2>
end

# if bool is not true, code is executed
# otherwise, code2 is executed

while <bool>
  <code>
end

# as long as bool is true, code is executed

until <bool>
  <code>
end

# as long as bool is false, code is executed

There are some more, but I won't get into those unless you ask. ;-)


You can use them in in Script command boxes, just as long as you can get it all in the text box. If you can't get it all in the box, just use this:

Code:
class Interpreter
  def yourmethodnamehere
    <put your code here>
  end
end

Insert that in the script editor below the Interpreter class, then in the call script, just have yourmethodnamehere as the only code.

Let me know if you need any other help.
 

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