View unanswered posts | View active topics It is currently Sat Dec 28, 2024 5:19 am



Reply to topic  [ 11 posts ] 
 LUA code to gib a given object between two times 
Author Message
User avatar

Joined: Thu Dec 16, 2010 11:06 pm
Posts: 276
Reply with quote
Post 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!


Sat Jan 08, 2011 8:20 pm
Profile
User avatar

Joined: Wed Feb 17, 2010 12:07 am
Posts: 1545
Location: That small peaceful place called Hell.
Reply with quote
Post Re: LUA code to gib a given object between two times
Ya might need another zero in that "100" there Non.


Sat Jan 08, 2011 9:12 pm
Profile
User avatar

Joined: Thu Dec 16, 2010 11:06 pm
Posts: 276
Reply with quote
Post 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.


Sat Jan 08, 2011 9:27 pm
Profile
User avatar

Joined: Wed Jan 07, 2009 10:26 am
Posts: 4074
Location: That quaint little British colony down south
Reply with quote
Post 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.


Sun Jan 09, 2011 9:12 am
Profile WWW
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post 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


Sun Jan 09, 2011 9:42 am
Profile
User avatar

Joined: Wed Jan 07, 2009 10:26 am
Posts: 4074
Location: That quaint little British colony down south
Reply with quote
Post 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%.


Sun Jan 09, 2011 11:14 am
Profile WWW
Data Realms Elite
Data Realms Elite
User avatar

Joined: Tue May 25, 2010 8:27 pm
Posts: 4521
Location: Constant motion
Reply with quote
Post 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.


Sun Jan 09, 2011 4:40 pm
Profile
User avatar

Joined: Wed Jan 07, 2009 10:26 am
Posts: 4074
Location: That quaint little British colony down south
Reply with quote
Post 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.


Sun Jan 09, 2011 5:43 pm
Profile WWW
Data Realms Elite
Data Realms Elite
User avatar

Joined: Tue May 25, 2010 8:27 pm
Posts: 4521
Location: Constant motion
Reply with quote
Post Re: LUA code to gib a given object between two times
I had a feeling it wasn't random. Very clever.


Sun Jan 09, 2011 5:45 pm
Profile
User avatar

Joined: Wed Feb 17, 2010 12:07 am
Posts: 1545
Location: That small peaceful place called Hell.
Reply with quote
Post 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...


Tue Jan 11, 2011 2:47 am
Profile
User avatar

Joined: Wed Jan 07, 2009 10:26 am
Posts: 4074
Location: That quaint little British colony down south
Reply with quote
Post 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.


Tue Jan 11, 2011 3:05 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.025s | 17 Queries | GZIP : Off ]