2DWorlds Forums
Glitch with velocity - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4)
+--- Forum: Bug Reports (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=23)
+--- Thread: Glitch with velocity (/showthread.php?tid=168)



Glitch with velocity - Duck - 12-16-2012

I think this is a glitch but I might be just misunderstanding the velocity objects
[Image: glitchithink.png]

I had the same problem with the VelocityController object


RE: Glitch with velocity - Duck - 12-16-2012

Code:
gamePlayer = script.Parent.Parent.Parent

function firebullet()
Bullet = game.Environment.Bullet
link(script.Parent.MouseDown, firebullet)
gunPosition = script.Parent:getPosition()
gunAngle = script.Parent:getAngle()
newBullet = Bullet:clone()
newBullet.Parent = game.World
newBullet.Position = gunPosition
newBullet.Rotation = gunAngle - 180
newBullet.FixedVelocity.Velocity = -100 --error here
end

link(script.Parent.MouseDown, firebullet)



RE: Glitch with velocity - Ghosty - 12-16-2012

Try

newBullet:getChild("FixedVelocity").Velocity = Vec2D(-50, 0)

[-100 is WHAM out of sight in a hundreth of a second.]


RE: Glitch with velocity - Duck - 12-16-2012

I tried ghostys solution and got a different error saying Script:12: attempted index of a non-table


RE: Glitch with velocity - Jacob__mybb_import1 - 12-16-2012

Use gameConfusedcheduleTask():
[lua]
gameConfusedcheduleTask(function()
newBullet = Bullet:clone()
newBullet.Position = gunPosition
newBullet.Rotation = gunAngle - 180
newBullet.FixedVelocity.Velocity = Vec2D(-100,0) --error here
newBullet.Parent = game.World
end)
[/lua]

(long, probably confusing explanation below)

The problem is that scripts run separately from everything else. The game engine can only do one thing at a time (it can't add stuff while simulating physics, for example) so when you run a script that does something like cloning, adding, or removing stuff, it's actually telling the game to do it as soon as it's not busy. But the script won't wait for the game to be done, so 3 lines later the bullet's children won't have been added yet when the script asks for the one called FixedVelocity. Using gameConfusedcheduleTask() just tells the game engine to run the block of code in between drawing stuff or whatever else it's doing, and since only one thing is happening at the same time it can do any cloning, adding, or removing exactly when it's supposed to.

It would be better to just make all scripts use gameConfusedcheduleTask() without you having to type it yourself, but that would require removing sleep() and infinite loops and a lot of people were against that last time I tried.


RE: Glitch with velocity - Duck - 12-16-2012

thanks, it works fine now