Jump to content
The Dark Mod Forums

A to Z Scripting series


Dragofer

Recommended Posts

@Dragofer That's a great public service you're doing! I was always meaning to make a scripting tutorial, but I just managed to make a simple page because it's practically a mini-course in coding by itself. So I recognize how much work goes into it, and what you've made so far is great.

I think it will get more mappers to get into scripting, as I think a lot stay away not because they couldn't get it, but just because the step-by-step instructions weren't there. Then that means we'll have better FMs, which is a win for everybody. Thanks!

What do you see when you turn out the light? I can't tell you but I know that it's mine.

Link to comment
Share on other sites

@Dragofer

Yes, thanks for this contribution! Scripting has always generated lots of “how do I?” discussions among the troops.

I’ve written tons of TDM scripts over the past decade, so if you run across one or more of my problems/solutions you’d like to use as examples, feel free to use whatever you need.

Thanks again.

  • Like 1
Link to comment
Share on other sites

@grayman Indeed, I had the idea for this guide while I was writing an answer to one of those "How do I..." questions. Those sure add up over time and sometimes it feels like I'm writing the same things.

I was actually thinking of dissecting i.e. your "frosty breath" script, so that's good to know. Well, as demagogue knows it's a long project, for now I'm happy with the demonstrations I have.

@peter_spy Being familiar with a programming language definitely helps a lot, but there's still plenty of TDM-specific aspects, i.e. the bulk of A to Z Scripting: Practical exercise: subtle teleportation concerns itself with how the map should be setup, which entities with which spawnargs, while the script itself is just a handful lines. TDM also has lots of subsystems like stim/response and objectives that can become part of a scripted setup.

Also, learning from a generic programming tutorial in order to script in TDM felt... dissatisfying. All that about memory management, classes and those fancy operators, but only a small fraction is actually relevant in TDM's scripting. Most mappers just want to make x happen as a result of y.

  • Like 1
  • Haha 1
Link to comment
Share on other sites

17 minutes ago, Dragofer said:

Also, learning from a generic programming tutorial in order to script in TDM felt... dissatisfying. All that about memory management, classes and those fancy operators, but only a small fraction is actually relevant in TDM's scripting. Most mappers just want to make x happen as a result of y.

That's why I bounced off a C++ course, even though it was really comprehensive (or maybe exactly because of that). But my first language was Java, so no need for manual memory management ;)

Link to comment
Share on other sites

12 hours ago, Dragofer said:

Also, learning from a generic programming tutorial in order to script in TDM felt... dissatisfying. All that about memory management, classes and those fancy operators, but only a small fraction is actually relevant in TDM's scripting. Most mappers just want to make x happen as a result of y.

I certainly don't agree that only a small fraction of a C++ tutorial is relevant to TDM scripting, why, mostly because DoomScript is pretty much inspired on C/C++ and OOP (object oriented programming), the syntax may be a little different but the concepts are the same in many aspects.

You can also manage memory directly in DoomScript (thou is less deep than c++ because of the lack of pointers), just like in c++, you have new and delete, so you can create and delete manually, data from the heap (PC RAM).

Second is a static typed language just like C/C++, meaning when you define a variable you need to say what type it is.  On JavaScript for example you just say "var whatever = whatever;" no need to say the type, it is inferred automatically by the value you give it, and now, you can do the same in c++ with the auto keyword.  ex:. auto myVar = 1.0f. // this will be a float automatically. 

About classes, imo Objects in doomScript are classes, they are almost a one to one to c++ classes/structs but only with single inheritance, objects have constructors, destructors, methods (functions), virtual functions, internal (static) defined variables and you can put objects inside other objects, just like c++ classes.

Printing in DoomScript is also very similar to c++ (and C).

C++                              cout << "some text: " << someIntvar << "some other text" endl;

DoomScript                  sys.println("some text: " + somefloatvar + " some other text");

C                                  printf("some text: %d  some other text\n", someIntvar);

The use of C like compiler macros like #define, #if #else and many other similarities. 

Link to comment
Share on other sites

I agree with HMart and may add, that concepts of oop have also found their way into other aspects of Doom3 and therefore TDM. You just have to take a look at entity definitions and the entity class tree. The concept of inheritance plays an important role here.

 

