I need help with Lua scripting in terms of spawning and deleting objects as well as providing an impulse and detecting collision!

0

I'm just starting in Lua and I'm struggling with the APIs and finding a lack of information the actions I need to implement. I managed to do all this with flowgraph but can't even figure out where to start with Lua.

asked 7 years ago285 views
6 Answers
0
Accepted Answer

Hey @REDACTEDUSER

We should have more Lua documentation soon.

answered 7 years ago
0

I have looked at that tutorial. The biggest problem is just lack of API documentation makes things really difficult to get things started. The only other resource seems to be looking at the Lua scripts attached with the samples.

answered 7 years ago
0

I understand for sure. Hopefully the LUA API docs will be out before too long. Sorry for the frustrations.

answered 7 years ago
0

Hi.

Thanks for checking out my previous tutorial, but unfortunately it's pretty basic and out of date as of 1.8. I'm actually planning to make a new one that goes over some of the stuff you're asking about. Kind of like a 'here's lots of cool stuff to do in lau' video.

I'll post a link in a few days, but for now, try this: I made a small little script that demonstrates how to add an impulse to a physics object:

REMOVEDUPLOAD

And here's a demo of it running:

REMOVEDUPLOAD

Open a new script, Save the lua file in the dev/Projects folder, this will make it compile and give you a .luac file that you can attach via a lua script component..

	local luatest = {
Properties =
{
-- properties are editable in the editor
Speed = {default = 10.0},
},
}
--
-- connect the tick bus when the script activates
--
function luatest:OnActivate()
-- need to connect the tickbus for the ontick function
self.tickBus = TickBus.Connect(self, 0);
end
--
-- on tick is called every frame (if the tickbus is connected)
--
function luatest:OnTick()
-- use the physics component bus to add an impulse. Notice the use of self.Properties.Speed
PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, Vector3(self.Properties.Speed,0,0));
end
--
-- this will be called when the stops running
--
function luatest:OnDeactivate()
-- disconnect the tick bus
self.tickBus:Disconnect();
end
return luatest
answered 7 years ago
0

Hi @REDACTEDUSER I know you asked the question a while ago and probably already have a solution, but I thought I would add to the solutions provided above for those who come along later. To spawn a dynamic slice (it has to be a dynamic slice), you add a spawner to the entity where you want the object to spawn and add the dynamic slice by dragging it from the Asset Browser to the Dynamic slice field listed in the Spawner.

To call the Spawner in Lua script (after you have your bindings):

function shoot:OnPressed() SpawnerComponentRequestBus.Event.Spawn(self.entityId) end

To remove the dynamic slice, use: GameEntityContextRequestBus.Broadcast.DestroyDynamicSliceByEntity(self.entityId)

I have the Destroy command in a Lua script that is associated with the a dynamic slice projectile. Here is the full projectile script. This also includes a SetVelocity to move the projectile in the forward direction:

	local shot = {
Properties = {
force = {default = 1000.0, description = "force applied to shot"},
timer = {default = 3.0, description = "timer for projectile"},
},
}
function shot:OnActivate()
self.tickBusHandler = TickBus.Connect(self)
local shotDir = TransformBus.Event.GetWorldTM(self.entityId)
local forwardV = shotDir:GetColumn(1) --returns direction facing
PhysicsComponentRequestBus.Event.SetVelocity(self.entityId, forwardV * self.Properties.force)
self.tickBusHandler = TickBus.Connect(self) self.timer = self.Properties.timer
end
function shot:OnTick(deltaTime)
if self.timer > 0.0 then self.timer = self.timer - deltaTime
else
-- remove projectile
GameEntityContextRequestBus.Broadcast.DestroyDynamicSliceByEntity(self.entityId)
end
end
function shot:OnDeactivate()
self.tickBusHandler:Disconnect()
end
return shot
answered 6 years ago

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