Scripters: Please complain more!

For porting of the campaign to javascript discussions

Scripters: Please complain more!

Postby Per » 22 Apr 2017, 19:49

I've had a look at the campaign library again, and it struck me that it is making use of some very clever, very complicated workarounds for the fact that I made some bad design choices when I made the original javascript API. Basically, when I made it, I did not anticipate that anyone would make libraries, and the campaign script has to work around that.

One of the guiding ideas when I made the API initially was, besides to be familiar to people who already knew WzScript, was that it should be simple enough that people not familiar with programming could have fun with it and make interesting stuff (and perhaps even use it to learn coding). But maybe I made things a bit too simple.

For example, the __camPreHookEvent stuff in the campaign library, to work around the limitation that you can only register a single event handler for any game event type. It is neat, but not easy to understand. I would actually prefer if you guys complained about such things, and we improved the API, rather than making such complicated workarounds in the scripts. It shouldn't be terribly complicated to add a function to register additional event handlers, for example.

So I guess what I'm trying to say is, please complain more - I'm happy to fix the API, and it is usually not that hard to do :-)
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
 
Posts: 3582
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Postby NoQ » 22 Apr 2017, 20:09

Thanks for that topic, yeah, that's an important thing to consider><

I guess i was too obsessed with a new fun thing that i've learned to use my imagination for a better API. It haven't crossed my mind at all until this post :oops: Also, it seems that the way we did it is very similar to how javascript normally works in browsers, and as far as i understand from my little experience, that's what people consider normal.

What you're proposing is essentially a C-side implementation of hooks to replace my machinery. I'm also fine with, say, having eventGameInitWithAnyWeirdSuffix() be auto-detected as an extra handler - that'd be even easier to write and understand. The game may scan the script's global object right after loading the game to find such functions and register them as handlers.

In any case, we'd somehow need to figure out what to do if the user wants to ensure handlers fire in particular order. I suspect (don't remember) that campaign's hooks being *pre*-hooks (firing before the level-scope event handlers) is significant.
User avatar
NoQ
Special
Special
 
Posts: 6048
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Postby Per » 21 May 2017, 23:12

I attempted to code up a solution to the event namespace issue: https://github.com/perim/warzone2100/commit/070cb1b4f7b2e0bb00e53c98d839023b03e8a58d

However, I am not sure how to change the area event handling in the campaign library. See https://github.com/perim/warzone2100/commit/070cb1b4f7b2e0bb00e53c98d839023b03e8a58d#diff-5e70e6628a3ad1898edd07a752444477R2351

Suggestions?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
 
Posts: 3582
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Postby NoQ » 22 May 2017, 17:22

As far as i understand, we pursue the awesome goal of eliminating the camAreaEvent() function (otherwise the solution is trivial).

I'm not immediately seeing any awesome solution.

As a quick solution, which would still be a better than the old approach, we could, in libcampaign, enumerate all eventArea<> handlers defined in __camGlobalContext() (by iterating through all keys) and highlight the labels with the given names.

+// Make sure no other library uses
+// the same namespace, or strange things will happen.

I guess we could detect this and crash instantly.
User avatar
NoQ
Special
Special
 
Posts: 6048
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Postby Per » 22 May 2017, 22:51

As far as I can tell, camAreaEvent() is the only user of the weird eventArea<label>(droid) event. That you choose to wrap every use of it suggests to me that it wasn't designed so well, but I'm not sure what is wrong with it. The camAreaEvent() function comment says "Implement eventArea<label> in a debugging-friendly way". So how could the area event be implemented better, so that it wouldn't need this somewhat strange wrapping?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
 
Posts: 3582
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Postby NoQ » 23 May 2017, 10:04

Hmm. Probably a jsdebug button (and/or a separate builtin cheat) for highlighting all area labels, or, even better, all "active" area labels (i.e. areas that would trigger events when entered) would be a great replacement for the camAreaEvent() functionality.

Hmm. In fact, right now simply by entering cheat mode you give away spoilers to yourself, like highlight active labels or debug dumps in the console. This is a great thing to have for the author of the script, but not so much for people who simply want to cheat in campaign in order to have fun cheating.
User avatar
NoQ
Special
Special
 
Posts: 6048
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Postby Per » 25 May 2017, 22:56

I've pushed the patch to implement library namespaces - and changed all events in libcampaign.js except the area handler. (I hope I did not break anything - please check.) The reason I did not touch the area handler is because I'm not very happy about the way I originally designed the area event API - it looks ugly and strange to me - but I'm struggling to come up with something more elegant.

I also added new debug menu buttons to show all labels, all active labels, clear all labels, and show gateways.

The 'show all labels on cheat' is still triggered from inside libcampaign - and I guess that is no longer needed now that the debug menu can do this.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
 
Posts: 3582
Joined: 03 Aug 2006, 19:39


Return to Campaign