Data Realms Fan Forums
http://45.55.195.193/

LUA code to gib a given object between two times
http://45.55.195.193/viewtopic.php?f=75&t=21063
Page 1 of 1

Author:  thesoupiest [ Sat Jan 08, 2011 8:20 pm ]
Post subject:  LUA code to gib a given object between two times

Hello.
I am currently using the code the Revolver Cannon uses to detonate its shells (albeit with different time settings) to cause my explosives to explode after a certain period of time. The code is as follows.
Code:
function Create(self)
    self.LTimer = Timer();
end

function Update(self)
   if self.LTimer:IsPastSimMS(250) then
      self:GibThis();
   end
end

However, as I create more complex mods, I find myself requiring more randomness in the gibbing of my rounds. I, not being especially verse in the ways of LUA (though I would be were the wiki working), ask you for a shred of help.
I need a LUA script which will cause whatever the script is used by to gib randomly between two points in time, I.E. a script which would make something detonate at any point between 500MS and 1000MS, allowing the something to gib at 500MS, 1000MS, or anywhere in between (this detonation point randomly selected).
Whoever would provide this script would receive credit for it in any mod which I use it in.
Thank you in advance to whoever does!

Author:  Coops [ Sat Jan 08, 2011 9:12 pm ]
Post subject:  Re: LUA code to gib a given object between two times

Ya might need another zero in that "100" there Non.

Author:  thesoupiest [ Sat Jan 08, 2011 9:27 pm ]
Post subject:  Re: LUA code to gib a given object between two times

Thank you kindly, Nonsequitorian. You will receive full credit when I update my Red Loki pack with this modification.

Author:  411570N3 [ Sun Jan 09, 2011 9:12 am ]
Post subject:  Re: LUA code to gib a given object between two times

That rolls it every frame though, which might lead to it not detonating at all. It would be better to assign something the value of math.random(500,1000) on create and check against that in update.

Author:  mail2345 [ Sun Jan 09, 2011 9:42 am ]
Post subject:  Re: LUA code to gib a given object between two times

That's trivial:
Code:
function Create(self)
    self.LTimer = Timer();
    self.DTime = math.random(500,1000)
end

function Update(self)
   if self.LTimer:IsPastSimMS(self.DTime) then
      self:GibThis();
   end
end

Author:  411570N3 [ Sun Jan 09, 2011 11:14 am ]
Post subject:  Re: LUA code to gib a given object between two times

Yeah, but it's a potentially horrific error to make your weapon not detonate at all 36.751199037189124961134981627226% of the time when it should be 100%.

Author:  Roast Veg [ Sun Jan 09, 2011 4:40 pm ]
Post subject:  Re: LUA code to gib a given object between two times

How did you calculate that statistic, Allstone?

Either it's a random collection of numbers, or it has some sense in it, in which case I'm intrigued as to how you came to it.

Author:  411570N3 [ Sun Jan 09, 2011 5:43 pm ]
Post subject:  Re: LUA code to gib a given object between two times

Every frame, it rolls for a number between 500 and 1000, inclusive. If the current life timer count is equal to the number rolled, the projectile detonates. The only times when a the check is positive are between 500 and 1000 frames, inclusive. This is 501 checks of a 1 in 501 chance. Hence, the chance of the projectile not detonating at all is (500/501)^501, which is roughly equivalent to the shown percentage.

Author:  Roast Veg [ Sun Jan 09, 2011 5:45 pm ]
Post subject:  Re: LUA code to gib a given object between two times

I had a feeling it wasn't random. Very clever.

Author:  Coops [ Tue Jan 11, 2011 2:47 am ]
Post subject:  Re: LUA code to gib a given object between two times

Non, What I think Allstone was trying to say was when he's got it like this:

... :IsPastSimMS(math.random(500,1000))

then if the number is chosen in the math.random is equal to the time that its at then it gibs or might not gib at all.

But if you define the math.random when the particle is created then the number that is generated is already chosen and is ready for when the timer has got to it. So no, they would not have the same lifetime everytime since its always a different scripted Object every time.

Also defining the math.random after the self:GibThis() is quite useless as the scripted MO is already gone...

Author:  411570N3 [ Tue Jan 11, 2011 3:05 am ]
Post subject:  Re: LUA code to gib a given object between two times

I think he's confused as to the timings of Lua. Create() will run every time that kind of particle is created and all of the Self variables apply only to that particle.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/