Tutorials

 Reply to this postStart new topic

[TUT]Logging Changes

Deji
post May 12 2010, 02:49 AM
Post #1


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



Logging All Changes


This is a basic tutorial teaching how to use wait 0 in order to log any change made to anything we can read.

For example, we want to calculate how many bullets a player fires in 15 seconds as well as giving visual indication each time a single bullet is fired.

How do we do that? Well, there is the "Bullets Fired" stat, which gives us the total amount of bullets fired since the start of the game, but we only need the bullets fired in the last 15 seconds.

SANNY
:Thread
0652: 0@ = integer_stat 126 // Get the total amount of bullets fired in the game.
wait 15000                  // Wait 15 seconds.
0652: 1@ = integer_stat 126 // Get the total amount of bullets fired in the game.
1@ -= 0@                    // Calculate the difference between the stat now and 15 seconds ago.

// 1@ now contains the bullets fired in the last 15 seconds.

{

If the stat was originally 200 and 15 seconds later the stat is 240, 40 bullets have been fired in 15 seconds.

200 - 240 = 40. Simple enough, eh?

}



Pretty simple, however we want to display indication for each hit. This means we need the code in a constant loop until 15 seconds has passed.

With the code above, we got the difference between the bullets fired now and the amount fired 15 seconds ago. Calculated it and got the amount of bullets fired during those 15 seconds in return. The next step is pretty much the same concept.


Let's see this first:

SANNY
:Loop
0652: 0@ = integer_stat 126
wait 0
0652: 1@ = integer_stat 126
1@ -= 0@
// 1@ will probably only be 1 or 0. Indicating that either a bullet was just fired or no bullets were just fired.
jump @Loop


People may get confused with the wait 0 since it seems like there is no wait at all, but really a lot can happen while the wait is in action... Well, not really. But a lot can happen if the wait is run over and over again. Another big mistake is the belief that wait has to occur before anything else. It can in fact be anywhere inside the loop, and only needs to be in a loop.


Yeah, a wait 0 is virtually "no time at all". So what's to say the player didn't shoot a bullet before or after the wait?

The wait is the only place the code sits to have a cup of tea and watch what happens to the rest of the game... The rest of the time it is doing everything instantly. That's why a wait is always required in a loop. While the wait is in action, CJ has probably not fired any bullets. But after a million or so loops, CJ may have fired a bullet... And the only space for him to have fired the bullet is during the wait.


So that's basically it... A wait is just the code letting everything else happen after it finishes it's code.

The code could also be written like this:

SANNY
:Loop
wait 0
0652: 1@ = integer_stat 126
1@ -= 0@
0652: 0@ = integer_stat 126
jump @Loop


However the first time this code is run, it will return the amount of bullets fired since the start of the game, when usually it would return 0.

It would usually return 0 because the chances of CJ firing a weapon at that exact moment the code first runs is are very low. And the math line is pointless because 0@ does not have a value yet.


And we definitely shouldn't do this:

SANNY
:Loop
wait 0
0652: 0@ = integer_stat 126
0652: 1@ = integer_stat 126
1@ -= 0@
jump @Loop


Here we will always get 0. There was no wait between stat retrieval so it just got the same number twice and subtracted from itself... Which will always make 0 the return.



Back to coding the actual script, now you should have a better understanding of why wait's are important and such... We can make our cool "bullet logger"... Or somethin.


SANNY
{$CLEO}

0000:

:Loop
0652: 0@ = integer_stat 126
wait 0
0652: 1@ = integer_stat 126
1@ -= 0@
if
   1@ > 0
then
   00BC: show_text_highpriority GXT 'BULLET' time 1000 flag 1  // BULLET
end
jump @Loop


Every time a bullet is fired, the game will display the text "BULLET". Try it.

The 1@ > 0 just checks if the bullets fired sum returns 1, meaning yes.. A bullet has been fired. It will never be higher because there is not enough time in the wait to fire more bullets. But it is coders tradition to simply check if it's over 0, not exactly 1... For some reason.


Now we add the time limit and final bullet count:

SANNY
2@ = 0
32@ = 0

:Loop
0652: 0@ = integer_stat 126
wait 0
0652: 1@ = integer_stat 126
1@ -= 0@
if
   1@ > 0
then
   000A: 2@ += 1@  // Add that bullet to the total amount of bullets.
   00BC: show_text_highpriority GXT 'BULLET' time 1000 flag 1  // BULLET
end
if
   32@ > 15000  // if 15,000 milleseconds (15 seconds) has passed.
else_jump @Loop
01E5: show_text_1number_highpriority GXT 'KICK1_9' number 2@ time 5000 flag 1  // SCORE ~1~


Now after 15 seconds of bullet fun has passed... The total amount of bullets fired will show.

If 23 bullets have been fired, for example, it would display "SCORE 23".


Amazing, eh? Check out all the other stats, we can check some pretty amazing things.


--------------------
Go to the top of the page
 
+Quote Post
Huckleberry Pie
post Jul 29 2010, 01:49 PM
Post #2


Ameteur Member

Posts: 49
Joined: 5-August 09



Sounds useful. Will it be possible to use this for determining if I've killed a certain amount of pedestrians, or if I've done a combo/kill streak, and then play a sound or display something afterwards?
Go to the top of the page
 
+Quote Post
Deji
post Jul 29 2010, 11:10 PM
Post #3


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



Yeah, but with ped kills, you can use something like this...

CODE
0297: reset_player $PLAYER_CHAR destroyed_model_counters
wait 5000
0806: get_player $PLAYER_CHAR kills_from_last_checkpoint 0@


0@ will contain the people killed in the last 5 seconds (since the last model counter reset).


--------------------
Go to the top of the page
 
+Quote Post
Reply to this postStart new topic

2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members: