Main Menu
  • Welcome to The RPG Maker Resource Kit.

[VX] Simple Skill Alphabetical Sort

Started by exhydra, June 12, 2011, 11:56:24 PM

0 Members and 1 Guest are viewing this topic.

exhydra

The code I have actually works, but I was wondering if there was a way to dump the sorted information back into the @actor.skill so I could just alias refresh instead of overwriting the whole thing? @actor doesn't seem to take kindly to any array, hash or string methods (.clear, etc) that I've tried so I'm not sure how to re-insert the sorted skill information.

class Window_Skill < Window_Selectable
  def refresh
    @data = []
    @data_sorted = @actor.skills.sort { |a,b| a.name.downcase <=> b.name.downcase }
   
    for skill in @data_sorted
      @data.push(skill)
      if skill.id == @actor.last_skill_id
        self.index = @data.size - 1
      end
    end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max
      draw_item(i)
    end
  end
end

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

modern algebra

#1
Well, you could alias the skills method in Game_Actor and do the sort there before returning it.

Of course, you could also do the sorting inside the learn_skill method of Game_Actor. That is how they do it in the default scripts and it's a little smarter since it means you aren't running a rather inefficient sort method every time you call the skills method. There is a possibility someone would have written a script which bypasses the learn_skill method for whatever reason and if so, that would effectually render your script unfunctional, but I think that would be a small risk.

Both ways, of course, would affect it everywhere skills is used though, so it may cause some incompatibilities with other scripts if they do weird things expecting skills to be sorted by ID.

exhydra

Yeah, I tried that as well and I found out that it was a Bad Thing©. If I assign skill 1 through 10 to one actor and 11 through 20 to another actor then sort it using the skills method in Game_Actor, the first actor might have skills 1,5-7,17,20 depending on the name of the skill. So I'd have to go and update the new positions within the skills array for each actor. Meh.

Ah well ... the code works the way it is, I'm just trying to find ways to be more efficient.

EDIT: Hmm, I'll fiddle with the learn_skill and see what I can come up with.

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

modern algebra

#3
I don't understand what you mean by that. If the first actor only gets skills 1 through 10, then alphabetically sorting them isn't going to give him skills 17 & 20. I also don't get why this would necessitate updating the positions with other actors. Already, the same skills will be in different positions in each actor's arrays depending on what skills they have. So, for instance, if Actor 1 has skills 5 & 7 and Actor 2 has skills 2, 3, & 5, then skill 5 will be in the 0 position for Actor 1 and in the 2 position for Actor 2. That doesn't cause any problems.

Also, it's not really any more efficient to alias and not redefine. It's actually (slightly) less efficient. The reason you would want to alias is more for compatibility reasons, as otherwise redefining the draw_item method could easily conflict with other custom skill scene scripts.

exhydra

Alright, I see what I did wrong. I was destructively sorting $data_skills which was re-sorting all of the skills and generally mucking things up. Sorting the result within the skills method in Game_Actor works just fine. I think I did it the first way because I could get away with aliasing then, since it was at the top ... of course then everything else was wrecked, but hey!

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

modern algebra

An alias would certainly be available by doing it at the bottom too. All you would need to do is:


  alias exhyd_alphsort_skills_5th2 skills
  def skills (*args)
    s =  exhyd_alphsort_skills_5th2 (*args)
    return (s.sort { |a,b| a.name.downcase <=> b.name.downcase })
  end
end

exhydra

Ooh, I see now. That's pretty neat ... I had it in my head that changes needed to be at the top before the original method was run. That opens up some more avenues to take on other scripts. Thanks for pointing that out!

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5