Tips on incorporating a past random selection into an RPG lifepath generator's current random selection process

Currently, I am in the process of creating a JavaScript version of a basic "life path generator" commonly seen in pen and paper RPGs. The generators I have been working on are inspired by the structure provided at this link:

However, I am encountering difficulties with making a reference to a previously selected random outcome.

The traditional pen and paper method involves rolling a d6 to determine the character's class (Bard, Cleric, Druid, Fighter, Rogue, Wizard) and then rolling a d20 to establish a unique background based on the chosen class.

My plan is to create arrays or objects for each set of data, but I am unsure of how to make the second set react automatically to the first random selection. Ideally, I would like to generate everything randomly with the click of a button.

Below is my current attempt at solving this challenge:

  var gen_data = {};
  gen_data['main'] = {
    'You are a {class}.' 
  if {class} = Bard '{bardBackstory}'
  else if {class} = Cleric '{clericBackstory}'
  else if {class} = Druid '{druidBackstory}'
  else if {class} = Fighter '{fighterBackstory}'
  else if {class} = Rogue '{rogueBackstory}'
  else if {class} = Wizard 'wizardBackstory}'
  };
  gen_data['class'] = {
    '1': 'Bard',
    '2': 'Cleric',
    '3': 'Druid',
    '4': 'Fighter',
    '5': 'Rogue',
    '6': 'Wizard
  };
  gen_data['bardBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };
  gen_data['clericBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };
  gen_data['druidBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };
  gen_data['fighterBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };
  gen_data['rogueBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };
  gen_data['wizardBackstory'] = {
    '1': 'story1',
    '2': 'story2',
    '3': 'etc'
  };

Answer №1

The generator.js script, found in random.html, utilizes regular expressions to scan strings for words enclosed in '{example_here}' and substitutes them with a random value from the object stored in gen_data['example_here']. By using additional {} symbols consecutively, a nesting effect can be achieved.

var gen_data = {};
gen_data['main'] = ['You are a {class}.']
gen_data['class'] = {
  '1': 'Bard. {bardBackstory}',
  '2': 'Fighter. {fighterBackstory}',
  '3': 'Wizard. {wizardBackstory}'
};
gen_data['bardBackstory'] = {
  '1': 'story1',
  '2': 'story2',
  '3': 'etc'
};
gen_data['fighterBackstory'] = {
  '1': 'story1',
  '2': 'story2',
  '3': 'etc'
};
gen_data['wizardBackstory'] = {
  '1': 'story1',
  '2': 'story2',
  '3': 'etc'
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rendering text with three.js in the scene

I'm just starting out with three.js. For my project, I need to create a 3D graphical website. After doing some research on Google, I discovered that three.js is a great tool for manipulating WebGL with ease. In the three.js documentation(), TextGe ...

Steering clear of inserting 'Array' into a database through autocomplete using Js, Ajax, and Json

I'm currently working on a script that auto-populates input fields based on the autocomplete feature of the first input field. Although the script works fine and everything looks good when I hit submit, the problem arises when I check the database. A ...

What factors should I consider when choosing the appropriate socket for receiving messages from a RabbitMQ queue?

I have encountered an issue while trying to connect to a queue on a remote server using Rabbit.js. Every attempt to connect results in the following error message: Error: Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITI ...

Guide to prompting a browser to download a file using an XHR request

When it comes to downloading files from a server using an XHR request, how can I ensure that the browser initiates the download once the response is received? Additionally, which headers should be included by the server for this process to work seamlessl ...

Tips for efficiently printing invoices on specific paper: Print a maximum of 20 items per sheet, and if it exceeds this limit, continue onto the next page. Ensure the total amount is

$(document).ready(function(){ var j = 23; for (var i = 0; i < j+11; i++) { if (i != 0 && i % 11 == 0) { $("#printSection div").append("<?php echo '<tr><td>fff</td></tr>'; ?>"); ...

The property 'dateClick' is not found in the 'CalendarOptions' type in version 6 of fullcalendar

Below is the Angular code snippet I am currently using: calendarOptions: CalendarOptions = { plugins: [ dayGridPlugin, timeGridPlugin, listPlugin ], initialView: 'dayGridMonth', headerToolbar: { left: 'prev today next' ...

A helpful guide on displaying validation errors in a form using React JS

After creating a form and connecting it to the server, I encountered an issue with displaying validation errors below respective input fields. The error message response in case of validation error is as follows: "message": "ValidationError: confirmPasswor ...

The Angular directive was unable to reach the controller located higher up in the DOM hierarchy

template <section id="content" ng-controller="ReservationController as resvnCtrl" > <form role="form" name="resvnCtrl.form"> <div data-date-picker> </div> ...

"Having trouble with sound in react-native-sound while playing audio on an Android AVD? Discover the solution to fix this

react-native-sound I attempted to manually configure react-native-sound but I am unable to hear any sound. The file was loaded successfully. The audio is playing, but the volume is not audible on Android AVD with react-native-sound. ...

Challenge with Saving Core Data Relationships

I've been facing an issue where I'm trying to add an entity to a set, but it's not persisting after the application is closed. Here's my relationship: https://i.sstatic.net/GCoPk.png When creating a new Task, I go about it like this: ...

Incorporating a computed variable in a v-select within a VueJs endless loop

I've been attempting to utilize a computed value in a v-select component from Vuetify, but every time I select an item, it triggers an endless loop. To demonstrate the issue, I have recreated my code in this CodePen. Please be cautious as it may caus ...

Unable to create a new collection in Firebase Firestore

I encountered an issue while trying to add a collection in Firebase Firestore using the function .collection(doc).set. Despite finding the new user in authentication in Firebase, the collection was not created and the console displayed an error message. ...

Is there a way to display the entire stack trace in Mocha when testing Promises and an error occurs?

Imagine I have a small specification like this: describe("feature", () => { it("does something", () => { return myPromiseBasedFn().then(result => { expect(result).to.eql(1); }); }); }); At the moment, when the promise is reject ...

Save the function as a local variable and preserve the reference to the current object

In the prototype of my Car object, I have a function that looks like this: Car.prototype.drive = function() { this.currentSpeed = this.speed; } I find myself needing to call this drive function frequently within another function of the Car prototype. ...

updating dropdown options based on user input with PHP

I need help with implementing a code for two dropdown boxes. The first dropdown should display main category values dynamically, and when a value is selected, the second dropdown should appear with corresponding sub-category values. How can I achieve this ...

Eliminate jQuery's delayed blinking effect with the use of an event

Utilizing the mouseenter and mouseleave events, I have implemented a functionality to add a button (not actually a button) to an <li>. However, there seems to be a problem with my code. The button appears and disappears on mouseleave and mouseenter, ...

Python does not display values while using the second for loop

I tried to implement random selection in my dataset, but I encountered a challenge when attempting to nest all the data into an array using a for-loop. Dataset data = [ {"Item1": "Book", "Item2": "Pen", "It ...

Transforming an NSArray into an Array in Swift can result in some values becoming nil in the Array

Facing a seemingly straightforward problem that I can't seem to solve. It involves converting an NSArray to an Array, and here's the code I have so far: func getTrainingSubCategories(completion: @escaping(Result<[TrainingSubCategory], GetErro ...

Fetch data from an API using JavaScript and load a JSON file

I'm currently facing an issue with this specific problem: I am attempting to retrieve the JSON file from using the following code: $("document").ready(function () { // Triggering function reLoad(); function reLoad() { $.getJSON( ...

Using Vue.js, separate the values that are separated by commas

I am looking to extract values from a string and store them in an array for use in displaying values in a dropdown format within Vuejs String str = "abc,123,676,uuu". Once I have iterated through the values <li v-for = "value i ...