2019-07-21 04:03:46 (edited by amerikranian 2019-07-21 04:05:02)

So, I am messing with JavaScript because I currently have no idea what to do with python  project wise.  I wanted to create a log that keeps the most recent events occurred within a game. For example, something like this:
A peasant has been eaten by some wolves.
When a new message is received, this entry will shift down so the log would look like this.
The King demands his taxes be paid.
A peasant has been eaten by some wolves.
I can get the shifting part to work, it’s individual text that is giving me issues. One way I could do it is by setting up 10 text elements and give them unique IDs so I can control them individually, but I wonder if there’s a better, easier and less type free way to do it.   The method listed above  also limits the amount of entries I can have in my log at a time.  Suggestions? Tips?

2019-07-21 06:43:14

You could have an array of strings, and specify a maximum number of elements that you want to store at a time. Each time you receive a new message, put it at the end of the array. If the array is one above the maximum allowed quantity, remove the oldest item. Then print the entire array with whatever message separator you want such as a line break etc, to an element that will display the text. If you don't plan to have a huge log, that method will work well and is very simple to implement.

Kind regards,

Philip Bennefall

2019-07-21 11:39:55

philip_bennefall is correct, you should use an array:

const li = ["red","blue","green"]
console.log(li.reverse().join("\n")

This will make the list reversed, so latest is at the top, then the join function will add a newline in between each entry.
Then when you get a new item, just do li.push("new Item")

2019-07-21 19:03:35 (edited by amerikranian 2019-07-21 19:03:47)

Thank you for the help, I somehow forgot that arrays can be printed.