Scripters: Please complain more!

For porting of the campaign to javascript discussions
Post Reply
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Scripters: Please complain more!

Post by Per »

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 :-)
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Post by NoQ »

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.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Post by Per »

I attempted to code up a solution to the event namespace issue: https://github.com/perim/warzone2100/co ... 3b03e8a58d

However, I am not sure how to change the area event handling in the campaign library. See https://github.com/perim/warzone2100/co ... 44477R2351

Suggestions?
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Post by NoQ »

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.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Post by Per »

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?
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Scripters: Please complain more!

Post by NoQ »

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.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Scripters: Please complain more!

Post by Per »

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.
Post Reply