Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Basic GSC Coding: Making your third code
#1
Hello

This is the third tutorial about making your first codes. In this one I am going to explain how to precache something, how to make a distance monitor and how to set dvars. Lets start with what well add in Init(), everything that needs to be precached should go in Init().

According to the previous tutorials:

Code:
init()
{
    level thread onPlayerConnect();
}

Lets add our precached model and a dvar to get hardcore mode on:

Code:
init()
{
    setDvar( "g_hardcore", 1 ); //this sets the g_hardcore dvar (is the one which enables or disables hardcore mode) to 1 (means that hardcore is on), in some games likes Black Ops the 1 needs to go beetween " " or it will give an error
    precacheModel( "soccer_ball" ); //precache a model like this (by adding its name beetween " "), I have used this model, its Favelas soccer ball. There is another tutorial on this section about getting model names.
    level thread onPlayerConnect();
}

Now lets make that when a player joins the lobby/server he will say on chat something, to do this, we will need to add our code to onplayerconnect:

Code:
onPlayerConnect()
{
    for( ; ; )
    {
        level waittill( "connected", player );
        player sayAll( "Hello, my name is: " + player.name ); //it will say on chat to all the players: Hello, my name is: WHATEVER, I have put the part which will never changed beetween " " and after it I add it the players name, so the only thing that will change in the text will be the players name (which is a variable)
        player thread onPlayerSpawned();
    }
}

Now lets make the main script of our mod: the distance monitor, it consists in a counter that counts the number of metres you have moved, to make that well make a HUD element to show the number of metres and well put our code inside a "while" to keep it update:

Code:
DistanceMonitor()
{
    self endon( "death" );
    self endon( "disconnect" ); //these 2 endons to end the while/loop on death or disconnect
    self.prevorigin = undefined; //we must define the variable to avoid a unitialized variable error once the "while" is played
    self.distancetotal = 0; //we must also make another variable with the distance we have moved, in a begin the distance we have moved should be 0 (because we havent moved yet)
    self.text = self createFontString( "default", 1.5 ); //this is a way to create a HUD text. "default" is the font which I am using (there are more, but this one is my favourite), "1.5" is the scale of the text.
    self.text setPoint( " ", " ", 0, -200 ); //set the position of the text, in this case: x = 0 and y = -200 (up and centered in screen)
    self.text.foreground = true; //this means that the text will be on the "background" of HUD, any text that well put on its same position, will be on top of it
    self.text setText( "Distance Moved: " + self.distancetotal ); //set the text, in a begin it will say: Distance Moved: 0, later the 0 will change to the ammount of inches
    self.text.glowalpha = 1; //to allow glow, this has to be in 1
    self.text.glowcolor = ( 1, 0, 0 ); //red glow, is just to make it nice
    self.text.owner = self; //one thing to destroy the text later
    self.text thread DestroyOnEvents( "death", "disconnect" ); //I have made this custom thread, it will destroy the text on owners death or on owners disconnect
    while( 1 )
    {
        self.prevorigin = self.origin; //we now set the variable we defined before and we give it the value of out position, which is like this ( x position, y position, z position )
        wait ( 0.05 ); //a small wait (its needed)
        self.distancetotal += distance( self.origin, self.prevorigin ); //to the distancetotal (which was 0 in a begin) it will add the distance beetween the previous position (self.prevorigin) and our new position
        self.text setText( "Distance Moved: " + self.distancetotal ); //we set the text like before, it will update the distance we have moved
    }    
}

DestroyOnEvents( event1, event2 )
{
    self.owner waittill_any( event1, event2 ); //when the owner has the first or second event it plays the following code
    if( isdefined( self ) ) //not necesary, just to make sure we are destroying something
        self destroy(); //you destroy HUD elements like this
}

Lets improve it, because "setText" command will give you a "string overflow" crash/error when you have updated your text 511 times, is very annoying, so, since is just a number we can use another command: "setValue", which wont make the game crash after those updates. The problem of setValue is that it only accepts numbers, it wont let you add text, if you wont to have both toghether, create a text on the left of the value and the problem will be solved. Also previously the distance was being measured in Inches (game distance unit), so I created a function that converts inches to metres. You can get on any conversor how many inches is one metre and any other.

