PDA

View Full Version : GameObj Array manipulation



ryaden
10-04-2017, 09:17 PM
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

Tgo01
10-04-2017, 09:37 PM
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:



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.

ryaden
10-04-2017, 10:12 PM
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/)}

Tgo01
10-04-2017, 10:30 PM
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.

Imperarx
10-04-2017, 10:34 PM
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)?