Equip Script Issue

Hey everyone! Back from 3 months traveling the U.S. and am working my games again.

When I use the following equip script, I have no issue when I equip a weapon while no weapon is equipped. But when a weapon is equipped everything works but the player.equipped = object line. I look at the debugger and the previous weapon is still equipped. I don't see why. Is it not in the right line? It shows equipped in the inventory panels and the msg prints correctly. Also FYI, the ClearTurn is a function to clearscreen and showroomdescription.

So the only issue is the player.equipped attribute is not changing to the new weapon.

The command pattern is equip #object#

ClearTurn
if (HasBoolean(object, "dead")) {
  msg ("That's not something you can wield as a weapon.")
}
else if (not HasAttribute(object, "damage")) {
  msg ("That's not something you can wield.")
}
else if (not object.parent = player) {
  msg ("You are not carrying it.")
}
else if (object = player.equipped) {
  msg ("You already have.")
}
else if (player.equipped = null) {
  msg ("You equip your " + GetDisplayAlias(object) + ".")
  player.equipped = object
  list add (object.inventoryverbs, "Unequip")
  list remove (object.inventoryverbs, "Equip")
  object.listalias = object.listalias + " (equipped)"
}
else {
  msg ("You put away your " + GetDisplayAlias(player.equipped) + " and equip your " + GetDisplayAlias(object) + ".")
  list add (player.equipped.inventoryverbs, "Equip")
  list remove (player.equipped.inventoryverbs, "Unequip")
  player.equipped.listalias = Replace(player.equipped.listalias, " (equipped)", "")
  player.equipped = object
  list add (object.inventoryverbs, "Unequip")
  list remove (object.inventoryverbs, "Equip")
  object.listalias = object.listalias + " (equipped)"
}
foreach (obj, GetDirectChildren(player.parent)) {
  if (HasBoolean(obj, "dead")) {
    if (not obj.dead) {
      if (ListContains (player.attackers, obj)) {
        msg ("{colour:red:<i>You are still in battle!}")
        do_attack (obj, obj, player, false)
      }
    }
  }
}

Visually, your code looks fine to me. I would guess that the player.equipped value is being changed somewhere after the above code.


Another thing you could do is add a print script immediately after the player.equipped = object script that prints out the value of player.equipped, just to verify that it is, in fact, the newly equipped weapon. If the print script prints out the correct weapon, then player.equipped must be being changed afterwards.


OK. I'm looking outside the script for issues. Thanks


I can't find anything outside this script causing the problem. I can equip an object and it will create player.equiped but when I try to equip another weapon it will not change the player.equipped I cannot figure this out.

Once a weapon is equipped it stays as the player.equipped from then on. I really think I'm missing a step here but can't figure what that may be.

Everything else in the script is running correctly but that one line...

I will keep troubleshooting.


So I'm assuming that if you place msg (player.equipped) immediately after player.equipped, it will print out the correct (new) weapon.

Just brainstorming here -

I wonder if creating player.equipped on the fly has anything to do with this problem. What if you created the .equipped attribute in the player's attribute tab beforehand? You would then have to assign it the value of some object in the game.

What if you added player.equipped = null immediately BEFORE player.equipped = object? Maybe the attribute will be "reset" somehow? Actually, that should delete the attribute, then recreate it.


Have tried player.equipped = null earlier with no effect.

just tried msg (player.equipped) and it prints out the change to the new weapon correctly (wow) but it does not change the player.equipped attribute correctly. (odd).

so what does this tell me...?


else {
  msg ("You put away your " + GetDisplayAlias(player.equipped) + " and equip your " + GetDisplayAlias(object) + ".")
  list add (player.equipped.inventoryverbs, "Equip")
  list remove (player.equipped.inventoryverbs, "Unequip")
  player.equipped.listalias = Replace(player.equipped.listalias, " (equipped)", "")
  msg (player.equipped) 
  player.equipped = (object)
  msg (player.equipped)
  player.equipped = null 
  msg (player.equipped) 
  list add (object.inventoryverbs, "Unequip")
  list remove (object.inventoryverbs, "Equip")
  object.listalias = object.listalias + " (equipped)"
}

player.equipped = nulldeletes the player.equipped attribute as it should but gives the error `Object reference not set to an instance of an object'


So if you put player.equipped = null BEFORE player.equipped = object (and get rid of the parentheses around object):

// other code here

player.equipped = null
player.equipped = object

// other code here

Do you still get that error message?


I did some editing when I posted the script and had the two lines out of order. My mistake...Opps
But I assure you it is correct in the game as you pointed out.

Yes, I still get the error message with the null and the player.equipped = object does not show in the Equipped attribute in the debugger. I think the player.equipped = object is not correct here in this case for some reason.


I should copy the script into a blank game and see if it works there. Otherwise, the head-scratching will continue. Let's see.


I'm stumped. Maybe someone else with more insight can suggest something.


I found that after I equip a short sword it works if it's the first weapon I equipped. player.equipped = short sword. When I try to equip another weapon, say Eagle sword all of the script works but the debugger shows that the current command is still Equip short sword, and the player equipped is still short sword as well.


Are you sure that it's not the debugger showing the wrong info?

If you temporarily make a command that prints the currently equipped weapon, or put "equipped" in the player status attributes, does it show the same values as the debugger?


msg (player.equpped) show the change correctly. But debugger is wrong


In that case, it looks like it's a bug in the debugger. Frustrating, but not something to lose any sleep over. Just makes it a little harder to track any other problems in your code.

(for debugging, I normally have a function something like JS.console.log(ProcessText (param)); so in the javascript console I can type ASLEvent('Inspect', '{player.equipped}'); to quickly look at the state of variables without interrupting the game. I never tried using the proper debugger)


That sounds like the best option mrangel. Thanks for helping me out with this. I have spent many days on this one. 😜


In the Quest debugger, maybe if you click on another object and then click back to the player.equipped attribute, it will refresh and show the correct value.


Dcoder, you hit on the problem. Everything is working in my script. I check the debugger after the equip command and it is wrong when it replaces a weapon. However, after I enter another command say, 'stats' the debugger catches up and shows correctly.

#happycamper


Another Scooby mystery solved! 😋


Log in to post a reply.

Support

Forums