Of course you don't need to know about every aspect of programming just to be able to make proper use of scripting, but you definetely need the basics. What aspects are important and what not is something hard to judge by someone new to that matter, but experienced people can tell you. I did so in the past and my experience is that people simple don't want to, as they don't want to deal with how graphic engines work to improve the performance of their missions or to deal with modeling programs etc...

 

If there is a wiki article people somehow expect it to be simple and fast to learn. But if you point them to anything longer than five sides things get uncomfortable. If everything else fails you can still ask someone else for help ;)

  • Like 2

FM's: Builder Roads, Old Habits, Old Habits Rebuild

Mapping and Scripting: Apples and Peaches

Sculptris Models and Tutorials: Obsttortes Models

My wiki articles: Obstipedia

Texture Blending in DR: DR ASE Blend Exporter

Link to comment
Share on other sites

1 hour ago, Obsttorte said:

don't want to, as they don't want to deal with how graphic engines work to improve the performance of their missions or to deal with modeling programs etc...

I always found that a bit weird. Every form of creative expression is connected to technical abilities, there's no way around it. And natural curiosity about everything, the technical side of things included, is a kind of prerequisite, assuming you want to get better at stuff. At some point, the time and effort you make to avoid learning new things actually wastes more time than you'd need to get familiar with modelling, programming, etc. The initial time investment might seem like much, but then you both improve your skills, and work much faster than you did. Kinda similar to time investment in test automation ;)

From my experience, that definitely applies to modeling, I suspect it's similar with programming and other things as well.

Link to comment
Share on other sites

1 hour ago, peter_spy said:

I always found that a bit weird. Every form of creative expression is connected to technical abilities, there's no way around it. And natural curiosity about everything, the technical side of things included, is a kind of prerequisite, assuming you want to get better at stuff. At some point, the time and effort you make to avoid learning new things actually wastes more time than you'd need to get familiar with modelling, programming, etc. The initial time investment might seem like much, but then you both improve your skills, and work much faster than you did. Kinda similar to time investment in test automation ;)

From my experience, that definitely applies to modeling, I suspect it's similar with programming and other things as well.

If the user wants to create something advanced than of course yes. But if it's just to make a clever map than Dark Radiant should be ideally sufficient. Portal 2 is a simplified comparion for this. The in-game map creator is simple, but easy to use for anyone. At the same time, if anyone wants something more advanced, with scripts and whatnot, than they can use Hammer. But it's not uncommon for unskilled people to make another bad map named "teh impossru test play this nao". What's worse, is that the map created with Hammer is more likely to be buggy, convoluted and broken.

Edited by Anderson

"I really perceive that vanity about which most men merely prate — the vanity of the human or temporal life. I live continually in a reverie of the future. I have no faith in human perfectibility. I think that human exertion will have no appreciable effect upon humanity. Man is now only more active — not more happy — nor more wise, than he was 6000 years ago. The result will never vary — and to suppose that it will, is to suppose that the foregone man has lived in vain — that the foregone time is but the rudiment of the future — that the myriads who have perished have not been upon equal footing with ourselves — nor are we with our posterity. I cannot agree to lose sight of man the individual, in man the mass."...

- 2 July 1844 letter to James Russell Lowell from Edgar Allan Poe.

badge?user=andarson

Link to comment
Share on other sites

8 hours ago, Obsttorte said:

I agree with HMart and may add, that concepts of oop have also found their way into other aspects of Doom3 and therefore TDM. You just have to take a look at entity definitions and the entity class tree. The concept of inheritance plays an important role here.

 

Of course you don't need to know about every aspect of programming just to be able to make proper use of scripting, but you definetely need the basics. What aspects are important and what not is something hard to judge by someone new to that matter, but experienced people can tell you. I did so in the past and my experience is that people simple don't want to, as they don't want to deal with how graphic engines work to improve the performance of their missions or to deal with modeling programs etc...

 

If there is a wiki article people somehow expect it to be simple and fast to learn. But if you point them to anything longer than five sides things get uncomfortable. If everything else fails you can still ask someone else for help ;)

