3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1] 2

Author Topic: Tutorial for absolute beginner: Start Scripting.  (Read 3578 times)

« on: January 07, 2011, 11:17:41 PM »
CONTENTS

Part 1: Introduction: What does the Script object do ?
Part 2: How the Script object is related to the objects, linked to it ?
Part 3: Understanding the code: float Score (variable)
Part 4: Understanding the code: void Main
Part 5: Understanding the code: IN_22
Part 6: Understanding the code: OUT_0



Part 1: Introduction: What does the Script object do ?

We don't write a new script here, we'll try to understand the one that comes with 3DRad.
So I took as a basis ScoreDemo.3dr sample project, that comes with 3DRad.
It contains a script. If we open the script, we'll see the code:

float Score = 0;
void Main()
{
   if (IN_22 > 0)
   {
      Score = Score + 1;
   }
   OUT_0 = Score;
}


 What does this stuff mean ?
   What is...
    - ... float ?
    - ... Score ?
    - ... void Main() ?
    - ... if ?
    - ... IN_22 ?
    - ... OUT_0 ?
    - ... { } (brackets)?

1) The first thing one need to understand is how the Script object is related to other objects in the project.
Let's click once on the Script object - it must become highlighted with red.
(see 'RelatedObjects' picture below)
We see that if the script is highlighted with red, then the objects, which are related to the script, are checked (EventOnContact and ValuePrint in the project).
So the Script object is like a PC: it gets data from other objects and sends data to them.
(see 'DataInOut' picture below)
(to be continued...Part 2)
« Last Edit: January 28, 2011, 11:51:44 AM by Alec »

jestermon

« Reply #1 on: January 08, 2011, 12:44:44 AM »
stuff
The material out of which something is made or formed; substance.
The essential substance or elements; essence:

staff
A stick or cane carried as an aid in walking or climbing.
A group of assistants to a manager, executive, or other person in authority

I noticed this typo in many of your posts, and let them go with a smile, however for a tutorial, the correct spelling is important
« Reply #2 on: January 08, 2011, 01:36:23 AM »
Yes  :), many thanks for correcting.
I mix them up, though I know their meanings.
Please, correct me if I make a mistake, that can confuse.
« Reply #3 on: January 08, 2011, 01:56:36 AM »
Part 2: How the Script object is related to the objects, linked to it ?

Now, let's go on...
Let's double-click on the Script in order to open Features of the script.
It looks like in the 'ScriptFeatures' picture below.
So, it contains 3 sections(to the left) for displaying links between objects and script:
1) OBJECT HANDLES
2) INPUT
3) OUTPUT

Let's get back to the ScoreDemo.3dr.
If we open we'll see:
1) OBJECT HANDLES
    2 objects linked to the script:
     -OBJ_0 - ValuePrint
     -OBJ_22 - EventOnContact

2) INPUT
    Objects features whose values the Script reads or gets from an object, linked to it:
     -IN_0 corresponds with OBJ_0 (ValuePrint)
     -IN_1 corresponds with OBJ_0 (ValuePrint)
     -IN_22 corresponds with OBJ_22 (EventOnContact)
     -IN_23 corresponds with OBJ_22 (EventOnContact)

3) OUTPUT
    Objects features whose values the Script writes or sends to an object, linked to it:
     -OUT_0 corresponds with OBJ_0 (ValuePrint)
     -OUT_1 corresponds with OBJ_0 (ValuePrint)

To undestand this better, imagine that:
   - the script is like your PC processor (it gets and and sends data);
   - INPUT (or objects, linked to the script) is like a keyboard, which the processor gets data from;
   - OUTPUT (or objects, linked to the script) is like a monitor or printer, which the processor send data to.
_____________________________
A little practice:
1)
With the ScoreDemo.3dr project
    -open the project, click once the Script object (to highlight it with red,
     see the 'RelatedObjects' picture in the Part 1);
    -uncheck the 'EventOnContact' and the 'ValuePrint' objects;
    -double-click the 'Script' object in order to open it;
   Can you see anything in those 3 sections to the left ?
   No, they are empty ! Why ? Because we've disconnected the Script object
    from the 'EventOnContact' and the 'ValuePrint' objects, unchecked them.
   (see 'UncheckedEmpty' picture below)

