Data Realms Fan Forums http://45.55.195.193/ |
|
New topic: LifeVariation through lua http://45.55.195.193/viewtopic.php?f=73&t=31929 |
Page 1 of 1 |
Author: | 4zK [ Fri Oct 12, 2012 3:41 pm ] |
Post subject: | New topic: LifeVariation through lua |
I'll just ask a new question on the same topic to save space. Is there a way to define - or at least simulate random lifetime variation through lua? |
Author: | CaveCricket48 [ Sat Oct 13, 2012 2:06 pm ] |
Post subject: | Re: Checking for AEmitters |
You can't really do this since wounds are GestHitByMOs = 0, so no ID checking can be done. |
Author: | 4zK [ Sat Oct 13, 2012 2:14 pm ] |
Post subject: | Re: Checking for AEmitters |
Oh, darn. Thanks anyway. |
Author: | Shook [ Sun Oct 14, 2012 2:23 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
To answer the new question, i typically do that by dividing self.Lifetime with a random number between 1 and X, though multiplication can obviously be used as well. Do note, however, that math.random only does integers (whole numbers), so you'll probably want to go like math.random(1000,2000)/1000 in order to get more than two distinct lifetimes. |
Author: | Bad Boy [ Sun Oct 14, 2012 3:17 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
Yeah, math.random() is by default between 0 and 1. I.e. 0.1, 0.00043234, etc. By that evidence, it wouldn't change to integers if you use a larger scale. Also I don't think there's anything in lua (or at least CC lua) that's just an integer, all the numbers seem to be doubles and when a function calls for an integer it's just converted. Though I may be wrong here. |
Author: | p3lb0x [ Sun Oct 14, 2012 3:20 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
From what I gather reading the lua documentation math.random() gives you a number between 0 and 1. BUT if you give it upper and lower limits it will only do integers. The easiest way to get a number from 0 to 40 for example would be to just multiply math.random() with well, 40. Where as something like a number between 20 and 40 would be 20 + math.random()*20. I am not sure if this is the best way to do it, but it works wonders for me. |
Author: | Bad Boy [ Sun Oct 14, 2012 3:25 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
Hmm, maybe it does only give integers, wouldn't be hard to test. I haven't looked into it since I just do the same thing you do, which seems like the best method to me too. |
Author: | Shook [ Sun Oct 14, 2012 4:46 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
Well, it's been my experience that defining lower and upper limits does make it only do integers, for reasons unknown. This is also backed up by the wiki, as p3lb0x said. It might be a better idea to do what he does, since it has more possible outcomes. However, while applying arguments to math.random() makes it return integers, the number itself doesn't seem limited to integer properties, since you can divide it by whatever and still have everything work. Probably just a quirk in the system. |
Author: | p3lb0x [ Sun Oct 14, 2012 4:53 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
From what I gather, Lua just converts values around as it suits it when you try to do something a datatype shouldn't be able to. |
Author: | TheLastBanana [ Mon Oct 15, 2012 5:22 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
It's probably not on the CC wiki, because it isn't so much a property of CC as it is of Lua itself. If you're curious about how Lua works, this is a far better resource than the wiki is (or should) be. |
Author: | Bad Boy [ Mon Oct 15, 2012 8:15 pm ] |
Post subject: | Re: New topic: LifeVariation through lua |
Thanks for that, not sure why I didn't just check some lua reference for this. Just to be sure, every number is a double and if a C function requires an int it gets converted automatically (i.e. combination of what p3lbox and I said)? Also, to get back to the original topic, it seems that giving any arguments to math.random() makes it only return ints. e.g. math.random(0.0005,0.5) will just print 0 and math.random(0.5,1) will print 0 or 1. However, RangeRand() seems to do this properly, though I'm not really sure what other differences (if any) there are between this and math.random(). |
Author: | Arcalane [ Tue Oct 16, 2012 2:35 am ] |
Post subject: | Re: New topic: LifeVariation through lua |
Back to the original message... set a nice high Lifetime on the actual object, then... in Create(self), set up a randomized timer, monitor it during Update(self)... then when it's reached, do self:GibThis() or ToDelete as the 'then' action. If that didn't make much sense - 'cause I know I'm bad at explaining things - then this should clear things up; Code: function Create(self) self.lifeTimer = Timer(); self.lifeTime = 400+math.random(-200,200) end function Update(self) if self.lifeTimer:IsPastSimMS(self.lifeTime) then self:GibThis(); end end I used this to add a random lifetime and auto-detonation to explosive submunitions on a certain weapon. If you only want it in steps of 50/100/200 ms or whatever, then just set up some appropriate math and lower the random range. Easy. |
Author: | Bad Boy [ Tue Oct 16, 2012 2:56 am ] |
Post subject: | Re: New topic: LifeVariation through lua |
Alternate and easier: Code: function Create(self) self.Lifetime = RangeRand(small#, big#); end function Update(self) end function Destroy(self) end |
Author: | Arcalane [ Tue Oct 16, 2012 5:12 am ] |
Post subject: | Re: New topic: LifeVariation through lua |
Probably more efficient, yes. Only reason I used a timer and GibThis in that instance is because it was for an explosive submunition, and AEmitters/etc. only gib if you tell them to; they just disappear into thin air if their lifetime runs out before they hit something hard enough to gib. |
Author: | Bad Boy [ Tue Oct 16, 2012 5:33 am ] |
Post subject: | Re: New topic: LifeVariation through lua |
Well in that case, chuck this in the update function (self.Lifetime is of course the lifetime which can be defined in the ini rather than the lua): Code: if self.Age > self.Lifetime - 100 then self:GibThis(); end It'll detonate a bit before what you actually set Lifetime to to make sure it doesn't just delete. I'm not sure what the safe margin there is but you may be able to cut it down to something like 20 MS instead. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |