Skip to content

Foundery

Foundery

https://github.com/VanceCole/macros

Actors

actor = token.actor
actor = canvas.tokens.controlled[0].actor
actor.data.name

//get all actors
let actors = canvas.tokens.controlled.map(token => {return token.actor});

Health

@health.value
@health.max

Items

heathpotion = actor.items.find(item => item.data.name == "Heath Potion")

heathpotion.data.data.quantity
heathpotion.delete()
heathpotion.data.data.attributes

Rolls

//Add varable 
let roll = new Roll("2d20kl + @fight", {fight:3}).roll();

//Send as message to Chat 
//set the name of the person the name of the 
roll.toMessage({
  speaker: {
	actor.data.name
  }
});
let roll = new Roll(`1d20 + ${weapon.data.data.attributes.attack.value} + ${modifier}`).roll()

Chat

ChatMessage.create({ 
  content: "Blah blah blah", 
  speaker: { 
	alias: "Steve" } 
});

UI

Errors

ui.notifications.error("Please select a single token")

Dialog Box

new Dialog({
  title: "My Cool Popup",
  content: "",
  buttons:{
	rollAttack: {
	  label: "Roll Attack",
	  callback: (html) =>{
		console.log(html.find("#weapon")[0])
	  }
	}
	close:{
	  label: "Close"
	}
  }
}).render(true)

Tokens

//Get a list of the tokens that are controlled by the user
canvas.tokens.controlled

//Get a list of the tokens that are Targeted.
let targets = Array.from(game.user.targets);

Scripts

Attack Roll Selected Actors:

async function main(){
  // Fetch All Selected Targets
  let actors = canvas.tokens.controlled.map(token => {return token.actor});
  for(let actor of actors){
	let _atk = actor.data.data.attributes.fight.value
	console.log("Atk: ", _atk)
	if(!_atk){_atk = 0}

	console.log("Wep: ", _wep)
	let roll = new Roll("2d20kl + @actorAtk", {actorAtk: _atk}).roll()
	roll.toMessage()
  }
}

Attack with Equipped weapon:

main()

async function main(){
  // Get Selected
  let selected = canvas.tokens.controlled;
  if(selected.length > 1){
	ui.notifications.error("Please select only one token")
	return;
  }
  let selected_actor = selected[0].actor;
  // Get Target
  let targets = Array.from(game.user.targets)
  if(targets.length == 0 || targets.length > 1 ){
	ui.notifications.error("Please target one token");
	return;
  }
  let target_actor = targets[0].actor;

  // Select Weapon
  // Why Filter instead of Find?
  let actorWeapons = selected_actor.items.filter(item => item.data.data.attributes.type.value == "weapon")
  let weaponOptions = ""
  for(let item of actorWeapons){
	weaponOptions += `<option value=${item.id}>${item.data.name} | ATK: ${item.data.data.attributes.attack.value}</option>`
  }

  let dialogTemplate = `
  <h1> Pick a weapon </h1>
  <div style="display:flex">
	<div  style="flex:1"><select id="weapon">${weaponOptions}</select></div>
	<span style="flex:1">Mod <input  id="mod" type="number" style="width:50px;float:right" value=0 /></span>
	<span style="flex:1"><input id="ignoreArmor" type="checkbox" checked /></span>
	</div>
  `
  new Dialog({
	title: "Roll Attack", 
	content: dialogTemplate,
	buttons: {
	  rollAtk: {
		label: "Roll Attack", 
		callback: (html) => {
		  let wepID = html.find("#weapon")[0].value;
		  let wep = selected_actor.items.find(item => item.id == wepID)
		  let modifier = html.find("#mod")[0].value;
		  let ignoreArmor = html.find("#ignoreArmor")[0].checked;
		  // Roll Attack
		  let newRollString = `1d20 + ${wep.data.data.attributes.attack.value} +${modifier}`
		  let roll = new Roll(newRollString).roll();
		  // See if Attack is Greater than their armor, if so
		  let result = roll.total
		  console.log(result)
		  // Print Chat with Button to Roll Damage
		  let chatTemplate = ""
		  let armor = target_actor.data.data.attributes.armor?.value && !ignoreArmor ? target_actor.data.data.attributes.armor?.value : 0;
		  if(result > armor){
			chatTemplate = `
			<p> Rolled: ${result} against ${armor} Target Armor </p>
			<p> It was a Hit! </p>
			<p> <button id="rollDamage">Roll Damage</button></p>
			`
		  } else {
			chatTemplate = `
			<p> Rolled: ${result} against ${armor} Target Armor </p>
			<p> It was a Miss! </p>
			`          }
		  ChatMessage.create({
			speaker: {
			  alias: selected_actor.name
			},
			content: chatTemplate,
			roll: roll
		  })

		  // Roll Damage
		  Hooks.once('renderChatMessage', (chatItem, html) => {
			html.find("#rollDamage").click(() => {
			  //console.log("Damage Button Clicked")
			  let wepDmg = wep.data.data.attributes.damage?.value ? wep.data.data.attributes.damage.value : ""
			  new Roll(wepDmg).roll().toMessage();
			})
		  })         
		}
	  }, 
	  close: {
		label: "Close"
	  }
	}
  }).render(true)
}

Import Export

From Foundery to Foundery

  1. go to the world your desired actor exists in.
    2) drag it into a scene and select it.
    3) open the console and type: _token.actor.exportToJSON()
    4) save the file in a convenient place.
    5) open the world you want you the actor to be in.
    6) create a temp actor and drag it into a scene and select it.
    7) open the console again and type: _token.actor.importFromJSONDialog()
    8) a dialog pops up that prompts you to select the JSON file you created earlier.
    9) your temp actor now is the actor you wanted move from one world to the next, with all their items, spells, feats, etc etc etc.

Note

This will only work if both worlds are of the same game system, or else there will be tons of issues.