Lesson 0 - The science behind mods

Lessons and tutorials about mods and maps
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Lesson 0 - The science behind mods

Post by Feralidragon »

Making a mutator, mod or addon for the game often involves programming (also called "coding"), so your mod behaves the way you want it to, but what's a program?

How does "code" exactly work?

If you never wrote a single line of code in your life, these are the questions you're probably asking.
In short, programming is nothing but teaching your computer what to do and how to behave with something.
By computer, I mean anything you can execute with it, be it a game, an application, an engine, an operating system, and obviously, mods and addons for games like UT.


For instance, let's say you have a son called John and instruct him to do the following tasks everyday:
(I am not a parent, but this is the best analogy I have)

Code: Select all

- wake up at 7:00AM
- eat breakfast
- go to school
- have lunch
- more school
- go home
- have dinner
- take out the garbage
- feed the dog
- do the homework
- have fun
- go to sleep at 9:00PM
And assuming John is a good boy, he will execute those instructions you "programmed" him to.
Also, the fact that he repeats the same behavior everyday makes it a "routine".
Programming is no different, it works the same way, but instead of telling John what to do, you tell a computer what to do instead.

And just like John, you must instruct it using some sort of language. If he only understands English and you try to instruct him in Chinese, he won't do anything at all and will instead ask you to speak English instead, the sole language John understands.
In programming is the same, there are a multitude of programming languages just like there are spoken languages around the world, each one with its own words, grammar, "syntax" and punctuation. But instead of Chinese or English, you have UnrealScript, Java, C++, etc, therefore in order to be able to instruct your computer to do something, you must "talk" to it in a language it understands.

So as you may have understood so far, it's pretty much like writing a list of tasks to someone, but instead of writing it in English using the English grammar, you use UnrealScript instead, and this is called "writing code" or "coding".
However, you will find that the computer is the biggest grammar extremist you will find, as while people still understand what you mean despite your grammar mistakes, the computer does not, and it will instead obligate you to write using perfectly written grammar, otherwise it won't do anything at all, as when the computer finds a grammar error in your instructions (something it doesn't understand), it will dispatch you a "syntax error" message, providing information and tips in what you did wrong.

But as long as you follow its language rules perfectly fine, the computer will do whatever you tell it to do.

To conclude this, I leave you a hypothetical example on how English could translate to UnrealScript:
English:

Code: Select all

If John has homework to do, he must do it first and only then he can go out to play
UnrealScript:

Code: Select all

if (John.hasHomework())
    John.doHomeWork();
John.goOutToPlay();
________________________________________________________________________________________________________________________

TheLame
Novice
Posts: 26
Joined: Tue Jan 22, 2013 12:37 pm
Personal rank: retired UT reject

Re: Lesson 0 - The science behind mods

Post by TheLame »

Feralidragon wrote:

Code: Select all

If John has homework to do, he must do it first and only then he can go out to play
UnrealScript:

Code: Select all

if (John.hasHomework())
    John.doHomeWork();
John.goOutToPlay();
This little example will actualy not work, let me explain. John has to do his homework before he can go out to play. The way you exexute the script will make him start doing the homework and go out and play in the same tick, which is impossible unless he has no homework to do at all? You should make it something like this:

Code: Select all

if (John.hasHomework())
    John.doHomeWork();
else if (bHomeworkIsDone)
John.goOutToPlay();
You need the check, otherwise you will send John out to play in the same tick as you made him start doing the homework.
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Lesson 0 - The science behind mods

Post by Feralidragon »

Not really, you don't need the check.
The example doesn't reflect ticks, but even if it did the tasks I set were (in a more organized way):
If John has homework to do:
.... he must do it first
he can go out to play


So I make a check if John has homework to do, then if he has, I call a function for him to do the homework.
Now, the function is called doHomework, therefore the next step will only execute after doHomework finishes and never before that, therefore by the time goOutToPlay is executed, doHomework was already finished.

If I was going to consider ticks, analogically the closest example of a tick here would be a day, since a tick executes the same batch of instructions every time it runs (hence a routine as I mentioned in the first tasks example), the same way John has to wake up, eat, do stuff and then sleep everyday.
So even by that logic, John can do the homework and play in the same day (as in the same tick), but again, to do the latter he must do and finish the former first, and since this is a single thread of tasks, 2 or more tasks can never run at the same time.

Therefore, it's irrelevant if John goes out to play in the same tick or in the next, the conditions set were simply: do homework first, and once finished, he can go out to play.
TheLame
Novice
Posts: 26
Joined: Tue Jan 22, 2013 12:37 pm
Personal rank: retired UT reject

Re: Lesson 0 - The science behind mods

Post by TheLame »

lol, you call one function in your first check, then when that check is done you call another function in the same tick, you refer to the example as UnrealScript, this is how UnrealScript will function.

