[LUA] How make pumping ?

recreating units
Post Reply
D.Durand
Posts: 87
Joined: Sun Feb 07, 2010 11:21 am

[LUA] How make pumping ?

Post by D.Durand » Sun Sep 12, 2010 6:50 am

Hi,

How do i make pumping with lua ? I mean, an element go in a direction, then in reverse, then reverse again, etc.

I found something in a TA unit (a T3 mexe), but i don't understand how to do the same thing in lua.

User avatar
zuzuf
Administrateur - Site Admin
Posts: 3281
Joined: Mon Oct 30, 2006 8:49 pm
Location: Toulouse, France
Contact:

Re: [LUA] How make pumping ?

Post by zuzuf » Sun Sep 12, 2010 9:36 am

you need to start a script (which will then run in parallel to the function that started it) with a loop containing one cycle of the pumping animation which will be repeated until the loop ends.

Something like:

Code: Select all

local ANIM_SPEED = 0.2
__this.PumpingAnimation = function(this)
    while true do
        this:move( this.piston, y_axis, 0, 1)
        this:sleep( ANIM_SPEED )
        this:move( this.piston, y_axis, 10, 1)
        -- required to avoid the engine being stuck in a script
        this:sleep( ANIM_SPEED )
    end
end
you could then do "this:start_script(this.PumpingAnimation, this)" in the Create function to have the unit pumping as soon as it's created (you may also want to delay the animation until build is complete). With this code it'll pump until its death but you can replace the "true" condition with a variable or something else to allow stopping/starting the animation with some event.
=>;-D Penguin Powered

D.Durand
Posts: 87
Joined: Sun Feb 07, 2010 11:21 am

Re: [LUA] How make pumping ?

Post by D.Durand » Sun Sep 12, 2010 2:43 pm

Thank you. I will try that.

D.Durand
Posts: 87
Joined: Sun Feb 07, 2010 11:21 am

Re: [LUA] How make pumping ?

Post by D.Durand » Mon Sep 13, 2010 3:26 pm

Well, i tried to add that to my unit script, but how do i call the function, particularly by adding the name of the element i want to make "pumping" ?

User avatar
zuzuf
Administrateur - Site Admin
Posts: 3281
Joined: Mon Oct 30, 2006 8:49 pm
Location: Toulouse, France
Contact:

Re: [LUA] How make pumping ?

Post by zuzuf » Mon Sep 13, 2010 3:58 pm

Just use the start_script function which accepts as many parameters as you want:

Code: Select all

__this.my_function = function(this, parameter1, parameter2)
end

__this.Create = function(this)
    this:start_script(this.my_function, this, this.head, this.lleg)
end
Since you can manipulate only variables in Lua, the model pieces are just variables containing the piece's ID so you can copy it into another variable, change it (not recommended), use it as a return value for a function, etc...
=>;-D Penguin Powered

D.Durand
Posts: 87
Joined: Sun Feb 07, 2010 11:21 am

Re: [LUA] How make pumping ?

Post by D.Durand » Mon Sep 13, 2010 5:19 pm

Thanks, i will try that.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests