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.

ading attribute to RPG::Item

i would like to add an attribute to all items called sort_order, for my custom sort method im writing. however no matter what i try i cant seem to get it to work. mostly because im not sure how to set the new attribute for every item. could some one please exsplain to me? ive tried searching but nothing i found was what i was looking for. i got this, not sure if it is right.
[rgss]class RPG::Item
  alias_method :plague180_rpg_item_init,          :initialize
  attr_accessor :sort_order
  def initialize()
    plague180_rpg_item_init
    @sort_order
  end  
end
[/rgss]
 
Your problem is probably the fact that you have to actually edit every single item when you load the game. What I mean is, by editing the class, you edit what happens when it is initialized after making the edit. But in this case, you're too late, and the items have already been saved without that attribute when you saved the project before testing. Therefore, you also have to make a script that, on opening the game in debug mode, will actually load the items file, go through every item in it, initialize a new instance of the item, making sure to pass the data from the original item, and then add the new data to the new instance.
 
You can also have a hash with sort orders, and just add a sort_order(item_id) method to that class that reads from the hash or returns a default value.
That's what I did in Hartacon to add range and other properties to my weapons/armors/skills.
 
just for shits and giggles im gonna try both ways to see what is faster, but im not really sure what i need to do glitches way...

edit:something like this?
[rgss]RPG::Item = Struct.new:)id, :name, :icon_name , :description, :scope, :eek:ccasion , :animation1_id, :animation2_id, :menu_se, :common_event_id, :price, :consumable, :parameter_type, :parameter_points, :recover_hp_rate, :recover_hp, :recover_sp_rate, :recover_sp, :hit, :pdef_f , :mdef_f, :variance, :element_set, :plus_state_set, :minus_state_set , :sort_order)                                                                                                                                                
module RPG
  class Item
    def initialize
      @id = 0
      @name = ""
      @icon_name = ""
      @description = ""
      @scope = 0
      @occasion = 0
      @animation1_id = 0
      @animation2_id = 0
      @menu_se = RPG::AudioFile.new("", 80)
      @common_event_id = 0
      @price = 0
      @consumable = true
      @parameter_type = 0
      @parameter_points = 0
      @recover_hp_rate = 0
      @recover_hp = 0
      @recover_sp_rate = 0
      @recover_sp = 0
      @hit = 100
      @pdef_f = 0
      @mdef_f = 0
      @variance = 0
      @element_set = []
      @plus_state_set = []
      @minus_state_set = []
      @sort_order = 0
    end
    attr_accessor :id
    attr_accessor :name
    attr_accessor :icon_name
    attr_accessor :description
    attr_accessor :scope
    attr_accessor :eek:ccasion
    attr_accessor :animation1_id
    attr_accessor :animation2_id
    attr_accessor :menu_se
    attr_accessor :common_event_id
    attr_accessor :price
    attr_accessor :consumable
    attr_accessor :parameter_type
    attr_accessor :parameter_points
    attr_accessor :recover_hp_rate
    attr_accessor :recover_hp
    attr_accessor :recover_sp_rate
    attr_accessor :recover_sp
    attr_accessor :hit
    attr_accessor :pdef_f
    attr_accessor :mdef_f
    attr_accessor :variance
    attr_accessor :element_set
    attr_accessor :plus_state_set
    attr_accessor :minus_state_set
    attr_accessor :sort_order
  end
end
 
if true
  $data_items = [nil]  
  RPG::Item.new(1,"test","001-Weapon01,","",3,0,0,0,"001-System01",0,0,false,0,0,0,100,0,0,100,0,0,0,[],[],[],1)
  save_data($data_items, "Data/Items.rxdata")
else
  $data_items = [nil]
  $data_items = load_data("Data/Items.rxdata")
end
[/rgss]
 
mmm, what I was talking about was more something like this:

Code:
module RPG

  ITEM_ORDER = {

    1 => 1, 2 => 1, 3 => 5

  }

  class Item

    def sort_order(id)

      return ITEM_ORDER[id] ? ITEM_ORDER[id] : 1

    end

  end

end
 
While I like to solve problems just for the sake of solving them, I don't really see how using the build-in index doesn't work for you... :huh:

And what Glitch is talking about is you opening the .rxdata file with a File.new call, read the contents, add a sort_order=anInteger to them, and finally save the changes. In my opinion (and I'm sure in Glitch's as well), that is overcomplex and totally not worth it, nevermind the hassle of telling every item a certain number (and keep in mind you'd still have to sort them later more complicated than needed really).

So much for unconstructive criticism, here goes the counterpart: If you just want to add an attribute, you MIGHT handle it by editing the .rxdata file with a text editor. Be sure to use an editor like Notepad++ though that supports special characters, as you don't want to mess up your file structure there (and either way, make a backup).
The other way I can imagine is leaving the file alone and mess with $data_whatever upon initialized, so you get the variables directly ingame without the long way over file editing.

Just to mention it: If you'd be using RMVX, my Database Flag Reader would give you a nice alternative that's neither hard nor complicated to use and gives you perfectly what you want without any performance issues. :biggrin:
 
ya i tend to get caught up on doing things wrong ;)

here is what im currently working with
[rgss] 
@order = [2,1,3]
@data = @data.sort {|a,b|@order[a.id-1]<=>a.id }
 
[/rgss]
i would expect it to give me: high potion, potion, full potion based on default. but it gives me :high, full, potion.

ive never really used sort so im kinda at loss..
 
Plague180":146ckh80 said:
ya i tend to get caught up on doing things wrong ;)

here is what im currently working with
[rgss] 
@order = [2,1,3]
@data = @data.sort {|a,b|@order[a.id-1]<=>a.id }
 
[/rgss]
i would expect it to give me: high potion, potion, full potion based on default. but it gives me :high, full, potion.

ive never really used sort so im kinda at loss..
In a quick look, you are calling the same item in the comparation:
{|a,b|@order[a.id-1]<=>a.id}

you need b.id or the other:

{|a,b|@order[a.id-1]<=>b.id}

If you want to sort items by custom atribute just use a tipical constant array or hash with each group of items and the sort value:

Sort_items = [[[1,2,3], 1], [4,5,6], 2]]]

def item_sort_value(id)
for group in Sort_items
return group[1] if group[0].includes?(id)
end
return 0 # default value
end

{|a,b|item_sort_value(a.id)<=>item_sort_value(b.id)}
 
what im trying to do atm is sort by id, then ill mod it to my needs once i understand.

i dont see how im doing it wrong since @order[a.id-1] = 2 and a.id = 1

do you know what i mean? when i print the value of b btw i get some strange number like 234532, but if i dont include ,b i get some other strange number
 
I havent work with sort in a time but using b is indispensable,no? If not how can the ruby know to move the value in the array or not?

See that sort miht use some sort algorithm like:
Check all items comparing. Put to the top if its true.
Iterate doing this with all,etc.
If you tell he to use a and a its comparing the same value... it seems to me to be buggy.

To sort by id:
{|a,b|a.id<=>b.id}

To sort by your custom data:


@order = [2,1,3]
@data = @data.sort {|a,b|@order[a.id-1]<=>@order[b.id-1] }

Also, you dont need to do asignements. Just call a method in a "destructor" way:

@data.sort! {|a,b|@order[a.id-1]<=>@order[b.id-1] }

Are the same.
 

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