2)With the ScoreDemo.3dr project
    -open the project, click once the Script object (to highlight it with red,
     see the 'RelatedObjects' picture in the Part 1);
    -uncheck the 'EventOnContact' and the 'ValuePrint' objects;
    -check the 'Car' object and double-click the Script object in order to open it.
   What do you see ? (See the 'ScriptWithCar' picture below)
 a) Which features can the Script have access to and get values from the
     Car object ?
     
  • Speed
  • Maximum steering
  • Steering Angle
  • Brakes State
  • Throttle State

   What opportunities does it open to you to process the Car features ?
     You can get these 5 features from the Car and change or process their 
      values !


 b) Which features can the Script have access to and send values to the
     Car object ?
     They are 15 ! (See OUTPUT Section to the left of the opened Script object)
   What opportunities does it open to you to control the Car?
    Think and guess yourself ! You can have access to these 15 features of
      the Car and change or process their values !


(to be continued... Part 3)
« Last Edit: January 18, 2011, 01:25:52 PM by Alec »
« Reply #4 on: January 08, 2011, 05:16:41 AM »
Starting to look nice!
FPS game creator for 3drad and >2000 games GamesAtNight
« Reply #5 on: January 08, 2011, 05:36:37 AM »
Thanks Alec! :)
new forum search:
http://www.google.com/ site:www.3drad.com searchword  ( <-- copy )
3DRad is now in the same Brain-Area like my old C64 and Amiga Memories!
« Reply #6 on: January 08, 2011, 06:40:19 AM »
Uh oh, the tutorial is catching up with me... I'm gonna have to start paying attention soon.  :P
 
Awesome  8)
3D Rad: The best abandonware ever!
« Reply #7 on: January 08, 2011, 09:52:41 AM »
Thanks, guys for the feedback !

Part 3: Understanding the code: float Score (variable). (line 1)

That's enough to beat about the bush.
So, let's get to the point.
I raised some questions in the part 1 of the tutorial:
What is float, Score, IN_22 and so on ?
Let's get them all sorted out into their proper places.
Open the script of the 'ScoreDemo.3dr'.
1) First little thing we should do: check 'Display line numbers'.
    See the 'LineNumbers' picture below. This is for convenience.

2) Examining line by line:
    Line 1: float Score = 0;
     - Score - it speaks for itself. We use this for determining a score in a game (or the score in this sample project).
        - Score is a variable in the script. Then what is variable ?
        - variable is a value, that can be changed in a game or any programme.
        - But how do we know it is a variable (and not a value, e.g. number that can't be changed) ?
           - because it is marked (or to say correctly, 'declared') with the special
             word float.
           - therefore, float Score = 0; means that we need 
             a  score in our game and it can be changed, because we marked 
             Score with a special word float.
        - And why it is zero ( float Score = 0;) ?
          - If we want an initial score to be zero, we write: float Score = 0;
             - if we want an initial score to be 45, we write: float Score = 45;
             - if we want an initial score to be -45 (minus 45), we write: float 
               Score = -45; and so on.
       - now, close the script with 'OK' button, run the project and look at it 
         from this point of view.
          - every time the car runs into the ball, we see the score is changed on 
            the PC screen
          - and what�s happening right now in the script ? Do you guess ?
             - float Score is being changed every time the car 
               runs into the ball.
               - and it changes only due to a special word float ! (Try removing the
                 word float and run the project. The score won't be changed ! It'll 
                 be zero no matter the car runs into the ball or not !)

(to be continued...)
« Last Edit: January 18, 2011, 01:30:34 PM by Alec »

jestermon

« Reply #8 on: January 08, 2011, 02:02:49 PM »
Excellent work Alec
« Reply #9 on: January 09, 2011, 03:09:58 AM »
Thanks Jestermon.
I believe, my tutorial is just 'to have a snack'  :).
And the book you're writing will be a 'complete meal'  :).
« Reply #10 on: January 09, 2011, 11:47:56 AM »
Part 4: Understanding the code: void Main. (lines 2,3 and 9)

