Topic: Dynamic and Clean Programming(Tutorial)

Don't you just hate digging through code to fix a bug (same reason I hate digging through my room to find my wallet).

Then you stop and think, ''Wait, if my room were a bit cleaner, perhaps I could find stuff!''

So, to keep my room from getting so messy, I just don't put that much in it! hahaha. It is easy now for me to keep it clean. I can also move it around easier. I have two ottomans, (for clothes) a futon(doubles as a bed), a hamper, a trash can, a small desk, and a lamp.

Hmmm, let's change my room into a program.

Let's see, I need to make a list of things in my room (I will be using lua):

CREATING THE VARIABLES
--VARIABLES
ottoman_clothes =
ottoman_pants =

futon =
hamper =
trash_can =
desk =
lamp =

I use the Maratis Game Engine. In Maratis, the way you set a variable to a game object is this way:

lamp = getObject("lamp")

Simple enough huh? So I will do that for all the variables:

--VARIABLES
ottoman_clothes = getObject("ottomanclothes")
ottoman_pants = getObject("ottomanpants")

futon = getObject("futon")
hamper = getObject("hamper")
trash_can = getObject("trashcan")
desk = getObject("desk")
lamp = getObject("lamp")

As you might have noticed, the 3D object's name is ottomanclothes, but the variable name is ottoman_clothes. I did that because it is easier to spot ottoman_clothes than it is to spot ottomanclothes in the program.

So, anything we do to the ottoman_clothes variable in the script, directly affects the ottomanclothes 3D object. Different game engines use different functions to get 3D objects, and different programming languages have different syntax to create variables, but all you should know is that you create your VARIABLES FIRST.

CREATING THE FUNCTIONS

Functions are like VERBS. And verbs are ACTION WORDS. What type of ACTIONS do ottoman's perform? Or what type of actions can we PERFORM on an ottoman? Well, we can OPEN() an ottoman. We can also CLOSE() a ottoman. So these will be our functions for the ottoman:

--OTTOMAN FUNCTIONS
Open()
Close()

We will do this for all game objects.

--FUTON FUNCTIONS
Unfold()
Fold()
Sit()
LieDown()

--HAMPER FUNCTIONS
Open()
Close()

--TRASH CAN FUNCTIONS
Open()
Close()

--DESK FUNCTIONS

-- What can a desk really do? hehe

--LAMP FUNCTIONS
TurnOn()
TurnOff()
SetColor()
SetBrightness()

As you can see, we can perform similar actions on various objects. So how do we determine whether we are opening a trash can, or opening an ottoman?

ARGUMENTS!

Arguments allow you to SUBSTITUTE variables. We can use it like this:

Open(trash_can)
Open(hamper)
Open(ottoman_clothes)
Open(ottoman_pants)

Close(ottoman_pants)
Close(ottoman_clothes)
Close(hamper)
Close(trash_can)

In the lua programming language the function is created something like this:

function Open(object)
    translate(object,{1,0,0},"local")
end

"object" is not a variable or anything. It is like a place holder. Whatever we put in the parenthesis gets dumped wherever "object" is inside of the function.

It turns out that the only type of data the translate can take in the first slot is a game object. If it required a number, we could only use a varaible that is a number there. So it depends on WHERE you are substituting that determines if the argument you are using is able to be used (read it again slowly).

So when we type:

Open(ottoman_clothes)

This will translate the ottoman_clothes variable (which is getting the 3D object "ottomanclothes") on the x-axis with respect to it's local orientation.

FILLING IN THE FUNCTIONS

So now you have to FILL IN ALL THE FUNCTIONS. And this requires:

-Thinking about what you want to do
-Determining the PROGRAMMING COUNTERPART
-Figuring out how the API does it
-Putting it in the funtion

Say you want to make a character JUMP(), well, the programming counterpart of jumping is basically translating an object while playing an animation.

You would use CONDITIONAL STATEMENTS to determine when the character can jump. You might use LOOPS to determine if any actions should repeat etc. How do you translate an object in Unity? In UDK? in CRY? How do you play an object's animation in these softwares? Which button needs to be pressed to make the JUMP() function work? This all goes in the function. And we make it dynamic with ARGUMENTS.