Your example to do homework will give the impression for the reader that work has to be done. No use in giving all your long explanations realy? He will be instructed to go out and play the exact same time as he is instructed to do homework, I hope I'm not the only one to see the conflict in that? Otherwise, good luck in coding :loool: functions does take time regardless of beeing example of "john from real life" or within UnrealScript. Try set a character to do an animation and then have it destroyd after this method, you will NEVER see the animation ... sry.
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Lesson 0 - The science behind mods

Post by Feralidragon »

No, that's not how UnrealScript works, nor any language I know of.
Again, it's not set at the same time. He does one task, then the other once the first is finished. This is how UnrealScript, Java, C++, PHP, Python, any language in the world works.
Plus, UnrealScript (at least in UEngine1.x) is single threaded, therefore there's no way you will have 2 tasks running at the same exact time, which although it seems to do so, in reality it does not (search about "pseudo-parallelism").

By tasks, I mean functions, code running with a start and an end point.

Animation is a really bad example for this because it's an "ongoing task" and even it doesn't run at the same time as other things, also called "latent" with no safe guarantees it will actually end because it depends on something else to be updated, but even animation has internal tasks for it to move each model vertex depending on the tick delta time, and while animation is being processed, nothing else in the game is processed, because again UEngine1.x is single threaded.
Actually, the proper naming for an animation is a "state machine", which is an entire different matter and is outside the scope of what I am trying to show here.

For instance, when you call PlayAnim, you are not executing the animation itself, instead you are setting up the animation state machine switches for it to start working, that's all.
Here John has to do the homework himself, he won't turn switches on and off for something to do it for him, hence he has to finish the homework himself first, and then go out and play, he won't do both at the same time.

