printOrRun or die trying....

You are going to hate me by the time I'm done getting this game together...

Okay.. had to struggle a bit with "printOrRun()" ( https://github.com/ThePix/QuestJS/wiki/Other-Functions ) - but I did get it after doing a search through the site, and various experiments.

printOrRun(char, item, attname, options)

Basically the issue for me was that the third variable 'attname' has to be a string if I am referring to an attribute within the 'item' .
I got an error when I made it a reference to the actual attribute.

this bombs:
printOrRun(player, this, this.eat)

this works:
printOrRun(player, this, 'eat')

If however, I am calling a function defined in code.js for example

function testFunc() {
	msg("TEST FUNCTION")
}

then I can run it like this:

printOrRun(player, this, testFunc())

Even though the function is run and the message is printed, I get this error:

Error: Unsupported type for printOrRun (undefined is a undefined).
    at printOrRun (_util.js:77:11)
    at Object.bite (data.js:285:3)

If I can get some clarification on calling functions this way, that would be great.

So, for me, I had two attributes in my Item "apple" that were doing the same thing (this works but I wanted to have "bite" run "eat" and remove the duplicate code):

createItem("apple", SURFACE(), EDIBLE(false),{
	loc:"refrigerator",
	getTakeMsg:function() { // this is not  working
		msg("doing stuff here...",{},'rudy-words')
		return ""
	},
	eat:function() {
		msg("do something in this function")
	},
	bite:function() {
		msg("do same thing as 'eat' in this function")
	},
	examine: "Print a few words"
})

This is what I finally was able to do:


	eat:function() {
		msg("do something in this function")
	},
	bite:function() {
		printOrRun(player,this,'eat')
	},

So... After saying all that, in the end..

I just put this in code.js

commands.unshift(new Cmd('eat', {
  regex:/^(?:eat|bite|nibble|chew) (.+)$/,
  objects:[
    {scope:parser.isHeld}
  ],
  defmsg:"No, I don't want to bite {nm:item:the}... Seriously?",
 }))

and yanked 'bite' out of 'apple' all together...


You are going to hate me by the time I'm done getting this game together...

QuestJS gets incrementally better each time...

The printOrRun was one of the first bits in QuestJS, and was written for a very specific use. Unfortunatelky the docs did not reflect that, plus the function got updated, so the docs were even more wrong. I have now updated them:


This function is designed to be used within a command. It is common for a command to relate to an item, and to access an attribute of that item, which might be a string or a function. For examine, the TAKE command accesses the "take" attribute of the item.

With this in mind, then, the function has these parameters:

  • char: the character doing the command; usually the player, but the player might ask an NPC to take the hat, for example
  • item: the item, obviously
  • attName: the name of the attribute
  • options: an optional dictionary of further options (the function will set "char" to the character and "item" to the item unless these are already set)

If the attribute is a string, the string is printed, using options as text processor parameters. If the attribute is a function, the function is called, using options as the only parameter.


I have also slightly modified the function so it handles defaults better, but that will not affect you; unless you are using options the old version will be fine.

In fact, you do not need to use it at all, because you know your "eat" attribute is a function. Just do this:

  bite:function() {
    this.eat()
  },

Thanks for the update and clarity.


Log in to post a reply.

Support

Forums