there has to be a better way to do this

Started by Ryex, July 17, 2009, 12:02:46 pm

Previous topic - Next topic

Ryex

there has to be a better way to do this

  alias artifact_setup_later setup
  def setup(actor_id)
    @artifact1_id = @artifact2_id = @artifact3_id = @artifact4_id =
      @artifact5_id = @artifact6_id = 0
    @rechargeing_artifacts = {}
    @recharge_artifacts = []
    artifact_setup_later(actor_id)
  end
 
  def add_recharging_artifact(id, turns)
    @rechargeing_artifacts[id] = turns
    @recharge_artifacts.push(id)
  end
 
  def artifact_can_use?(id)
    return artifacts.include?(id) ? @recharge_artifacts.include?(id) ? false : true : false
  end
 
  def update_recharging_artifact
    recharged = []
    @rechargeing_artifacts.each_key { |id|
      @rechargeing_artifacts[id] -= 1
      if @rechargeing_artifacts[id] <= 0
        recharged.push(id)
      end
    }
    if recharged != []
      recharged.each { |id2|
        @rechargeing_artifacts.delete(id2)
        @recharge_artifacts.delete(id2)
      }
    end
  end


those methods are added to the game actor class at a part of my artifact system and as you can see I'm using both a hash and an array for determining if an artifact is recharging and there has to be a better way. now, to see if an artifact is recharging, all I do is test to see if its armor id is included in @recharge_artifacts but I also need to connect the Id number and the number of battle turns left till it recharges. and I don't think using a 2-D array will work because I need to test for id inclusion in the array, any idea how how to improve the system? its running on a crappy n00b workaround right now.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

July 18, 2009, 07:23:51 am #1 Last Edit: July 18, 2009, 07:29:53 am by Blizzard
Yes, you don't need the array. You can easily get an array of all IDs used by using hash.keys which would then return all IDs that are currently active. When removing them from the hash, you need to use hash.delete(ID) to make sure you remove it completely (so it doesn't happen that you use hash[ID] and get a nil value because there's a nil value actually stored corresponding to that key).

From a first look at your code you only need to change "@recharge_artifacts.include?(id)" to "@rechargeing_artifacts.has_key?(id)" and remove all other lines that use @recharge_artifacts.

BTW, it's "recharging", not "rechargeing". xD

EDIT:

return artifacts.include?(id) ? @recharge_artifacts.include?(id) ? false : true : false


This is kind of redunant as the results of those methods already do return boolean values. It's also confusing a bit because of a double condition. This is better:

return (artifacts.include?(id) && !@recharge_artifacts.include?(id))


I also suggest the use of parenthesis ( () brackets) to distinguish better what part of the line is used in which condition. In your original line it would look like this:

return (artifacts.include?(id) ? (@recharge_artifacts.include?(id) ? false : true) : false)
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

July 18, 2009, 02:13:57 pm #2 Last Edit: July 18, 2009, 02:16:14 pm by Ryexander
wait @recharging_artifacts.keys would return an array of all the armor ids that have values mapped to them?
so @recharging_artifacts.has_key?(id) could replace my @recharge_artifacts.include?(id) ? cool thankyou Blizz
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />