Last active
September 14, 2018 08:39
-
-
Save fmonaca/7988479 to your computer and use it in GitHub Desktop.
Creates a copy of a Character object in case more tokens are linked to it, then assigns the newly created Character to the selected token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on("chat:message", function(msg) { | |
if(msg.type == "api" && msg.content == "!spawn") { | |
var selected = msg.selected; | |
_.each(selected, function(obj) { | |
var newID = spawnCharacter(obj._id); | |
var oAttrib = findObjs({name: "STR",_type: "attribute", _characterid: newID}, {caseInsensitive: true})[0]; | |
var attrib = parseInt(oAttrib.get('current')); | |
attrib += 10; | |
oAttrib.set("current", attrib); | |
}); | |
} | |
}); | |
function spawnCharacter(tokenID) | |
{ | |
var oToken = getObj("graphic", tokenID); | |
var tokenName = oToken.get("name"); | |
var sheetID = oToken.get("represents"); | |
var Character = getObj("character", sheetID); | |
var Attributes = findObjs({ _type: 'attribute', _characterid: Character.id }); | |
var Abilities = findObjs({ _type: 'ability', _characterid: Character.id }); | |
var character = createObj('character', { | |
name: tokenName, | |
bio: Character.get("bio"), | |
gmnotes: Character.get("gmnotes"), | |
archived: Character.get("archived"), | |
inplayerjournals: Character.get("inplayerjournals"), | |
controlledby: Character.get("controlledby") | |
}); | |
_.each(Attributes, function(attr) { | |
createObj('attribute', { | |
_characterid: character.id, | |
name: attr.get("name"), | |
current: attr.get("current"), | |
max: attr.get("max") | |
}); | |
}); | |
if(Abilities.length>0) | |
{ | |
_.each(Abilities, function(abil) { | |
createObj('ability', { | |
_characterid: character.id, | |
name: abil.get("name"), | |
description: abil.get("description"), | |
action: abil.get("action") | |
}); | |
}); | |
} | |
// Now we need to assign the token to the newly created character sheet | |
oToken.set("represents", character.id); | |
sheetID = character.id; | |
return sheetID; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment