[Suggestion] Scripting revamp

5 replies [Last post]
KoBeWi
KoBeWi's picture
User offline. Last seen 3 days 15 hours ago. Offline
Joined: 2010-12-25
Posts: 564

I had this idea after writing some LUA script for Hedgewars recently. The whole system could be improved, especially to avoid things like this:

LUA allegedly wrote:
SetGearValues(gearUid, Angle, Power, WDTimer, Radius, Density, Karma, DirAngle, AdvBounce, ImpactSound, ImpactSounds, Tint, Damage, Boom)
Obviously this can be broken down into many methods, but I had another thing in mind when scripting.

Now, this might be a matter of taste, but I found operating on "objects" really bothersome. Like, you don't have Hedgehog - you have it's gear id and supply it to method that is supposed to give you properties you seek. How about rethinking the system?

It would be cool if it was all more object oriented. There could be a wrapper for each gear Uid, so instead of doing GetHealth(gearUid) you could do Gear.GetHealth(). Or better, Gear.health, which would internally call a getter method returning the value. I'm sure there's a way to implement this without creating much overhead, by e.g. creating these objects on-demand.

Now, not sure how much of this can be done in LUA. LUA isn't a friendly language anyways. This is actually something I suggested long time ago, but I was misunderstood: how about adding Ruby as scripting language for Hedgewars? XD
Particularly the mruby implementation can be easily embedded, as it takes less than 1MB of space. This is the only language I know that allows calling methods without using (), so stuff like Gear.health would be possible and could transparently communicate with the engine. Wouldn't it be nice if you could do Gear.karma = 1, instead of SetGearValues(Gear, nil, nil, nil, nil, nil, 1)?

Ruby could be added as secondary scripting language or just replace LUA. In latter case, I'd gladly volunteer to rewrite all existing scripts Big Grin

So yeah, this >.>

nemo
nemo's picture
User offline. Last seen 11 hours 44 min ago. Offline
Joined: 2009-01-28
Posts: 1861

SetGearValues was shoved in as a quick wrapper to expose the majority of the gear struct for "less important" values that are rarely needed without creating another two dozen getters and setters. It was almost entirely due to laziness. Adding new ones was very easy. Ditto SetVisualGearValues.

Some of those later acquired their own Getters and Setters. I'll agree it's a mildly naughty function though.

Whether object oriented is necessary or even a good idea depends entirely on what language structure you personally come from. I don't personally think it would improve matters in our lua or how people use it, but if that's what you prefer, you're welcome to add an OOP-ish wrapper insofar as lua supports it. If it doesn't do anything too silly, we could probably formally include it in the support libs.

--
Oh, what the heck. 1PLXzL1CBUD1kdEWqMrwNUfGrGiirV1WpH <= tip a hedgewars dev

Wuzzy
Wuzzy's picture
User offline. Last seen 4 days 16 hours ago. Offline
Joined: 2012-06-20
Posts: 1301

I fully agree that the SetGearValues and GetGearValues functions are just crazy in that they have too many arguments. These are really ugly. I am also constantly annoyed by these.
The question is how to redesign it.
Your suggestion of just adding more functions per field is not much better. The Lua API already has so many functions.

Ideally, there would be some genericism. Something like SetGearValue(parameterName, value) where parameterName is for the gear value type you want to change (as a string), like "WDTimer", "Boom", etc. That would be super-easy to extend, too.

The idea of adding a completely new scripting language is just crazy. Especially if it does not even replace Lua, but is supposed to work as an alterative option, this would massively increase the complexity for no reason. It would not be fun to maintain two completely different scripting languages.
It's also utterly pointless since I highly doubt this new language would actually give us anything of value that the current system doesn't. All it really adds is a different syntax.

Hedgewars already is made out of 4 languages: Lua, Pascal, Haskell and Pascal. I think 4 languages are more than enough. We REALLY do not need another one! Enough is enough. Big Grin

Replacing Lua is not much better. Yes, Lua ain't perfect but it still works just fine. Plus, this step would automatically break all non-official scripts as well (for absolutely no reason, I want to add), so that alone is a complete no-go.

Hedgewars is not exactly programmed object-oriented. At least the engine. I don't think this automatically means that Hedgewars is bad. Object-orientation is just another style of programming and not the holy grail.

Some kind of object-orientation could be theoretically be done in Lua as well. But I don't see the point. The data structures of the engines aren't object-oriented either. What is more important is to make common tasks easier to achieve. Templates for stuff. That's why I added things like SimpleMission, SpeedShoppa and TargetPractice.

The main task for the Lua API is to expose engine features in a more or less accessible way. I think it mostly does. Some functions are a bit crap, but doesn't justify changing the entire paradigm. I'm all for small incremental changes.

Quote:
Ruby could be added as secondary scripting language or just replace LUA. In latter case, I'd gladly volunteer to rewrite all existing scripts :D

Thanks for the offer, but are you sure? You are probably underestimating this task. Especially the campaigns could be tricky.
Unless you have found some way to automate everything.

If you are really willing to do such an herculean task, I think you would be much more productive by working on things that actually matter in Hedgewars right now, like bugfixes and all that stuff. The bug and wishlist for Hedgewars is long, see our bugtracker. I would highly appreciate any work on high-severity / high-priority bugs and features.

Also, I am generally sceptical of any claims of the form “Oh [DISLIKED LANGUAGE] is so terrible! If [PROJECT] would just be using [OVERHYPED_LANGUAGE], everything would be rainbows and unicorns!”. All languages have flaws more or less, but most well-known languages are usually just doing fine. Except PHP. Wink Smiley

Hi, I am a Hedgewars developer. Smile

KoBeWi
KoBeWi's picture
User offline. Last seen 3 days 15 hours ago. Offline
Joined: 2010-12-25
Posts: 564

Wuzzy allegedly wrote:
Thanks for the offer, but are you sure? You are probably underestimating this task. Especially the campaigns could be tricky.
Heh, you underestimate my love for Ruby ( ͡° ͜ʖ ͡°)

I mean, I wouldn't be so willing if it was about any other language. I don't feel like bugfixing for the reasons I outlined in the other topic, unless I could build Hedgewars on Windows without any problems.

nemo
nemo's picture
User offline. Last seen 11 hours 44 min ago. Offline
Joined: 2009-01-28
Posts: 1861

I would say for the game scripting community, Lua is considerably more accessible than ruby - having an additional scripting interface is fine if someone wants to maintain it, but I wouldn't want to remove lua.

https://www.reddit.com/r/lua/comments/10oz5x/ruby_vs_lua/ (TLDR - simple, fast, unbloated)

--
Oh, what the heck. 1PLXzL1CBUD1kdEWqMrwNUfGrGiirV1WpH <= tip a hedgewars dev

Lyberta
Lyberta's picture
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 2016-09-10
Posts: 177

I'd vote for WebAssembly so ppl can use C, C++, Rust, Go, whatever and have compile-time checks. I'm really tired of firing up HW only to find that I've made a silly syntax error.

Also, you get pretty much all of the scripting languages for almost free because they are written in mostly C and C++ so porting them to WebAssembly should be easy.

Copyright © 2004-2023 Hedgewars Project. All rights reserved. [ contact ]