Results 1 to 5 of 5

Thread: GameObj Array manipulation

  1. #1

    Default GameObj Array manipulation

    I have a few questions regarding manipulating arrays

    1. I am trying to delete any object other than one I specify. I have used this but it doesn't work

    pickup = GameObj.loot
    pickup = pickup.delete_if { |obj| (obj.id.length != /7/)}


    2. Is there a straight forward way to delate one GameObj array from another. If for example i have a GameObj array that contains 2 things and another array with 3 and there are 2 common elements.

    I have tried this.

    Monsters1 = GameObj.npcs
    #an npcs enters the room
    Monsters2 = GameObj.npcs
    Monsters3 = Monsters2 - Monsters1

    Thanks for any help you can offer

  2. #2

    Default

    Quote Originally Posted by ryaden View Post
    I have a few questions regarding manipulating arrays

    1. I am trying to delete any object other than one I specify. I have used this but it doesn't work

    pickup = GameObj.loot
    pickup = pickup.delete_if { |obj| (obj.id.length != /7/)}
    GameObj.loot has more than just ids of stuff on the ground, it has items' names, nouns, etc etc. Do ;eq echo "#{GameObj.loot}" in game to see what I mean.

    If you want the pickup array to just track the ids of items on the ground you could do something like:

    Code:
    pickup = Array.new
    GameObj.loot.each{|i|
    	pickup.push(i.id)
    }
    This would have all ids of stuff in the loot array. Now you can go through each id and determine if the length of said id is 7 characters long.

  3. #3

    Default

    Thanks for the thought. I am aware there is more information in GameObj array. I would like to keep those details left but save only the ones that meet the length requirement for ID. I use a similar method for other things. I would prefer not to create an array just containing IDs.

    pickup = GameObj
    pickup = pickup.delete_if { |obj| (obj.name =~ /warhammer/)}

  4. #4

    Default

    My bad, I misread your post when I replied.

    pickup = pickup.delete_if { |obj| (obj.id.length != /7/)}

    You would not need to use // then as that is a regex comparison.

  5. #5

    Default

    So, a few things. From the code you posted, you're trying to see if the item meets the regex "7", which probably isn't what you're after. If you're looking to destructively mutate the array, you could easily use either select! or reject! (depending on how you want to use your comparison) to do it in place without having to use the assignment, or just do it off of your original assignment using the non-banged versions. delete_if should also work, but I can't remember the last time I saw it in the wild in ruby code, so I'd go with

    pickup = GameObj.loot.select { |obj| obj.id.length == 7}
    You could also use .map to return something other than the object itself, for instance if you just wanted to get a list of the id's (you'll need to .map and then .compact to remove the nil elements)

    As for your second question and statement, that type of array math should work fine in ruby as long as the objects in the array aren't too complex. What are you getting back when you perform that operation (and what are the values you had in npc1/npc2)?

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2015, 07:47 AM
  2. Array issue
    By Saurven in forum The Lich Project
    Replies: 6
    Last Post: 08-27-2014, 09:22 PM
  3. gameobj-data.xml
    By masterdtwin in forum The Lich Project
    Replies: 4
    Last Post: 09-19-2013, 06:44 PM
  4. GameObj update
    By Tillmen in forum The Lich Project
    Replies: 8
    Last Post: 09-18-2010, 04:36 AM
  5. Array test
    By Vindicate in forum The Lich Project
    Replies: 1
    Last Post: 04-27-2010, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •