Jump to content

Loading text file in an array in Phaser 3


keysabyl
 Share

Recommended Posts

For a game I would like to load a text file (ex. fruit.txt) with some words in an array in Phaser 3. How can I do this?

example of fruit.txt:

apple
banana
strawberry
lemon
peach
pear
...  

array:

fruit[0] = apple
fruit[1] = banana
fruit[2] = strawberry
fruit[3] = lemon
fruit[4] = peach
...

Link to comment
Share on other sites

I found the solution myself:

public preload{
   this.load.text('fruit', 'assets/text/fruit.txt');
}

public create{
   this.loadFruit();
}

 private loadFruit(){
    let cache = this.cache.text;
    let myFruit = cache.get('fruit');
    this.arrayFruit = myFruit.split('\n');};
    console.log(this.arrayFruit[3]); // >>> lemon
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...