Looking for a trigger after logs are loaded

Game development with Board Game Arena Studio
Post Reply
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Looking for a trigger after logs are loaded

Post by JonChambers »

I've got a function that will pretty up the logs a little. Ultimately, it adds HTML elements into the output display of the logs without cluttering up the logs on the back end.

I'm quite impressed with how well it works, but the trouble is the setup function is called BEFORE the logs are loaded. So calling it from setup results in absolutely nothing happening.

The first solution I can think of is starting a timer and then calling the function after an arbitrary length of time. Trouble is, too little time and the function could call before the logs are displayed. Too long and the beautification is laggy.

The next solution was maybe imbedding script tags into the first log, and calling it from there. I haven't tried that yet, so I don't know whether or not there is security protocol specifically designed to stop me doing stuff like that. Maybe there is. Also, writing script into a log feels a bit inelegant. (Also, if it runs the risk of executing while someone is browsing my logs from a different system, and maybe doing damage unintentionally if there's a namespace clash.)

The third solution, and probably the one I'll end up using if no one can think of something cleaner, is to just write the pretty html directly into the logs. Still a bad solution, but the least bad available.

What other solutions are there? The ultimate solution would be finding a way to have my function trigger as soon as the logs have loaded.
User avatar
robinzig
Posts: 461
Joined: 11 February 2021, 18:23

Re: Looking for a trigger after logs are loaded

Post by robinzig »

Usually this is done by overriding the client-side "format_string_recursive" method, as described in detail in the docs here: https://en.doc.boardgamearena.com/BGA_S ... in_the_log
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Looking for a trigger after logs are loaded

Post by JonChambers »

Thanks for the link. I've had a quick look, and I THINK I understand it.

The main thing I'm not sure about is is "format_string_recursive" an arbitrary name that would work no matter what it's called? Or is there something special about that name that gets triggered at the right point?
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Looking for a trigger after logs are loaded

Post by JonChambers »

Okay, I had a play, and for anyone reading along with the same problem: this is the important bit. (Yes, the function name must be spelled exactly that way.)

Code: Select all

format_string_recursive : function(log, args) {
    //do stuff
    return this.inherited(arguments); //the entire game crashes without this bit
},
I'll do some "do stuff" tomorrow, but keep in mind, it seems to execute 7 times for some reason. (I did a console log ;) ) I noticed the code example made sure to only execute the if statement once, and there are lots of ways of doing that.

I think that was the trigger I needed, but I'll post one more update here another day to let everyone know what I learned.
User avatar
robinzig
Posts: 461
Joined: 11 February 2021, 18:23

Re: Looking for a trigger after logs are loaded

Post by robinzig »

JonChambers wrote: 02 August 2021, 10:29 Thanks for the link. I've had a quick look, and I THINK I understand it.

The main thing I'm not sure about is is "format_string_recursive" an arbitrary name that would work no matter what it's called? Or is there something special about that name that gets triggered at the right point?
You do need to use precisely that name.

Others who know the framework better than me can doubtless explain it better, but as I understand it: this method is called automatically by the framework when putting the log messages on the screen. There is a default one that does all the stuff it needs to do, but by defining your own method of the same name you are overriding the default so that you can output whatever pretty HTML you need for your game depending on the notification arguments. If you're familiar with OOP, the client-side parts of the BGA framework (or possibly dojo) basically fake class-based inheritance, which is why the code includes the line "return this.inherited(arguments)" to make sure that the default method, from the "parent class" is still called. (Javascript doesn't really have class-based inheritance - although there are various ways to fake it, including the one now built into the language since ES6 with the "class" keyword. I'm sure if the BGA framework was being created now it would use that, but it's old so it doesn't and does its own thing. You don't need to worry about the details though - I certainly don't, most of the time!)

EDIT: started on this before seeing your latest post, I see you've figured this out yourself, the "hard" way
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Looking for a trigger after logs are loaded

Post by JonChambers »

robinzig wrote: 02 August 2021, 12:23 EDIT: started on this before seeing your latest post, I see you've figured this out yourself, the "hard" way
Yes, and continued to do it the "hard" way. I've removed all the code and put it back in line by line until I understand how it works and what it does.

Not quite put it back. It's all rewritten in my own "voice" as a developer.

That's been part of the challenge. Everything written by me, I've worked hard to make it match the style of the community, and everything written by the community I've worked hard to make it match my style. It probably seems like a pointless restriction to place on myself, but I've learned a lot in the process! I could only code functionally when I started this process, but now I think I'm fluent in Object Oriented.
User avatar
robinzig
Posts: 461
Joined: 11 February 2021, 18:23

Re: Looking for a trigger after logs are loaded

Post by robinzig »

JonChambers wrote: 03 August 2021, 05:16 That's been part of the challenge. Everything written by me, I've worked hard to make it match the style of the community, and everything written by the community I've worked hard to make it match my style. It probably seems like a pointless restriction to place on myself, but I've learned a lot in the process! I could only code functionally when I started this process, but now I think I'm fluent in Object Oriented.
I can totally sympathise with that. I'm also pretty new here and like you am much happier with functional programming as a style than OO.
Post Reply

Return to “Developers”