Topic: Code Snippets.

I want to post a collection of code snippets that do useful things you could use in your game. I will try to keep the code short and easy to understand;

Clone an object on another object's location:
function offsetClone(object1,offset,object2)
    if onKeyUp("M") then
        objectPosition = getPosition(object2)
        clonedObject = getClone(object1)
        setPosition(clonedObject,{objectPosition[1] ,objectPosition[2] + offset,objectPosition[3]})
    end
end
How it works:

This function takes three arguments:
-The object whose location you want to use
-The direction along the Y axis you want to offset the clone
-The object you want to clone

Usage:
function onSceneUpdate()
offsetClone(clone,3,object)
end

It can be read:

Offset the clone object three units from another object (along the Y axis)
More functionality:
function offsetClone(object1,offset,object2,axis)
    if onKeyUp("M") then
        objectPosition = getPosition(object2)
        clonedObject = getClone(object1)
        
        x_axis = {objectPosition[1] + offset,objectPosition[2],objectPosition[3]}
        y_axis = {objectPosition[1] ,objectPosition[2] + offset,objectPosition[3]}
        z_axis = {objectPosition[1] ,objectPosition[2],objectPosition[3] + offset}

    
        setPosition(clonedObject,axis)
    end
end
offsetClone(clone,.5,object,x_axis)

It can be read:

Offset the clone object .5 units from another object along the X axis

Last edited by Tutorial Doctor (2014-01-30 01:02:50)

Re: Code Snippets.

Click an object from the view of a Camera:
clicked = false

function Click(objectName,camera)
    --[[----------------------------------------------------------------------UNPROJECTED POINT]]
    mx = getAxis("MOUSE_X")
    my = getAxis("MOUSE_Y")    

    V1 = getUnProjectedPoint(camera, vec3(mx, my, 0))
    V2 = getUnProjectedPoint(camera, vec3(mx, my, 1))
    
    point = rayHit(V1,V2,objectName)  -- note that rayHit only detects objects with physics enabled
    
    if point then 
        Xname = getName(objectName)
        Xobj = getObject(Xname)
        --setText(text,Xname)
        --print(Xname)
        
        if Xname == getName(objectName) and onKeyDown("MOUSE_BUTTON1") then 
            deactivate(Xobj)            
            playSound(sound) 
            clicked = true 
        else 
            clicked = false 
        end
    end
end    

The parameters it takes is the object you want to click, and the camera from which you want to click the object.

Last edited by Tutorial Doctor (2014-04-30 15:53:36)

Re: Code Snippets.

Toggle a Light on and off using a specified key
lightState = true

function ToggleLight(object,key)
    if lightState==false and onKeyDown(key)then
        setLightIntensity(object,1)
        lightState = true
        
    elseif lightState==true and onKeyDown(key)then
        setLightIntensity(object,0)
        lightState = false
    end
end

They "key" parameter must be in double quotes and can use any of the KEY LITERALS here:
http://wiki.maratis3d.org/index.php?title=Lua_scripting

Last edited by Tutorial Doctor (2014-04-30 16:07:02)

Re: Code Snippets.

Automatically type text

: Thanks to Vegas for this one. I just modified it a bit to make it easier to understand.

--time variable
t = 0

--initialize counter variable
count = 0


function TypeText(typedText,delay,usedText)
    setText(usedText, T1text)
    
    --increment timer
    t = t + 1
    
    -- Change 5 to a different value to increase or decrease typing speed. Lower is faster. 5 is the default.
    if t == delay then
        count = count + 1
        t = 0
    end
    
    -- it's that string.sub thing who's writing letter one by one
    for i=1, count do T1text = string.sub (typedText, 1, 0+i) end
    
    -- Stop counting when we are at the end of the line
    if count == string.len(typedText) then t = 0 end
end
Explanation
function TypeText(typedText,delay,usedText)

The typedText parameter should be a variable with a string data type. The speed is actually the "delay", so lower numbers are faster. The usedText parameter is the text object you want to use.

Last edited by Tutorial Doctor (2014-05-02 14:09:34)