Chances of happening in a lua [Solved]
Moderator: Moderators
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Chances of happening in a lua [Solved]
Is there a code that gives (for example) a one in four shot at happening? Such as in 20 seconds, an object will appear, but has a 1 in 4 chance of appearing.
Last edited by Noobasaurus on Tue Nov 08, 2011 11:29 am, edited 1 time in total.
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: Chances of happening in a lua
use a random function to generate an integer from 1 to 4. if the number is 4, do something.
I think that will work, unless i got the name of the function wrong.
Edit: Fixed a typo in the if statement. ~A moderator
Code: Select all
do_something = math.random(1,4)
if do_something == 4 then
--do whatever
endEdit: Fixed a typo in the if statement. ~A moderator
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
Would this apply to everything below it or just in the timer?
ex.
EDIT: Got this error:
ex.
Hidden/Spoiler:
Hidden/Spoiler:
- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
I dunno if you're familiar with this type of munge errors, but what it's telling you is that there should be a "then" right near the "=" on line 70. Wherever, line 70 is.....Just look down the side of the lua editor (notepad++ is best) and you'll find line 70 easily enough.
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
That line would probably be this onelucasfart wrote:I dunno if you're familiar with this type of munge errors, but what it's telling you is that there should be a "then" right near the "=" on line 70. Wherever, line 70 is.....Just look down the side of the lua editor (notepad++ is best) and you'll find line 70 easily enough.
Code: Select all
acklay3 = math.random(1,4)Code: Select all
if acklay3 = 4 thenEDIT: After my tests, I found that
Code: Select all
if acklay5 = 2 then- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
Could you post the whole lua? It might be linked to something a bit later in the script....
- Locutus
- 1st Lieutenant

- Posts: 420
- Joined: Fri Jun 04, 2010 10:08 am
- Projects :: Stargate Battlefront Pegasus
- Location: Germany
- Contact:
Re: Chances of happening in a lua
I never used Lua before but in other programming languages you usually write
Code: Select all
if a == b then-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
Yes, but even then if it does apply in luas it does not have a 'then' next to the '='.Locutus wrote:I never used Lua before but in other programming languages you usually write
Code: Select all
if a == b then
lucasfart wrote:Could you post the whole lua? It might be linked to something a bit later in the script....
Hidden/Spoiler:
-
THEWULFMAN
- Space Ranger
- Posts: 5557
- Joined: Tue Aug 17, 2010 3:30 pm
- Projects :: Evolved 2
- Location: Columbus, Ohio
- Contact:
Re: Chances of happening in a lua
Locutus wrote:I never used Lua before but in other programming languages you usually write
Code: Select all
if a == b then
Yeah... no. You'll find Lua doesn't do exactly what you expect it to do compared to other languages. It's still really fun though.
@OP
I might take a look at your code later, I'm a bit busy right now. You're probably just off on the syntax a tiny bit, so it expects something it doesn't actually need.
- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
I'm not much of a scripting pro, but shouldn't there be some sort of function line before it? What i mean is, shouldn't it be in its own function? I'm probably wrong, but thats just what i though. 
No need to be rude....THEWULFMAN wrote: Yeah... no.
-
THEWULFMAN
- Space Ranger
- Posts: 5557
- Joined: Tue Aug 17, 2010 3:30 pm
- Projects :: Evolved 2
- Location: Columbus, Ohio
- Contact:
Re: Chances of happening in a lua
lucasfart wrote:No need to be rude....THEWULFMAN wrote: Yeah... no.
No need to be presumptuous. I was just being a little sarcastic. Moreover, it's just how I talk in real life.
tl:dr
I wasn't rude, at least not intentionally.
- Maveritchell
- Jedi Admin

- Posts: 7366
- Joined: Mon Aug 21, 2006 11:03 pm
Re: Chances of happening in a lua
Are you sure? Are you really sure? Don't jump on someone when you're not.THEWULFMAN wrote:Yeah... no.
What we have here is a failure to communicate. (And understand!)Noobasaurus wrote:Yes, but even then if it does apply in luas it does not have a 'then' next to the '='.
Setting an equality in Luascript uses a single equals sign:
x = 10
Testing an equality in Luascript - just like Locutus said - uses a double equals sign
if x == 10 then
Fix the incorrect equals sign. The reason you're getting the error is because your logic is written incorrectly, not because the "then" is in the wrong place. So much time is wasted in discourse in a forum thread when it would take about two minutes to test what someone wrote for you as a possible solution.
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
Code: Select all
M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:76: `=' expected near `)'- Maveritchell
- Jedi Admin

