How to utilize C++ for scripting?

Started by GangstaRas, Apr 23, 2015, 11:41 PM

Previous topic - Next topic

GangstaRas

Please Note: I'm 100% fresh to the scripting world and generally fresh to the programming world (C++ that is) so to some things I may say like I know it, I assure you, guidance may be needed everywhere so please bare with me.

Okay so I saw a post by Stormeus explaining that C++ is usable due to plugin SDKs available so that you can make your own plugin. From my presumption, it would appear that I'd have to get some IDE type program that is able to compile a dll file with the specified header details and input the C++ function library in it to make it work as a plugin ready for use with server.exe. But if it was that "simple" then where's a sample dll for something like this? Something called c++04rel64.dll or something if I'm having the right idea behind all this? Or is that if I make write a set of functions right now in C++ style and save it as main.nut everything works.

Being a beginner is not bliss. Someone please clarify C++ support and 0.4 and the steps necessary for it to work if any with me please. A lot of the processes behind getting into scripting isn't really in layman's terms.

Stormeus

The Squirrel plugin is basically a shim for the C++ API. Most functions in Squirrel just call their equivalent API functions with the appropriate parameters. Callbacks are implemented by using plugin events and, in a few cases, using those events to implement lesser callbacks like onPlayerHealthChange.

That being said, there's a significant level of abstraction between Squirrel and the API because Squirrel has garbage collection, tries to be memory-safe, and is capable of catching numerous exceptions from issues such as incorrect and invalid parameters. C and C++ leave all of this entirely up to the developer, and the reason you don't see an example of a C++-implemented script like c++04 is because:

1. This is a significant undertaking. You are basically writing an entire scripting layer from scratch.

2. We don't want to give even the slightest impression that this is something that should be done casually. I can personally say without a doubt that at least 9 out of 10 VC:MP players who would think a C++ script would be a decent idea to try would immediately flood the forum complaining about crashes while neglecting even the most basic debugging information, or even knowing how to debug a C++ binary in the first place.

3. It's not necessary for us to really show a demonstration of a C++ "script" to those who would be capable of doing so. The documentation for the plugin API largely exists through the source code of existing plugins, and would be enough of a starting point for people to start developing a server script written as a plugin.

Your basic workflow would be to have the plugin and VcmpPluginInit start attaching callbacks to things like OnInitServer (to load databases and prepare the server for players) and OnCommandMessage (which would use string comparisons to determine what commands are being run). When commands are obtained and parsed, you would perform the appropriate action by calling the necessary plugin functions. You would most likely have a global variable declared as extern across your code for the functions struct of the SDK, so you can call functions anywhere in your code. Otherwise, you could put this in a core class with an accessor which would also be global.

Like I said before, though, there are numerous caveats that should be paid attention to. For example, null commands exist. The simple / character actually triggers the command callback, but the command text will be null and thus have nothing in it. Trying to do a string comparison on it will immediately crash the server. Most casual scripters would not know this and most C++ script attempts would thus be vulnerable to this.

Attention to minute details that could crash the server with ease are imperative to pulling something like this off, and were a significant challenge to address in beta testing when we had to use a C++ script because Squirrel wasn't implemented yet. Not even I would recommend it as anything other than a fun challenge, unless you're insane enough to try this on a complex enough level, as the performance gains as opposed to the time required to maintain and develop something like that aren't worth it.

If you do still want to try this and have more questions about it, don't hesitate to reply.

GangstaRas

#2
Well honestly if my competence level was up there to all of this, I'd have a go at it mainly for familiarity's sake but I think I see what you're saying with these vulnerabilities; to create exceptions for all these possible weak points that are able to crash the server, maddening. So then that makes me wonder who's the team behind LW because based on their server description, it seems their pulling off C++ for the most part.

As a newbie that aspires to write his own code instead of editing someone else's to fit my needs, where should I go? It was advised that I should learn C++ to get a familiarity with the syntax that is going to be used in Squirrel and the different function qualities I may call upon to solve varying scenarios. And because of it I did understand some of the things when I look at sample scripts but what I can't understand for example is the linking of complementary files.

For example there is a sample script that was posted by rObInX of an ADM server. This server has 4 scripts. A main script that houses mainly player related functions, a functions script that holds his events, an admin scripts for admin commands and an echo script for IRC channels. In none of these scripts have I seen anything that even looks remotely like the main script is calling on the others so I just assumed that the server.exe just reads each script and loads them. So I copied these extra scripts over into a VCMP Blank Server template I have to see if they will work and they did not so I checked through every other supporting files to see if there was some linkage happening in the codes and I'm not seeing it but I didn't see anything out of the ordinary in server.conf and server.cfg.

Also the documentation that I'm seeing for learning Squirrel is a referencing document and not a true guide like there is for Pawn VCMP. So idk where to go to start coding something for my server. I just feel stuck

.

@GangstaRas That "linking" in the scripts is happening exactly like this. In server.conf you have the main script as:
sqgamemode main_script.nut
And then somewhere in main_script.nut you include the others as well:
dofile("commands.nut", true);
dofile("functions.nut", true);
dofile("database.nut", true);
dofile("utilities.nut", true);
// etc....
.

GangstaRas

I saw the sqgamemode only in server.cfg and its this line he has:
sqgamemode scripts/main.nutBut yes thank you I finally saw it, its discretely put into the onScriptLoad function (which also makes to me where its put) of main.nut. You wouldn't know how much headbanging and stress that just relieved

.

Anyway, back on topic. Take @stormeus 's advice and don't use C++ for "scripting". Why? You said that your self. You're at the beginning and have no idea about it. And I can guarantee you (100%, strong guarantee) that you'll bang your head much more if you take that path. The man wouldn't have given you that advice if he wouldn't know what's expecting you on the other side.

Things won't be that easy on C++. There won't be any pretty errors to give you the exact file and line where the error is caused and every crash is a permanent crash. There's no room for error in C++. Debugging is heeeeeeelllllll if you don't know what to expect from your code.
.

GangstaRas

Hmm, so if I get a logical error in my Squirrel script, can it be debugged through a debugger?

.

Not really a debugger. Actually, the debugger is you. But you do get a nice error message with the file and line number where that happened so you can fix it. And even if there's an error, the script might continue after that. C++ on the other hand won't offer you something that easy ;D
.

GangstaRas

Ah I see. Well from all my experience with C++ so far, I've fixed everything without any help of a debugger so that experience will pay off working with scripting as its not that bad on me now than when I first started but its the "everything works without errors but what you wrote isn't doing what you wanted it to do" type of scenarios I'm kinda running from. So that's where the only solution is forum experts? :P

Shadow

#9
I first got acquaitanced with PAWN language (which is crap tbh). Then Squirrel and C++. C++ can be so, so annoying at times and it's a headbanger even for the most expertised ones, as it takes more to debug (about double the time it'd take to write half of the server code) and errors are not always explicit, and even debuggers might fail to make you understand.

Your best bet for now is Squirrel as @stormeus and @S.L.C have said. If you didn't know, you can start off with it's syntax from these two useful links:

Squirrel 3.0 Documentation
Electric Imp Squirrel Documentation
QuotePS:is trash is ur home language??