Well, this part of the tutorial is about mysterious void Main ()
with its equally misterious curly brackets { } . (Don't pay attention to
round brackets, that follow right after void Main; this , I think, is
not for absolute beginner).
Let's have a look again at the code in the script of ScoreDemo.3dr.
Open it. Line 2,3 and 9 are the lines, related to void Main ().
(Actually, void Main () is the main function in a script, but for the time being,
let's ignore technical description, which we can find in lots of books, articles etc.).
To understand it better, compare a script with kitchen and void Main with cooking pot  :D.
Have you ever watched how your mother, granny or wife cooked ? (We see the world  has changed and lots of people prefer having a meal in cafe or just fastfood, actually not cooking at home as in past). Still the illustration is clear.
Well, if you watched how she cooked... Normally she cooked it in a pot.
So, our void Main () is like her cooking pot, where our future game or a part of it 'is cooked'.
Thus, like a cook puts ingredients in a pot and cooks, we put 'ingredients': variables, OBJ_0, IN_X, OUT_X etc.) in a script and 3DRad (Anglescript) cooks it.
Then, {} curly brackets of the void Main we may compare with pot brims  :). For those curly brackets
define start and end of your code.
So, if we meet somewhere a code like this:
void Main ()
{

  // do any stuff here...
}
...it means we see an 'empty pot' and an offer: ''// cook something here''.

If, now, we have a code like ours:

float Score = 0;
void Main()
{
   if (IN_22 > 0)
   {
      Score = Score + 1;
   }
   OUT_0 = Score;
}


...our 'pot' (that is void Main) is filled with 'ingredients': if, IN_22, Score, OUT_0 and other pair of curly brackets.
So, the code, that is outside (that is higher or lower the curly brackets of void Main) won't be 'cooked'.
(There are some cases, in which the code still be 'cooked' or processed well even when the code is outside the curly brackets of the void Main function, but this is again not for a beginner).
Now, try putting any line of code ( that is line 4,5,6,7 or 8) outside the brackets  :).
(See the picture 'Outside' below). That is transpose line 4,5,6,7 or 8 to line 10, close the script, run the project and see what happens. The thing that we don't want ! The car runs well and runs into the ball, but the score isn't
changed ! And this is like badly-cooked dish, that you refuse to eat.

(to be continued...)
« Last Edit: January 18, 2011, 01:34:07 PM by Alec »
« Reply #11 on: January 09, 2011, 09:38:50 PM »

,and how about the random between 1 and 4?
need a hug ;D
« Reply #12 on: January 10, 2011, 12:12:21 AM »
Hi memenomentomori.
Quote
,and how about the random between 1 and 4?
What do you mean ?
Different illustrations in part 1 and 4 ? or what ?

jestermon

« Reply #13 on: January 10, 2011, 12:18:48 AM »
float myRandomNumber = iFloatRand(1,4) ;
« Reply #14 on: January 10, 2011, 04:22:56 AM »
Part 5: Understanding the code: IN_22 (lines 4 to 7)

Well, what do lines 4 to 7 mean ?
Line 4 begins with a special word if.
And this word if speaks for itself.
(If I go shopping, I'll buy some food. And if I don't, I'll be hungry.)  :)
IN_22 is a value, which the script gets from the EventOnContact object that linked with the script in this project.
And this value is Contact Force.It also can change.
When the car in the project simply runs, not running into the ball, IN_22 = 0,
that is contact force is zero and you see on the screen "0".
But if the car runs into the ball, IN_22 (Contact Force) is changed and becomes more than "0", so lines 5 to 7 start working ! And if the car doesn't run into the ball, IN_22 remains "0" and can't change the Score.
Lines 5 and 7 contain curly brackets. But these curly brackets are related to if and not to 'void Main' as we saw before. (See the 'CurlyBracketsIf' picture).

And next, Line 6 is easy to understand.

Score = Score + 1;
I started speaking about it in part 3 of the tutorial. Now, line 1 and line 6 are related. Remember that Score is marked with a special word float in line 1, therefore we can change it as we want. So, in line 6 sounds an order:
'' Change the score !".(But only if IN_22 > 0). The score changes, increasing by 1, because we ordered: Score = Score + 1.

Let's 'translate' Anglescript code into a human language (e.g. English):  :)

if (the car runs into the ball and the force, with which it runs into
    is more than 0, than...
happens the things we have between next 2 
    curly brackets, namely:
    {
       score changes, increasing by 1
       }



__________________
One more illustration:
if (I push the door)
   {
      the door opens
   }


(to be continued... Part 6)
« Last Edit: January 18, 2011, 01:37:16 PM by Alec »
Pages: [1] 2