View unanswered posts | View active topics It is currently Thu Mar 28, 2024 2:02 pm



Reply to topic  [ 15 posts ] 
 New topic: LifeVariation through lua 
Author Message
User avatar

Joined: Mon Oct 11, 2010 1:15 pm
Posts: 594
Location: Finlandia
Reply with quote
Post New topic: LifeVariation through lua
How do I check for up to (x) emitters that have have the same parent?

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?


Last edited by 4zK on Sun Oct 14, 2012 12:29 am, edited 1 time in total.



Fri Oct 12, 2012 3:41 pm
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13143
Location: Here
Reply with quote
Post Re: Checking for AEmitters
You can't really do this since wounds are GestHitByMOs = 0, so no ID checking can be done.


Sat Oct 13, 2012 2:06 pm
Profile
User avatar

Joined: Mon Oct 11, 2010 1:15 pm
Posts: 594
Location: Finlandia
Reply with quote
Post Re: Checking for AEmitters
Oh, darn. Thanks anyway.


Sat Oct 13, 2012 2:14 pm
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post 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.


Sun Oct 14, 2012 2:23 pm
Profile WWW

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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.


Sun Oct 14, 2012 3:17 pm
Profile
Forum Moderator
User avatar

Joined: Fri Feb 02, 2007 3:53 pm
Posts: 1896
Location: in my little gay bunker
Reply with quote
Post 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.


Sun Oct 14, 2012 3:20 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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.


Sun Oct 14, 2012 3:25 pm
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post 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.


Sun Oct 14, 2012 4:46 pm
Profile WWW
Forum Moderator
User avatar

Joined: Fri Feb 02, 2007 3:53 pm
Posts: 1896
Location: in my little gay bunker
Reply with quote
Post 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.


Sun Oct 14, 2012 4:53 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post 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.


Mon Oct 15, 2012 5:22 pm
Profile WWW

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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().


Mon Oct 15, 2012 8:15 pm
Profile
User avatar

Joined: Sun Jan 28, 2007 10:32 pm
Posts: 1609
Location: UK
Reply with quote
Post 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.


Tue Oct 16, 2012 2:35 am
Profile YIM

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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


Tue Oct 16, 2012 2:56 am
Profile
User avatar

Joined: Sun Jan 28, 2007 10:32 pm
Posts: 1609
Location: UK
Reply with quote
Post 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.


Tue Oct 16, 2012 5:12 am
Profile YIM

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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.


Tue Oct 16, 2012 5:33 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 15 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.334s | 15 Queries | GZIP : Off ]