Is certainly true that some people are afraid to learn new things, for a time I was one of them, my first foray into programing was with LUA that I liked (because of being a procedural/functional language), was so simple and direct that for a time, I didn't wanted to learn any other programming language, certainly not C++ because of all the bad rep it add, that I now know was mostly bs. But truth be told, C++ has some really stupid design decisions.

Btw the best way to learn is to teach others and so I will be glad to help anyone with questions on c++ (I don't code in pure C++ so mostly C thou), within my own capaibilities/knowlege on the language and available time. 

  • Like 2
Link to comment
Share on other sites

Clearly people differ in what they want out of a wiki tutorial, and where they are on the quick-read versus more-depth spectrum. I generally prefer a bit of depth. Coming to TDM with OOP experience already, I'm interested more about TDM-specifics; so with Dragofer in that regard. As a for-instance, the recent discussion in the Newbie Forum that you can declare an object of type "ai"; didn't know that before.

Link to comment
Share on other sites

On 12/24/2020 at 10:58 AM, HMart said:

I certainly don't agree that only a small fraction of a C++ tutorial is relevant to TDM scripting, why, mostly because DoomScript is pretty much inspired on C/C++ and OOP (object oriented programming), the syntax may be a little different but the concepts are the same in many aspects.

I would say it looks very much like C++, but it is quite far from it. It is closer to javascript, PHP, or all the rest of high-level language. Yes, the idea of classes and inheritance is important, but it is now present in most languages.

Quote

On JavaScript for example you just say "var whatever = whatever;" no need to say the type, it is inferred automatically by the value you give it, and now, you can do the same in c++ with the auto keyword.  ex:. auto myVar = 1.0f. // this will be a float automatically. 

There is major difference between static and dynamic typing. And "auto" keyword in C++ does not make it dynamically typed, so there is huge real difference with how javascript works.

Quote

certainly not C++ because of all the bad rep it add, that I now know was mostly bs. But truth be told, C++ has some really stupid design decisions.

I would say C++ is the "language of lifetime". You need a lifetime to master it enough to feel free using it. And the rest of your lifetime to catch up with new things piled upon the old things 😁

I guess only very scarce knowledge of C++ is helpful to deal with DoomScript. Basically, the C basic syntax, C preprocessor, C++ class/inheritance/virtual functions, and probably that's all. Then just practice and get generic programming skills, then look at doomscript examples and try to do something similar.
And for God's sake don't ever read anything about "threads" in C++! You will be lost for scripting forever 😁

  • Like 1
Link to comment
Share on other sites

7 hours ago, stgatilov said:

I would say it looks very much like C++, but it is quite far from it. It is closer to javascript, PHP, or all the rest of high-level language. Yes, the idea of classes and inheritance is important, but it is now present in most languages.

Yes I agree, is similar in concept to high level languages like javascript, but the syntax and some basic concepts are more alike to C++ than PHP or javascript.  This seems to be what idSoftware also agrees, they say the following about doomScript:

Quote

Scripts are about as close to programming Doom 3 as you can possibly get without having to purchase Visual C++. The language is similar to C++, but is arguably a bit easier to learn and use.

The scritping system is based around the idea of objects and events.

Objects are referenced with $name where "name" is the name of the object in DOOMEdit.

Events look like C++ member function calls, and you can really think of them that way. To send the 'hide' event to speaker_280, you'd write $speaker_280.hide();

This also means anyone learning doomScript can more easily upgrade into C/C++ than anyone using PHP for example. :)

There is major difference between static and dynamic typing. And "auto" keyword in C++ does not make it dynamically typed, so there is huge real difference with how javascript works.

Oh I know that, I didn't mean that auto makes c++ a dynamically typed language! I just meant that from c++11 forward, you can use auto to automatically infer types now, but I know it only works in some cases, mostly when you give it a value or a hint at definition time, if not the compiler obviously cannot infer the type just from the function name. :) 

example: 


auto somevar; // Error at lest from c++11 and above.
float somevar; // No Error 
auto somevar = 1.0f; // No error this is like saying, float somevar = 1.0f;

I would say C++ is the "language of lifetime". You need a lifetime to master it enough to feel free using it. And the rest of your lifetime to catch up with new things piled upon the old things 😁