Here is an example from my CUSTOMIZABLE GAME SCRIPT using the Maratis Game Engine API:

--OBJECTS
box = getObject("box")

--JUMP VARIABLES
jump_object = box
jump_height = 3
jump_speed = 1
jump_btn = "SPACE"
jump_animation = changeAnimation(object, animationId)
jump_direction = {jump_up,jump_down,jump,left,jump_right}
jump_up = vec3(x, y, jump_speed*jump_height) 
jump_down =    vec3(x, y, jump_speed*jump_height) 
jump_left =    vec3(-jump_speed*jump_height, y, z) 
jump_right = vec3(jump_speed*jump_height, y, z) 
collisions = getNumCollisions(jump_object)

--JUMP FUNCTION
function Jump(jump_object,jump_direction,jump_btn,jump_animation)
    if isKeyPressed(jump_btn) then
        changeAnimation(jump_object,jump_animation)
        addCentralForce(jump_object, jump_direction, "local")
    end
end

--MAKE ANY OBJECT JUMP
function onSceneUpdate()
    Jump(box,jump_up,jump_btn,jump_animation)
end

The format of our jump function is:

Jump(object you want to jump,direction you want it to jump,button used to jump,animation to play when button is pressed)

One of the ways you TRANSLATE an object in maratis is with the ADDCENTRALFORCE() function. The ADDCENTRALFORCE() function is formatted like so:

addCentralFoce(object,{x,y,z},"local") 
--don't put "local" if you want it global

Instead of putting the values into {x,y,z} as {0,0,1} we made a variable that is a VECTOR3 variable which is already in the format {x,y,z} and made it an argument. Then we can just change the variable without having to go dig through the code to find the JUMP() function.

As you also might have noticed, I hardly use any real numbers in my function. I did, however, use variables. And I also used them as ARGUMENTS.

The arguments of the JUMP() function are still only PLACE HOLDERS. I named them with the same names as the variables to make it clear what type of variables can go inside each slot.

The CHANGEANIMATION() function behaves in a similar way.

ITS DYNAMIC!

Now that we have a dynamic function (function with arguments), we can make any newly created object jump with a set speed, while playing a set animation.

Jump(man,jump_up,"W",animation_jump1)
Jump(woman,jump_up,"W",animation_jump2)
AND SO ON!

Last edited by Tutorial Doctor (2014-03-08 21:32:44)

Re: Dynamic and Clean Programming(Tutorial)

Very nice Introductory to Programming tutorial but, I would use the BBCode tags to format your tutorial.
Example this code should be in a code tag.

--OBJECTS
box = getObject("box")
--JUMP VARIABLES
jump_object = box
jump_height = 3
jump_speed = 1
jump_btn = "SPACE"
jump_animation = changeAnimation(object, animationId)
jump_direction = {jump_up,jump_down,jump,left,jump_right}
jump_up = vec3(x, y, jump_speed*jump_height) 
jump_down =    vec3(x, y, jump_speed*jump_height) 
jump_left =    vec3(-jump_speed*jump_height, y, z) 
jump_right = vec3(jump_speed*jump_height, y, z) 
collisions = getNumCollisions(box)
--JUMP FUNCTION
function Jump(jump_object,jump_direction,jump_btn,jump_animation)
    if isKeyPressed(jump_btn) then
        changeAnimation(jump_object,jump_animation)
        addCentralForce(jump_object, jump_direction, "local")
    end
end
--MAKE ANY OBJECT JUMP
function onSceneUpdate()
    Jump(box,jump_up,jump_btn,jump_animation)
end

Re: Dynamic and Clean Programming(Tutorial)

THANKS! I needed that! So that is what BBCODE is for? haha I will do that for my customizable game script thread.

Re: Dynamic and Clean Programming(Tutorial)

You have to write it like this:

[ code ] (with no spaces)
put your code here
[ / code ] (with no spaces)