dim3 Forum

Full Version: I need help with writing code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need some help using code but i searched on google and all i got was tips on how to buy some book anyway if there is anyone who will help me i'd really appriciate( if thats how you spell it) it.
welcome to the dim3 forum Johnx3 Smile.

dim3 uses java script, it works like 'if this happens do this'.
Code:
//this is a comment after the double slash, comments do nothing
var number; //this creates a variable called number

function construct(obj) //this is a function, whenever you write construct(obj); it will do the follwowing script until the end bracket of the function
{ //this says that the function has started
number=3 //this makes number = 3

if (number==3) {  //this says that if number = 3 then do the following
//code goes here for what should be done
} //end if, this is the closing bracket of the code

if (number!=3) { //this means if number does not = 3 then do the following
//foo
} //closing bracket
} //this bracket shows the end of the function


thats the main part of java script.
functions.

if you want the object to do somthing specail, like an animation you enter a automatic function which is just a command like
Code:
obj.motionVector.turnToPlayer();

to find those type things, go to the documentation folder, open index, and click on scripting Wink.

also, at the end of sccripts, you may see somthing like
Code:
//
// events
//

function event(obj,mainEvent,subEvent,id,tick)
{
    switch (mainEvent) {
    
        case DIM3_EVENT_CONSTRUCT:
            fyConstruct(obj);
            return;
    
        case DIM3_EVENT_SPAWN:
            fySpawn(obj);
            return;

        case DIM3_EVENT_DIE:
            fyDie(obj);
            return;

        case DIM3_EVENT_FALL:
            fyFall(obj);
            return;

        case DIM3_EVENT_LAND:
            fyHeightDeath(obj);
            return;

        case DIM3_EVENT_COLLIDE:
            fyCollide(obj);
            return;

        case DIM3_EVENT_DAMAGE:
            fyDamage(obj);
            return;


            
    }
}

here are comments on what that means:
Code:
        case DIM3_EVENT_DAMAGE: //if the object gets damaged
            fyDamage(obj); //go to the fyDamage function
            return; //end

and in the beginning you might see
Code:
    obj.model.on=true;
    obj.model.name='FY Trooper';
    obj.model.lit=DIM3_MODEL_LIT_VERTEX;
    obj.model.shadow.on=true;


    obj.setting.openDoors=false;
    obj.setting.damage=true;
    obj.setting.ignorePickUpItems=true;        // don't pickup the weapons/ammo
    obj.setting.hitBox=true;
    
    obj.size.x=1768;
    obj.size.z=875;
    obj.size.y=3754;
    obj.size.weight=230;
    
    obj.health.maximum=15;
    obj.health.start=15;
    obj.setting.pushable=true;
    obj.health.fallDamageMinimumHeight=2000;
    obj.health.fallDamageFactor=1;
    
        // setup weapon
        
    obj.weapon.add('HL Launcher');
    obj.weapon.add('HL Launcher alt');
these are varios settings Smile.




Wink
(its not really to hard)
Also try http://w3schools.com/js/default.asp
It gives you a basic understanding of javascript.
John, I'm writing your tutorial as we speak :-P
It's not going to be finished until Wednesday or Thrusday though, but it will be good. ;-P
A quick overview of how Dim3 uses functions user-created functions to 'do' things.

You have your main event function, which basically tells the engine what functions to execute and when. If you look at the simplist of these scripts, the plain object, you can see:

Code:
function event(obj,mainEvent,subEvent,id,tick)
{
    switch (mainEvent) {
    
        case DIM3_EVENT_CONSTRUCT:
            treeConstruct(obj);
            return;
    
        case DIM3_EVENT_SPAWN:
            treeSpawn(obj);
            return;
            
    }
}

Now let's break this up. First we've got the actual function:

Code:
function event(obj,mainEvent,subEvent,id,tick)
{
//Currently there is nothing in this function.
}

As you can see above, we've added the variables obj, mainEvent, subEvent, id, and tick to this function. This allows the function to access these variables inside its self. The engine dumps information to these variables constantly and can be accessed this way. For example, the mainEvent variable will contain a different value depending on other events which happen. One event it will return is "DIM3_EVENT_CONSTRUCT". This is where the next part comes into play. We will now add something to the function:

Code:
function event(obj,mainEvent,subEvent,id,tick)
{
    switch (mainEvent) {
    
        case DIM3_EVENT_CONSTRUCT:
            treeConstruct(obj); //Executes this
            return;      //And this etc
    
        case DIM3_EVENT_SPAWN:
            treeSpawn(obj);
            return;
            
    }
}

This brings us right back up to the top. This function and its contents are necessary for your script to work properly. The switch() function allows you create a list of different cases which javascript will go through and if one of the cases matches what the engine dumps to the variable, it will execute all functions and routines inside the case. Also, as you can see above, we've put the mainEvent inside the switch() function so that it sorts through the mainEvent. if there is no matching value, it is ignored. You can also set a default action to happen. This will not be necessary for you to use probably about 90% of the time, however there are times when it's useful. To do so, simply add:

Code:
default:
//functions, etc here
return;

If you notice after each case and the default we put "return;". You can also you "break;", though I'm not sure what the difference is. (Brian, I'm sure you know. Could you help us out?)

Now let's look at what happens if the mainEvent matches DIM3_EVENT_CONSTRUCT.

First off, there is only one thing inside this case to do: treeConstruct(obj);. (Remember, every time you use one of the "core-defined" variables such as 'obj' you will need to make sure in the top of the function it says 'obj', like so: function event(obj,mainEvent,subEvent,id,tick). This applies to the others too. You'll find a full list in the documentation.) So this means that it will execute the treeConstruct(); function. let's take a look:

Code:
function treeConstruct(obj)
{
    obj.model.on=true; //Makes the model visible
    obj.model.name="Tree"; //Assigns the physical model to show -- make sure this model exists!
    obj.model.lit=DIM3_MODEL_LIT_FLAT; //Assigns the method the engine uses to light the model
    obj.model.shadow.on=true; //Tells whether the model has a shadow or not

    obj.setting.suspend=true;
}

Look at the variables being assigned in this function. They all contain 'obj' in them. This means you will need the 'obj' in the function name:

Code:
function treeConstruct(obj)

Again, you can find a full list of these variables (which affect routines inside the core its self) in the documentation.

The same applies with the treeSpawn(obj) function:

Code:
function treeSpawn(obj)
{
    obj.size.x=3000;                // force the tree to be smaller, need to do it here after model has been loaded
    obj.size.z=3000;
    obj.size.y=10000;
    
    obj.model.animation.start("Idle"); //Tells the model to play its animation "Idle" which is assigned in the animator
}

If you can understand this, then you're pretty much good-to-go. The rest can be learned by looking at example scripts and reading the documentation. This took me a while to get used to myself, but it's easy once you get used to it. Smile
Note the above is:

Code:
Data/Scripts/Objects/Tree.js
Reference URL's