Code:
DistanceMonitor()
{
    self endon( "death" );
    self endon( "disconnect" );
    self.prevorigin = undefined;
    self.distancetotal = 0;
    self.text = self createFontString( "default", 1.5 );
    self.text setPoint( "", "", 0, -200 );
    self.text.foreground = true;
    self.text setValue( self.distancetotal ); //changed with setValue
    self.text.glowalpha = 1;
    self.text.glowcolor = ( 1, 0, 0 );
    self.text.owner = self;
    self.text thread DestroyOnEvents( "death", "disconnect" );
    while( 1 )
    {
        self.prevorigin = self.origin;
        wait ( 0.05 );
        self.distancetotal += distance( self.origin, self.prevorigin );
        self.text setValue( InchesToMetres( self.distancetotal ) ); //changed with setValue and added there the converter function
    }    
}

InchesToMetres( ammount ) //the converter function
{
    return ( ammount * 0.0254 ); //it will return the ammount of inches you give to this thread multiplied by 0.0254. The result of the multiplication is the metres you wanted to know.
}

DestroyOnEvents( event1, event2 )
{
    self.owner waittill_any( event1, event2 );
    if( isdefined( self ) )
        self destroy();
}

All toghether:

Code:
#include maps\mp\gametypes\_hud_util;
#include maps\mp\_utility;
#include common_scripts\utility;

init()
{
    setDvar( "g_hardcore", 1 );
    precacheModel( "soccer_ball" );
    level thread onPlayerConnect();
}

onPlayerConnect()
{
    for( ; ; )
    {
        level waittill( "connected", player );
        player sayAll( "Hello, my name is: " + player.name );
        player thread onPlayerSpawned();
    }
}

onPlayerSpawned()
{
    self endon( "disconnect" );
    for( ; ; )
    {
        self waittill( "spawned_player" );
        self thread DistanceMonitor();
    }
}

DistanceMonitor()
{
    self endon( "death" );
    self endon( "disconnect" );
    self.prevorigin = undefined;
    self.distancetotal = 0;
    self.text = self createFontString( "default", 1.5 );
    self.text setPoint( "", "", 0, -200 );
    self.text.foreground = true;
    self.text setText( self.distancetotal );
    self.text.glowalpha = 1;
    self.text.glowcolor = ( 1, 0, 0 );
    self.text.owner = self;
    self.text thread DestroyOnEvents( "death", "disconnect" );
    while( 1 )
    {
        self.prevorigin = self.origin;
        wait ( 0.05 );
        self.distancetotal += distance( self.origin, self.prevorigin );
        self.text setValue( InchesToMetres( self.distancetotal ) );
    }    
}

InchesToMetres( ammount )
{
    return ( ammount * 0.0254 );
}

DestroyOnEvents( event1, event2 )
{
    self.owner waittill_any( event1, event2 );
    if( isdefined( self ) )
        self destroy();
}

Any problems or help, just ask here or in the modding help section. If you want to suggest or criticize something, you are free to do it.

Thanks, @Yamato
Reply

#2
yamatozation modding era
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply

#3
(04-20-2012, 18:37)Yamato Wrote: setDvar( "g_hardcore", 1 );

[Image: misc-so-hardcore-l.png]
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Mod CheeseToast 10 7,231 11-02-2013, 18:02
Last Post: Yamato
  Help Code color crosshairs koren30 3 4,016 10-02-2013, 19:26
Last Post: koren30
  Help need help?how to make plugins code hXnarutoXone 12 8,549 09-01-2013, 18:30
Last Post: Bandarigoda123
  Making an apocalyptic themed Minecraft map AZUMIKKEL 54 29,124 08-31-2013, 02:54
Last Post: AZUMIKKEL
  Help Need Help with C# code tubwux 2 3,313 08-27-2013, 18:18
Last Post: tubwux
  [Request] Compile this code please dozsa0 4 4,133 08-10-2013, 21:02
Last Post: Nukem
  Compile this code please First_Semyon 12 9,683 08-08-2013, 14:53
Last Post: Bandarigoda123
  Compile please this code First_Semyon 8 5,639 07-28-2013, 01:52
Last Post: First_Semyon
  Code of vector Bloodfocus 1 2,411 06-23-2013, 11:54
Last Post: Yamato
  Making a Heli's angles match my angles? akillj 1 2,725 06-16-2013, 15:01
Last Post: Yamato

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.