Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array types do not update? #183

Closed
plobre opened this issue Jun 4, 2018 · 2 comments · Fixed by #185
Closed

Array types do not update? #183

plobre opened this issue Jun 4, 2018 · 2 comments · Fixed by #185

Comments

@plobre
Copy link

plobre commented Jun 4, 2018

Giving this little engine a go for a little hobby project. Attempting to create a "harvestable" item class to be used by the players. I have created a property to the following:

"properties": {
    "description": {
      "value": "An ore vein"
    },
    "harvestItems": {
      "array": [
        {
          "object": {
            "item": {
              "ref": "lib.item"
            },
            "amount": {
              "value": 1
            },
            "max": {
              "value": 1
            },
            "matchers": {
              "value": "item"
            }
          }
        }
      ]
    }
  }

to which is used by the following sript:

function harvest({ player, dobj, iobj, verbstr, argstr, dobjstr, prepstr, iobjstr }) {
	if (dobjstr) {
		player.tell(`Parsed ${dobjstr} from string.`);
		for (let item of this.harvestItems) {
			if (item.matchers.indexOf(dobjstr) > -1) {
				if (item.amount > 0) {
					item.amount = item.amount -1;
					player.tell(`You take some ${dobjstr} from the ${this.name}`)
					player.tell(`There is now ${item.amount} left`)
					// not including all insertion / cloning logic here for clarity
					return;
				}
				break;
			}
		}
		player.tell(`There is no harvestable ${dobjstr} here`) 
	} else {
		player.tell(`Harvest what from ${this.name}?`);
	}
}

but every time I make an update to the "amount" value, the changes are discarded as soon as the script finishes - as if the state is not being saved. I see that the value gets outputted as 0 remaining, but running the script again results in the same display of 0, running through the same branches again. Am I doing something wrong, or is this type of functionality not supported out of the box?

@Omikhleia
Copy link
Collaborator

Hi plobre

I think the value change is not saved because it's in a nested object and the main object saving logic doesn't do a deep check. The workaround is to re-assign the toplevel object property which is being changed, as shown below (marked "HERE"). In other terms, work with an object reference in your loop, and reassign the this.harvestItems property.

function harvest({ player, dobj, iobj, verbstr, argstr, dobjstr, prepstr, iobjstr }) {
	if (dobjstr) {
		player.tell(`Parsed ${dobjstr} from string.`);
                let harvests = this.haverstItems // HERE...
		for (let item of harvests) {
			if (item.matchers.indexOf(dobjstr) > -1) {
				if (item.amount > 0) {
					item.amount = item.amount -1;
					player.tell(`You take some ${dobjstr} from the ${this.name}`)
					player.tell(`There is now ${item.amount} left`)
                                        this.harvestItems = harvests // AND HERE...
					// not including all insertion / cloning logic here for clarity
					return;
				}
				break;
			}
		}
		player.tell(`There is no harvestable ${dobjstr} here`) 
	} else {
		player.tell(`Harvest what from ${this.name}?`);
	}
}

I just did that with a code very similar to yours, and it worked for me. For the record, in the demo, lib.door.addExit has the same issue and uses that workaround (i.e. reassigning this.sides).

Good luck with your hacking. I hope you'll enjoy the engine. Let me know if you could make it work. Might keep the issue open as a reminder, we should probably explain this restriction on nested objects in the documentation, and I actually have several pending patches to the documentation I should have pushed long ago...

@plobre
Copy link
Author

plobre commented Jun 12, 2018

Meant to comment on this yesterday but had some stuff come up. Thanks for the work around. It was either what you suggested, or just not support the nested objects and have them as direct properties. Glad I can have a harvestable trait support multiple things to "harvest".

Not sure if you still wish to keep the issue open, or close it out, but either way the problem has been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants