LUA noob question. How do you create a simple delay with LUA....like the delay node in script canvas does

0
asked 6 years ago298 views
20 Answers
0
Accepted Answer

I'm not sure if this is what you are trying to do. I have not tested this code out but it delays the thrust after the animation is played based on the amount of time you put in your Delay Property.

I pray it works.

REMOVEDUPLOAD

answered 6 years ago
0

local movement =
{
Properties =
{
jmp = {default = 500.0},
cam_z = {default = EntityId()},
cam_y = {default = EntityId()},
is_moving = false,
in_air = false,
timer = {default = .5, description = "timer for jump"},
has_waited = false,
walking_bw = false,
roll = false,
fight = false,
is_jumping = false,
}
}
function movement:OnActivate()
self.tickBusHandler = TickBus.Connect(self)
self.timer = 0 --self.Properties.timer
self.fbBusId = InputEventNotificationId("fb");
self.fbBus = InputEventNotificationBus.Connect(self, self.fbBusId);
self.lrBusId = InputEventNotificationId("lr"); self.lrBus = InputEventNotificationBus.Connect(self, self.lrBusId);
self.mouse_x_bus_id = InputEventNotificationId("mouse_x");
self.mouse_x_bus = InputEventNotificationBus.Connect(self, self.mouse_x_bus_id);
self.mouse_y_bus_id = InputEventNotificationId("mouse_y"); self.mouse_y_bus = InputEventNotificationBus.Connect(self, self.mouse_y_bus_id);
self.jump_bus_id = InputEventNotificationId("jump");
self.jump_bus = InputEventNotificationBus.Connect(self, self.jump_bus_id);
self.rollBusId = InputEventNotificationId("roll");
self.rollBus = InputEventNotificationBus.Connect(self, self.rollBusId);
self.fightBusId = InputEventNotificationId("fight");
self.fightBus = InputEventNotificationBus.Connect(self, self.fightBusId);
end
function movement:OnHeld(floatValue)
local current_bus = InputEventNotificationBus.GetCurrentBusId()
if ((current_bus == self.fbBusId) and (floatValue < 0)) then
walking_bw = true
else
if ((current_bus == self.fbBusId) and (floatValue > 0)) then
walking_bw = false
end
end if current_bus == self.fbBusId then is_moving = true
AnimGraphComponentRequestBus.Event.SetNamedParameterFloat(self.entityId, "speed", floatValue); end
if ((is_moving == true) and (current_bus == self.mouse_x_bus_id)) then
TransformBus.Event.RotateAroundLocalZ(self.entityId, -floatValue * .002);
else
if ((is_moving == false) and (current_bus == self.mouse_x_bus_id)) then
TransformBus.Event.RotateAroundLocalZ(self.Properties.cam_z, -floatValue * .002);
end
end
if current_bus == self.mouse_y_bus_id then
TransformBus.Event.RotateAroundLocalX(self.Properties.cam_y, -floatValue * .002);
end
if ((walking_bw == true) and (current_bus == self.lrBusId)) then
AnimGraphComponentRequestBus.Event.SetNamedParameterFloat(self.entityId, "direction", -floatValue);
else
if ((walking_bw == false) and (current_bus == self.lrBusId)) then
AnimGraphComponentRequestBus.Event.SetNamedParameterFloat(self.entityId, "direction", floatValue);
end
end
end
function movement:OnPressed(floatValue)
local current_bus = InputEventNotificationBus.GetCurrentBusId()
if current_bus == self.jump_bus_id then
self.timer = self.Properties.timer
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "jump", true)
if (self.Properties.has_waited == true) then
PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, Vector3(0,0,self.Properties.jmp)); -------------------------------------------------------------------------------
self.Properties.has_waited = false
end
end
if current_bus == self.rollBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "roll", true)
end
if current_bus == self.fightBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "fight", true)
end
end
function movement:OnReleased(floatValue)
local current_bus = InputEventNotificationBus.GetCurrentBusId()
if current_bus == self.fbBusId then
is_moving = false;
end
if current_bus == self.fbBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterFloat(self.entityId, "speed", floatValue);
end
if current_bus == self.lrBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterFloat(self.entityId, "direction", floatValue);
end
if current_bus == self.jump_bus_id then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "jump", false);
end
if current_bus == self.rollBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "roll", false);
end
if current_bus == self.fightBusId then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "fight", false);
end
end
function movement:OnTick(deltaTime)
local cam_y_loc = TransformBus.Event.GetLocalY(self.Properties.cam_y)
local cam_z_rot = TransformBus.Event.GetLocalRotation(self.Properties.cam_z)
local player_rot = TransformBus.Event.GetWorldRotation(self.entityId)
local pi = 3.14
----auto camera zoom if cam_y_loc > -1 then cam_y_loc = -1 -- clamp
end
if cam_y_loc < -3 then cam_y_loc = -3 --clamp
end
if is_moving == true then
TransformBus.Event.SetLocalY(self.Properties.cam_y, cam_y_loc -.01) --zoom out
end
if is_moving == false then
TransformBus.Event.SetLocalY(self.Properties.cam_y, cam_y_loc +.02) --zoom in
end
----auto camera correct
if is_moving == true and cam_z_rot.z > pi and cam_z_rot.z < 6.25 then TransformBus.Event.RotateAroundLocalZ(self.Properties.cam_z,.02)
end
if is_moving == true and cam_z_rot.z < pi and cam_z_rot.z > .03 then
TransformBus.Event.RotateAroundLocalZ(self.Properties.cam_z,-.02)
Debug.Log("things" ..tostring(cam_z_rot)) end
----in air
local config = RayCastConfiguration()
local worldPosition = TransformBus.Event.GetWorldTM(self.entityId)
config.origin = worldPosition:GetTranslation()
config.direction = Vector3(0.0, 0.0,-1.0);
local result = PhysicsSystemRequestBus.Broadcast.RayCast(config)
local count = result:GetHitCount()
local hit = result:GetHit(1) --object hit --future use
local dist = (result:GetHit(1).position.z - config.origin.z) *-1 --distance from ground
Debug.Log(tostring(self.Properties.has_waited))
Debug.Log(tostring(self.timer))
if dist > 1 then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "in_air", true) else
if dist < 1 then
AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "in_air", false)
end
end
----jump timer if self.timer > 0.0 then
self.timer = self.timer - deltaTime
else
--	if (self.timer < .1) and (self.timer > 0) then
self.Properties.has_waited = true
end
function movement:OnDeactivate()
self.fbBus:Disconnect()
self.lrBus:Disconnect()
self.tickBusHandler:Disconnect()
self.jump_bus:Disconnect()
end
end
return movement;
answered 6 years ago
0

I did see that last night, but couldn't seem to get it to work in my game...big time noob here.

answered 6 years ago
0

I saw that last night but couldn't seem to get it to work.

Here is where I need a delay.

function movement:OnPressed(floatValue)

local current_bus = InputEventNotificationBus.GetCurrentBusId()

if current_bus == self.jump_bus_id then

AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "jump", floatValue)

****need delay here because impulse activates before first jump animation causing the player to start jump in mid-air. ****

PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, Vector3(0,0,self.Properties.jmp));

end

end

answered 6 years ago
0

I appreciate all the info. I have been using script canvas with a lot of success, but hitting road blocks on more advanced aspects...such as trying to make building system for a survival game I am working on... mainly just getting the building materials to snap together. I will be tackling inventory after that.

answered 6 years ago
0

ok

answered 6 years ago
0

Ok im not at home right now so kind of hard to write the code. Ill right it out for you when I get Home but. That method is good you just need to add a new Property in your Properties maybe call it hasWaited = true, and in your OnTick() function were in the script under the else statement remove the missal code. Write and set self.Properties.hasWaited = true . Then in your OnPressed() function put all your input stuff for your jump button in an if statment and this is what it should say. If(self.Properties.hasWaited == true)then

self.Properties.hasWaited = false

Your input stuff here for jump <<

end

And it should delay the amount of time you set you wait property as like in the example link i showed you.

I hope this helps. :D

answered 6 years ago
0

After reviewing the code above, it looks like the if..then structure where you set self.Properties.has_waited is outside of the OnHeld function.

Is this the case? If it is, then move it so that the entire if..then so that it is before the last end of the OnHeld function.

answered 6 years ago
0

I was able to get the timer to count down(and now understand the concept), however, I cannot seem to get the impulse to fire after the timer...it seems to want to fire at the same time as the animation. I'm guessing because has_waited is in the true state by default. Any ideas?...I really appreciate the previous insight...btw

answered 6 years ago
0

Does anyone have any insight on this?

answered 6 years ago
0

Does anyone have any insight on this?

answered 6 years ago
0

Sorry It took me so long But This method may be easier. Hope this helps

REMOVEDUPLOAD

answered 6 years ago
0

function movement:OnPressed(boolValue)

local current_bus = InputEventNotificationBus.GetCurrentBusId()

if current_bus == self.jump_bus_id then

AnimGraphComponentRequestBus.Event.SetNamedParameterBool(self.entityId, "jump", true)

if (TickRequestBus.Broadcast.GetTickDeltaTime() >= self.Properties.nextJump) then

self.Properties.jumpWait = TickRequestBus.Broadcast.GetTickDeltaTime() + 1.0 / self.Properties.jumpWait;

PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, Vector3(0,0,self.Properties.jmp)); -------------------------------------------------------------------------------

Debug.Log(tostring(self.Properties.jumpWait))

Debug.Log(tostring(self.Properties.nextJump))

end

end

I must be doing something wrong. There is still no delay between OnPressed and impulse.

answered 6 years ago
0

Here is how my jump works in script canvas.

  1. onpressed> jump_up anim

  2. as animation editor transition from jump_up anim to in_air anim

  3. impulse kicks in from end of jump_up anim editor parameter

  4. raycast in_air takes over when >1m

  5. raycast in_air stops when <1m

  6. animation editor transitions from in_air anim to jump_down anim

answered 6 years ago
0

Well this totally looks like it should work....and IT DID>>!!

Thanks...I thought it wasn't working at first because the timer wasn't ticking down. Once I set it to .5 instead of 5 it started working perfectly.

smooth as glass

Hate to bug you anymore, but how can I get the world position of the collision on a raycast...?

It was simple in script canvas...but in lua, I can't seem to get the z from the hit...

I'm want to make a simple building system kind of like minecraft, but with a bit more flexibility on block position. I was close in script canvas but could not figure out a way to get the blocks to snap to grid or each other for that matter. I figured it was a limitation with script canvas which is why i'm trying to learn lua.

here is what i have so far...not working at all

local camPosition = TransformBus.Broadcast.GetWorldTM(self.Properties.cam_y)

local configs = RayCastConfiguration()

configs.origin = camPosition:GetTranslation()

local results = PhysicsSystemRequestBus.Broadcast.RayCast(configs)

local count = results:GetHitCount()

if count > 0 then

configs.maxHits = 1

configs.maxDistance = 200

configs.direction = Vector3(0.0, 1.0, 0.0);

local hit = results:GetHit(1)

buildLoc = hit.position

-- Debug.Log(tostring(hit.entityId))

-- Debug.Log("build_loc" ..tostring(buildLoc))

end

end

answered 6 years ago
0

It's in the OnPressed function. It works like that from script canvas, but I will give it a shot when I get home.

answered 6 years ago
0

The timer in its current state goes from true to false to true. I can't seem to get the impulse to fire on the second true so it still fires at the same time as the jump_up animation.

answered 6 years ago
0

Sorry, I meant to say I actually need the full world position of the collision...the way I have it returns the x and y, but the z never changes when I hit an object...it stays at 32m..(ground level). It should go up as I scan up the object. in script canvas if I getworldtranslation from the position output it works fine..

answered 6 years ago
0

This may work to get the World Z.

REMOVEDUPLOAD

answered 6 years ago
0

There is no set function to do this so far i have found. U have to use update. Example Here: https://forums.awsgametech.com/t/is-there-a-way-to-delay-time-in-lua-6/5490/1

answered 6 years ago

This post is closed: Adding new answers, comments, and votes is disabled.