I guess only very scarce knowledge of C++ is helpful to deal with DoomScript. Basically, the C basic syntax, C preprocessor, C++ class/inheritance/virtual functions, and probably that's all. Then just practice and get generic programming skills, then look at doomscript examples and try to do something similar.
And for God's sake don't ever read anything about "threads" in C++! You will be lost for scripting forever 😁

I couldn't agree more on this, you are exactly right. :)

 

Link to comment
Share on other sites

A somewhat random feedback: I tried to invoke a script by using console command, as described here: https://wiki.thedarkmod.com/index.php?title=A_to_Z_Scripting:_Ways_of_calling_a_script#From_the_console

Quote

name_of_script();

Either I did something wrong, or it doesn't work. Also not sure why would I want to use a semicolon at the end of a function name, in the console command.

Edited by peter_spy
Link to comment
Share on other sites

Ah, looks like I never got around to testing that method. You need to write as follows, either with or without a semicolon:

Quote

 script name_of_script()

 

Link to comment
Share on other sites

Yup, now it works. Semicolon is typically used to mark an end of a statement in a method, that's why I find it weird to place it after a method name (it makes sense when a method calls another method in its body, as this is a statement).

Another thing that caught my eye, when you're talking about types, you don't mention ints. Storing numeric values in floats makes sense if they need that kind of precision. So if you want to convert a boolean to a number, int would make more sense.

Link to comment
Share on other sites

I always believed that the console was meant to allow some basic scripting from the console, i.e. by combining a few events/functions, so a semicolon made sense to me. However, it seems quite limited in terms of what actually works, i.e. script $func_static_1.setFrobable(1) has no effect ingame, you'd have to write your own script function with that event and call that instead.

DoomScript doesn't support int as a data type, but you have the option of turning a float into an "int" with a script function (utility script) like this. Should probably integrate that into the guide somewhere.

float test_variable1 = int(5.8);   //returns 5

 

Link to comment
Share on other sites

18 minutes ago, Dragofer said:

DoomScript doesn't support int as a proper data type

That's really weird. I understand that it made no sense to support all C++ data types, but I was taught that using floats when you can use ints is wasteful, memory and performance-wise.

Link to comment
Share on other sites

DoomScript is weird to begin with (to me, an amateur at coding) because it's using C++ forms (made for compiling) as a script language (running real time on an interpreter). I don't mind too much because I basically learned C++ just by learning to code for TDM anyway.

  • Like 1

What do you see when you turn out the light? I can't tell you but I know that it's mine.

Link to comment
Share on other sites

On 12/28/2020 at 7:38 PM, peter_spy said:

That's really weird. I understand that it made no sense to support all C++ data types, but I was taught that using floats when you can use ints is wasteful, memory and performance-wise.

That is theorically right but today CPU's are, way way faster and have better float support than at the time that first recommendation was made, so I don't think is so important anymore but I'm not a expert far from it.

For example in the attachment bellow, is John Carmack saying he should use as default, 64bits types! He doesn't, because "old habits die hard", but if he says using such big types is fine then it most be, who I'm I to doubt him that man is a genius coder, I'm not...

jonhcarmack.GIF

Edited by HMart
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • Petike the Taffer

      I've finally managed to log in to The Dark Mod Wiki. I'm back in the saddle and before the holidays start in full, I'll be adding a few new FM articles and doing other updates. Written in Stone is already done.
      · 4 replies
    • nbohr1more

      TDM 15th Anniversary Contest is now active! Please declare your participation: https://forums.thedarkmod.com/index.php?/topic/22413-the-dark-mod-15th-anniversary-contest-entry-thread/
       
      · 0 replies
    • JackFarmer

      @TheUnbeholden
      You cannot receive PMs. Could you please be so kind and check your mailbox if it is full (or maybe you switched off the function)?
      · 1 reply
    • OrbWeaver

      I like the new frob highlight but it would nice if it was less "flickery" while moving over objects (especially barred metal doors).
      · 4 replies
    • nbohr1more

      Please vote in the 15th Anniversary Contest Theme Poll
       
      · 0 replies
×
×
  • Create New...