Been trying to get this to work for AGES. I have made a combat system that pulls different enemies from a passage. (sets different combat variables etc). I have used the below to change the display name based on a variable (this works), I also want to be able to change the image based on a variable as well.
I define the character like so (Have tried loads of different ways to call the image.
You’re quoting the variables you’re using when you should not be. In general, unless the macro you’re using tells you to do so, you never quote variables.
You’re attempting to use the [img[]] markup as an argument to <<character>> and I’m not sure that works.
The <<character>> macro allows you to predefine persistent, usually significant, characters. You seem to be attempting to use it to create short-lived/temporary characters, which may be better handled via the <<say>> macro.
Regardless. You’d predefine a character with <<character>> like so:
Thank you for the response. I have tried it and it is still not showing up :(. It might need altering in the javascript to get it to work, but this is way outside my level of understanding.
(function () {
// v1.1.1
‘use strict’;
var characters = new Map();
function addCharacter (name, displayname, icon) {
if(icon === undefined && displayname){
icon = displayname;
displayname = null;
}
if (State.length) {
throw new Error('addCharacter() -> must be called before story starts');
}
if (!name || !icon) {
console.error('addCharacter() -> invalid arguments');
return;
}
if (characters.has(name)) {
console.error('addCharacter() -> overwriting character "' + name + '"');
}
characters.set(name, {displayName: displayname, image: icon});
}
function say ($output, character, text, imgSrc) {
//
var $box = $(document.createElement('div'))
.addClass(Util.slugify(character) + ' say');
// portrait
var _img = characters.has(character) ? characters.get(character).image : null;
var $img = $(document.createElement('img'))
.attr('src', imgSrc || _img || '');
if ($img.attr('src') && $img.attr('src').trim()) {
$box.append($img);
}
// name and content boxes
var _name = character.toUpperFirst();
if (characters.has(character) && characters.get(character).displayName) {
_name = characters.get(character).displayName;
}
$box.append($(document.createElement('p'))
.wiki(_name))
.append($(document.createElement('p'))
.wiki(text));
if ($output) {
if (!($output instanceof $)) {
$output = $($output);
}
$box.appendTo($output);
}
return $box;
}
setup.say = say;
setup.addCharacter = addCharacter;
Macro.add('character', {
// character macro
handler : function () {
addCharacter(this.args[0], this.args[1], this.args[2]);
}
});
$(document).one(':passagestart', function () {
// construct array of character names
var names = Array.from(characters.keys());
names.push('say');
// generate macros
Macro.add(names, {
tags : null,
handler : function () {
if (this.name !== 'say') {
say(this.output, this.name, this.payload[0].contents);
} else {
say(this.output, this.args[0], this.payload[0].contents, this.args[1]);
}
}
});
});
However if the String value stored in the $enemypic variable doesn’t start with a forward slash / character, then you would need to modify TME’s expression to include that slash after the word enemies…