Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
p5.js loadStrings() is doing nothing

I'm using p5.js for my project, and I have to load some data from a file in another folder.

My js code is written in 'js' folder, and the data file is in 'txt' folder. Both 'js' and 'txt' folders are in the same root directory.

Here's my code,

var data;

function preload() {
   data = loadStrings('../txt/data.txt');
   console.log(data);
   console.log(data[0]);
}

and here's the logs:

0: "abcde"
1: "fghij"
2: "klmno"
length: 3
[[Prototype]]: Array(0)

undefined

and here's the request:

Request URL: http://localhost:3000/txt/data.txt
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin

It shows appropriate result when I print whole array, but when I access each data of array, it goes something wrong. What's wrong in my project?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since you are calling in the preload, you need to access the data in the setup or draw function.

function setup() {

console.log(data[0])
}

Otherwise, you will need to use a callback function to handle the data.

function setup() {
 loadStrings('../txt/data.txt', myCallback);
}

function myCallback(data) {
console.log(data[0])
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!