- Posts: 7366
- Joined: Mon Aug 21, 2006 11:03 pm
Re: Chances of happening in a lua
[Ignore the original text; it's late and I'm tired.]
You have a variable in line six with no precedent or antecedent ("acklay"). It's just there for no reason. Delete that whole line.
You have a variable in line six with no precedent or antecedent ("acklay"). It's just there for no reason. Delete that whole line.
- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
As before it's probably linked to something else, as i doubt you need to put an "=" near the end ofNoobasaurus wrote:It says I need this... Although I doubt if I put it in it will not work...Code: Select all
M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:76: `=' expected near `)'
Code: Select all
if acklay5 = 2 then
--SPAWN ACKLAY! YEZ!
SetProperty("acklay", "SpawnPath", "acklay_spawn")
StartTimer(acklay1)
DestroyTimer(timer)
end,
acklay
)
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
Yes, after reading how you analyzed the first one, I thought that it would be very weird to put one over there. Testing now.Maveritchell wrote:That's true, it does say that. You've been told why it says that and how to fix it, but if you want to simply ignore that because you think you've got a better handle on what the error says, more power to you.
- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
As Mav just added in, try deleting the line with "acklay" just before the ")". At least, i think that's the acklay he's talking about...
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Chances of happening in a lua
lucasfart wrote:As Mav just added in, try deleting the line with "acklay" just before the ")". At least, i think that's the acklay he's talking about...
In the whole timer script? Or starting from a certain point?Maveritchell wrote: in line six
@lucasfart Did that and got this...
Code: Select all
M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:75: unexpected symbol near `)'
- lucasfart
- Sith

- Posts: 1440
- Joined: Tue Feb 24, 2009 5:32 am
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
- Location: Australia
Re: Chances of happening in a lua
Well the second you get up, try moving the bracket down a line.
I'm guessing you put it right near the comma? If that doesn't work try getting rid of the comma altogether....hopefully that'll fix your problems. 
- [RDH]Zerted
- Gametoast Staff

- Posts: 2982
- Joined: Sun Feb 26, 2006 7:36 am
- Projects :: Bos Wars AI - a RTS game
- xbox live or psn: No gamertag set
- Location: USA
- Contact:
Re: Chances of happening in a lua
I don't know what line Maveritchell is referring to (line 7 in you short code bit?), but you need to keep that ackaly. It's the last parameter to the OnTimerElapse function. It says which timer that code is linked to. Without it, the code would be run anytime any timer elapsed.
The real problem is the if statement: It never ends. The end, line ends the function(timer) not the if statement, but the if statement has to end first. When there are syntax errors (typos, missing keywords, or extra keywords), the compiler/munger gets confused and has to guess what the error is. It normally guesses wrong, but the error tends to be close enough to the real problem or gives you enough info to figure it out if you have enough experience.
You have at least one more error too. You do StartTimer(acklay1) before the acklay1 variable exists. The scripts are a bit confusing on how timers are handled. You can refer to a timer by its name: variable = CreateTimer(name) or by its variable: variable = CreateTimer(name). The official map scripts normally have the variable and name being the same word, and that makes it confusing. The simplest way to fix this is to change that line to use the timer name: StartTimer("acklay1")
The corrected code for both issues is:Personally, I'd recommend only using the timer name as there's less chances of errors. The only drawback is if you want to rename a timer, you have to change it in every place you use it's name.
The real problem is the if statement: It never ends. The end, line ends the function(timer) not the if statement, but the if statement has to end first. When there are syntax errors (typos, missing keywords, or extra keywords), the compiler/munger gets confused and has to guess what the error is. It normally guesses wrong, but the error tends to be close enough to the real problem or gives you enough info to figure it out if you have enough experience.
You have at least one more error too. You do StartTimer(acklay1) before the acklay1 variable exists. The scripts are a bit confusing on how timers are handled. You can refer to a timer by its name: variable = CreateTimer(name) or by its variable: variable = CreateTimer(name). The official map scripts normally have the variable and name being the same word, and that makes it confusing. The simplest way to fix this is to change that line to use the timer name: StartTimer("acklay1")
The corrected code for both issues is:
Code: Select all
acklay = CreateTimer("acklay")
duration1 = math.random(200,400)
SetTimerValue(acklay, duration1)
StartTimer(acklay)
OnTimerElapse(
function(timer)
acklay5 = math.random(1,4)
if acklay5 = 2 then
--SPAWN ACKLAY! YEZ!
SetProperty("acklay", "SpawnPath", "acklay_spawn")
StartTimer("acklay1")
end
DestroyTimer(timer)
end,
acklay
)
--rnow delete the acklay path so that if it falls off a cliff it does not appear again!--
acklay1 = CreateTimer("acklay1")
SetTimerValue(acklay1, 5)
OnTimerElapse(
function(timer)
SetProperty("acklay", "SpawnPath", "")
DestroyTimer(timer)
end,
acklay1
)