Calling exact functions is not the same as an ongoing task, and although calling a function may start one (and this is where you're coming from I believe), I do not state that in my example and that's not the point of it at all.
Just like if you had:

Code: Select all

a = b + c;
d = a - b;
d = a - b won't execute until a = b + c finishes.

Proof of all this is that if you write CPU intensive task like having a million iterations in one actor in each tick, the game will literally stop at that point while it processes all those iterations, and in the meanwhile nothing else in the game will get updated until your task ends.

I hope I was clear this time.
TheLame
Novice
Posts: 26
Joined: Tue Jan 22, 2013 12:37 pm
Personal rank: retired UT reject

Re: Lesson 0 - The science behind mods

Post by TheLame »

dude, I'm impressed how good you are at making long explanations trying to make wrong things right. Let me make a SHORT example that totaly prove you WRONG!

IF what you say is true, then this should not work:

SomeActor.Destroy();
Spawn(class'something',SomeActor.Location);


In your world the SomeActor is destroyd first, then we try to spawn something in it's location, this should fail if you are right, but try it out, and you will find that I'm right.
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

Re: Lesson 0 - The science behind mods

Post by Higor »

TheLame wrote:dude, I'm impressed how good you are at making long explanations trying to make wrong things right. Let me make a SHORT example that totaly prove you WRONG!

IF what you say is true, then this should not work:

SomeActor.Destroy();
Spawn(class'something',SomeActor.Location);


In your world the SomeActor is destroyd first, then we try to spawn something in it's location, this should fail if you are right, but try it out, and you will find that I'm right.

//
// Destroy this actor. Returns true if destroyed, false if indestructable.
// Destruction is latent. It occurs at the end of the tick.
//
native(279) final function bool Destroy();

Following the engine's doc it should be possible to read variables from an actor we just destroyed... but I do remember some problems with that.
Anyone can confirm? (too lazy myself)

EDIT: I think I remember that we can't call functions anymore on a destroyed actor, but still have the doubt with variables.
EDIT2: Well, bDeleteMe can be read for a few sconds in Network Games, I'd guess other vars can too.
I always enforce the rule of clearing the pointer (setting to NONE) after destroying an actor, it's a healthy practice in all coding environments.
EDIT3: Just point things politely, nobody cares if you're right or not.
EDIT4: This is an edit :loool:
User avatar
Sp0ngeb0b
Adept
Posts: 376
Joined: Wed Feb 13, 2008 9:16 pm
Location: Cologne
Contact:

Re: Lesson 0 - The science behind mods

Post by Sp0ngeb0b »

Actually, the destroy() example is unfitting here.
When the Destroy() function [...] returns, the actor object is not actually gone, it just becomes unaccessible by other objects in UnrealScript.
[...]
You can still access the destroyed actor's variables and call its functions, but only directly, not through variables or function return values.
What happens when an Actor is destroyed

Remember that this is a beginner tutorial and what Ferali stated is simply true. Ofcourse, when working with UScript directly there are tons of special cases, but for a learner Ferali's post is more than enough ;)
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Lesson 0 - The science behind mods

Post by Feralidragon »

Ok, you don't seem to understand a single word of what I just posted (tip: search what are state machines, threads, pseudo-parallelism, and the like first please..., because you clearly don't know what the hell I am talking about by seeing the examples you're posting to try to prove me "wrong").

Destroy() doesn't actually "destroy" the actor at all. It calls some events and setups some flags so it doesn't get iterated through, and therefore not updated anymore, however as long as you have direct references to that actor, you can still do some stuff with it since the actor is still intact in RAM.
For instance, in the Canvas, if you are rendering an actor (with DrawActor) and then you destroy said actor, as long as you don't clean the reference, you are still able to render the "destroyed" actor. True story and you can try it yourself.

Remember my "long explanation" above where I mention state machines? Destroy() is also one of those.
TheLame
Novice
Posts: 26
Joined: Tue Jan 22, 2013 12:37 pm
Personal rank: retired UT reject

Re: Lesson 0 - The science behind mods

Post by TheLame »

Ok... this gets out of hands... so you guys are to determine what functions to use or not? ok .. it's the same thing if you destroy (e.g. server kick) a player, he can come back after some time and use the same playername, but if he autoreconnect the nickname will be changed to player by default because his first inctance is still hanging in the game because it takes TIME to remove the player completely.

You all need to rewind back to my very first post, look at the two functions called, they ARE called at the exact same time, remember that uscript is compiled and not interpretet, if you use a playanim this in the first call and use a playanim that in second call then the last call will overrule the first call. THIS IS BASIC!! .. in Feralis "John" example he simply forgot to put in eneough time for him to do the homework, why do you want to take it all this way?? His example will not give the impression on how UnrealScript works UNLESS you know it upfront!! And then what's the point in posting it???
User avatar
Sp0ngeb0b
Adept
Posts: 376
Joined: Wed Feb 13, 2008 9:16 pm
Location: Cologne
Contact:

Re: Lesson 0 - The science behind mods

Post by Sp0ngeb0b »

TheLame wrote:look at the two functions called, they ARE called at the exact same time
You arouse the impression that they are called simultaneously, which isn't true. They are called both in the same tick, okay. But inside the tick, there's an order aswell, and according to the order the first function will run completely and after it has finished the second one gets called. I dont believe I tell you anything new, it's probably just a misunderstanding.
TheLame wrote:in Feralis "John" example he simply forgot to put in eneough time for him to do the homework
Well, this depends how the homework looks like. In general, the doHomeWork() function can take as long as it wants, the tick will just last longer then. But if the homework is stuff which has to be done in more than one tick, we would indeed use your method. But I dont think that's what Ferali meant in his post, since its purpose was to show a non-programmer how the basic things work (like general courses, not even necessarily for UScript)
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Lesson 0 - The science behind mods

Post by Feralidragon »

Last time, I will keep it short and practical:

Code: Select all

log("Some people just don't get it...");
while (true);
log("Do you get it now?");
Try it, and let us know if you get the "Do you get it now?" written to your log after the game crash.
Next, grab yourself a book in how code actually works in pretty much all languages and what "compiled" and "interpreted" actually means.

And btw, UnrealScript is a bit of both, since it's "compiled" into "byte code" but then it's "interpreted" by the UVM, just like Java, that's why UnrealScript is so slow compared to C++.
The only way to get 2 tasks running at the same exact time is by multi-threading, and again: UEngine1 is not multi-threaded, and even if it was, I can honestly say that I already made multi-threaded code on other language (Python) and it's not that straightforward anyway.

All the languages work the same way, everything has an order of execution and one thing doesn't execute before the other unless you create threads to do so.
On a last note: I could make John's doHomework last an hour, and yet during that whole hour goOutAndPlay wouldn't execute at all, and the tick wouldn't finish either until then.

If you keep denying these basic facts of single-threaded programming, this discussion becomes pointless.
TheLame
Novice
Posts: 26
Joined: Tue Jan 22, 2013 12:37 pm
Personal rank: retired UT reject

Re: Lesson 0 - The science behind mods

Post by TheLame »

Yes, the discussion becomes pointless, why? Because you know you're right and I know I'm right :loool:
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Lesson 0 - The science behind mods

Post by Feralidragon »

Just keep in mind that every argument you posted trying to "prove" your point in this topic was debunked and explained (with absolutely no counter from you), not only by me but also by others with sources and examples, and I saw no signs of you trying the code I just posted to check for yourself (perhaps you're too proud to do so?).
Anyway, I am yet to see at least 1 single example that shows that you're right, but I bet I won't see any, because things do not work as you claim, it's that simple.
User avatar
anth
Adept
Posts: 257
Joined: Thu May 13, 2010 2:23 am

Re: Lesson 0 - The science behind mods

Post by anth »

I think TheLame's issue is that if "doing homework" and "going out and play" were animated actions, an external observer would only see john going out and play in real time. This is obvious since animations are rendered in a sequence of Render() calls. But that's not really the point here :) The example isn't supposed to have observable real-time behavior.
Post Reply