Page 1 of 2

Chances of happening in a lua [Solved]

Posted: Mon Nov 07, 2011 7:44 pm
by Noobasaurus
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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 8:01 pm
by kinetosimpetus
use a random function to generate an integer from 1 to 4. if the number is 4, do something.

Code: Select all

do_something = math.random(1,4)

if do_something == 4 then
--do whatever
end
I think that will work, unless i got the name of the function wrong.
Edit: Fixed a typo in the if statement. ~A moderator

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 8:24 pm
by Noobasaurus
Would this apply to everything below it or just in the timer?
ex.
Hidden/Spoiler:
[code]
acklay = CreateTimer("acklay")
dur = math.random(200,400)
SetTimerValue(acklay, dur)
StartTimer(acklay)
OnTimerElapse(
function(timer)
acklay3 = math.random(1,4)

if acklay3 = 4 then
--SPAWN ACKLAY! YEZ!
SetProperty("acklay", "SpawnPath", "acklay_spawn")
StartTimer(acklay1)
DestroyTimer(timer)
end,
acklay
)[/code]
Would the start timer only happen if the acklay spawn happened?
EDIT: Got this error:
Hidden/Spoiler:
[code]M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:70: `then' expected near `='[/code]

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:08 pm
by lucasfart
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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:22 pm
by Noobasaurus
lucasfart 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.
That line would probably be this one

Code: Select all

			acklay3 = math.random(1,4)
or this one

Code: Select all

			if acklay3 = 4 then
I really don't think 'then' fits next to the '=' in either of them, but since there's only 4 possibilities I'll troubleshoot.

EDIT: After my tests, I found that

Code: Select all

			if acklay5 = 2 then
is line 70 BUT putting then anywhere near '=' did not work.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:40 pm
by lucasfart
Could you post the whole lua? It might be linked to something a bit later in the script....

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:43 pm
by Locutus
I never used Lua before but in other programming languages you usually write

Code: Select all

if a == b then

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:52 pm
by Noobasaurus
Locutus wrote:I never used Lua before but in other programming languages you usually write

Code: Select all

if a == b then
Yes, but even then if it does apply in luas it does not have a 'then' next to the '='.
lucasfart wrote:Could you post the whole lua? It might be linked to something a bit later in the script....
Hidden/Spoiler:
--
-- Copyright (c) 2005 Pandemic Studios, LLC. All rights reserved.
--

--THIS IS THE EASY MODE--

ScriptCB_DoFile("setup_teams")
ScriptCB_DoFile("ObjectiveConquest")
ScriptCB_SetGameRules("campaign")
ScriptCB_DoFile("ObjectiveTDM")


-- REP Attacking (attacker is always #1)
REP = 2;
CIS = 1;
-- These variables do not change
ATT = REP;
DEF = CIS;

function ScriptPostLoad ()

SetProperty("cp2", "SpawnPath", "")
SetProperty("Local_Cp", "SpawnPath", "")
SetProperty("acklay", "SpawnPath", "")
ShowMessageText("level.GAA.objectives.campaign.start", ATT)

--Timer STARTZ--
music01Timer = CreateTimer("music01")
SetTimerValue(music01Timer, 90.0)

StartTimer(music01Timer)


OnTimerElapse(
function(timer)
SetProperty("cp2", "SpawnPath", "cp2_spawn")
SetProperty("Local_Cp", "SpawnPath", "Local_Spawn")
ShowMessageText("level.GAA.objectives.campaign.timerend", ATT)
DestroyTimer(timer)
end,
music01Timer
)

--Timer for victory startz nao...also known as when the first timer elapses--
music02Timer = CreateTimer("music02Timer")
SetTimerValue(music02Timer, 630.0)
ShowTimer(music02Timer)
StartTimer(music02Timer)
OnTimerElapse(
function(timer)
MissionVictory(ATT)
ShowTimer(nil)


DestroyTimer(timer)
end,
music02Timer
)

--random acklay timer! YAY 4 ACKLAYZ--
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)
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
)
--This defines the CPs. These need to happen first
cp1 = CommandPost:New{name = "cp1"}
cp2 = CommandPost:New{name = "cp2"}
cp3 = CommandPost:New{name = "cp3"}
cp4 = CommandPost:New{name = "cp4"}
cp5 = CommandPost:New{name = "Local_CP"}
cp6 = CommandPost:New{name = "acklay"}



--This sets up the actual objective. This needs to happen after cp's are defined
conquest = ObjectiveConquest:New{teamATT = ATT, teamDEF = DEF,
textATT = "game.modes.con",
textDEF = "game.modes.con2",
multiplayerRules = true}

--This adds the CPs to the objective. This needs to happen after the objective is set up
conquest:AddCommandPost(cp1)
conquest:AddCommandPost(cp2)
conquest:AddCommandPost(cp3)
conquest:AddCommandPost(cp4)

conquest:Start()

SetUberMode(1);

EnableSPHeroRules()

AddDeathRegion("death")
AddDeathRegion("death1")

end


---------------------------------------------------------------------------
-- FUNCTION: ScriptInit
-- PURPOSE: This function is only run once
-- INPUT:
-- OUTPUT:
-- NOTES: The name, 'ScriptInit' is a chosen convention, and each
-- mission script must contain a version of this function, as
-- it is called from C to start the mission.
---------------------------------------------------------------------------
function ScriptInit()

ReadDataFile("ingame.lvl")


SetMaxFlyHeight(300)
SetMaxPlayerFlyHeight (300)

SetMemoryPoolSize ("ClothData",20)
SetMemoryPoolSize ("Combo",50) -- should be ~ 2x number of jedi classes
SetMemoryPoolSize ("Combo::State",650) -- should be ~12x #Combo
SetMemoryPoolSize ("Combo::Transition",650) -- should be a bit bigger than #Combo::State
SetMemoryPoolSize ("Combo::Condition",650) -- should be a bit bigger than #Combo::State
SetMemoryPoolSize ("Combo::Attack",550) -- should be ~8-12x #Combo
SetMemoryPoolSize ("Combo::DamageSample",6000) -- should be ~8-12x #Combo::Attack
SetMemoryPoolSize ("Combo::Deflect",100) -- should be ~1x #combo

ReadDataFile("sound\\kam.lvl;kam1cw")

ReadDataFile("dc:SIDE\\gam.lvl",
"gam_inf_gamorreanguard",
"gam_inf_elite")
ReadDataFile("dc:SIDE\\imp.lvl",
"imp_inf_engi",
"imp_inf_obi",
"imp_inf_ganimedes")

ReadDataFile("dc:SIDE\\cis.lvl",
"cis_inf_rifleman")
ReadDataFile("dc:SIDE\\geo.lvl",
"geo_inf_acklay")

ReadDataFile("dc:SIDE\\tur.lvl",
"tur_bldg_tower",
"tur_bldg_built_beam",
"tur_bldg_laser",
"tur_bldg_chaingun_tripod",
"tur_bldg_chaingun_tripod2",
"tur_bldg_chaingun_roof")



SetupTeams{
rep = {
team = REP,
units = 350,
reinforcements = -1,
soldier = { "gam_inf_gamorreanguard",150, 200},
assault = { "gam_inf_elite",50,150},
engineer = { "",1,4},
sniper = { "",1,4},
officer = { "",1,4},
special = { "",1,4},

},
cis = {
team = CIS,
units = 50,
reinforcements = 200,
soldier = { "cis_inf_rifleman",0, 10},
assault = { "imp_inf_ganimedes",0, 10},
engineer = { "imp_inf_engi",0, 10},
sniper = { "imp_inf_obi",0, 10},
officer = {"",0, 0},
special = { "",0, 0},
}
}



SetTeamName (3, "jawas")
AddUnitClass (3, "gam_inf_gamorreanguard", 250,300)
SetUnitCount (3, 170)
AddAIGoal(3, "Deathmatch", 1000)
SetTeamAsEnemy(DEF,3)
SetTeamAsEnemy(3,DEF)
SetTeamAsFriend(ATT,3)
SetTeamAsFriend(3,ATT)
SetTeamAsFriend(4,3)
SetTeamAsFriend(3,4)

SetTeamName (4, "acklay")
AddUnitClass (4, "geo_inf_acklay", 1,1)
SetUnitCount (4, 1)
AddAIGoal(4, "Deathmatch", 1000)
SetTeamAsEnemy(DEF,4)
SetTeamAsEnemy(4,DEF)
SetTeamAsFriend(ATT,4)
SetTeamAsFriend(4,ATT)
SetTeamAsFriend(4,3)
SetTeamAsFriend(3,4)


-- Level Stats
-- ClearWalkers()
AddWalkerType(0, 4) -- special -> droidekas
AddWalkerType(1, 0) -- 1x2 (1 pair of legs)
AddWalkerType(2, 0) -- 2x2 (2 pairs of legs)
AddWalkerType(3, 0) -- 3x2 (3 pairs of legs)
local weaponCnt = 1024
SetMemoryPoolSize("Aimer", 75)
SetMemoryPoolSize("AmmoCounter", weaponCnt)
SetMemoryPoolSize("BaseHint", 1024)
SetMemoryPoolSize("EnergyBar", weaponCnt)
SetMemoryPoolSize("EntityCloth", 32)
SetMemoryPoolSize("EntityFlyer", 32)
SetMemoryPoolSize("EntityHover", 32)
SetMemoryPoolSize("SoldierAnimation", 999)
SetMemoryPoolSize("EntityLight", 200)
SetMemoryPoolSize("EntityPortableTurret", 1200)
SetMemoryPoolSize("EntitySoundStream", 4)
SetMemoryPoolSize("EntitySoundStatic", 32)
SetMemoryPoolSize("MountedTurret", 99)
SetMemoryPoolSize("LightFlash", 400)
SetMemoryPoolSize("Navigator", 500)
SetMemoryPoolSize("Obstacle", 1024)
SetMemoryPoolSize("PathNode", 1024)
SetMemoryPoolSize("SoundSpaceRegion", 64)
SetMemoryPoolSize("TreeGridStack", 1024)
SetMemoryPoolSize("UnitAgent", 600)
SetMemoryPoolSize("UnitController", 750)
SetMemoryPoolSize("Weapon", weaponCnt)

SetSpawnDelay(10.0, 0.25)
--ReadDataFile("dc:GAA\\GAA.lvl", "GAA_conquest")
ReadDataFile("dc:GAA\\GAA.lvl", "GAA_conquest")
SetDenseEnvironment("false")




-- Sound

SetSoundEffect("ScopeDisplayZoomIn", "binocularzoomin")
SetSoundEffect("ScopeDisplayZoomOut", "binocularzoomout")

voiceSlow = OpenAudioStream("sound\\global.lvl", "rep_unit_vo_slow")
AudioStreamAppendSegments("sound\\global.lvl", "cis_unit_vo_slow", voiceSlow)
AudioStreamAppendSegments("sound\\global.lvl", "global_vo_slow", voiceSlow)

voiceQuick = OpenAudioStream("sound\\global.lvl", "rep_unit_vo_quick")
AudioStreamAppendSegments("sound\\global.lvl", "cis_unit_vo_quick", voiceQuick)

OpenAudioStream("sound\\global.lvl", "cw_music")
-- OpenAudioStream("sound\\global.lvl", "global_vo_quick")
-- OpenAudioStream("sound\\global.lvl", "global_vo_slow")
OpenAudioStream("sound\\yav.lvl", "yav1")
OpenAudioStream("sound\\yav.lvl", "yav1")
OpenAudioStream("sound\\yav.lvl", "yav1_emt")

SetBleedingVoiceOver(REP, REP, "rep_off_com_report_us_overwhelmed", 1)
SetBleedingVoiceOver(REP, CIS, "rep_off_com_report_enemy_losing", 1)
SetBleedingVoiceOver(CIS, REP, "cis_off_com_report_enemy_losing", 1)
SetBleedingVoiceOver(CIS, CIS, "cis_off_com_report_us_overwhelmed", 1)

SetOutOfBoundsVoiceOver(2, "cisleaving")
SetOutOfBoundsVoiceOver(1, "repleaving")

SetAmbientMusic(REP, 1.0, "rep_yav_amb_start", 0,1)
SetAmbientMusic(REP, 0.8, "rep_yav_amb_middle", 1,1)
SetAmbientMusic(REP, 0.2, "rep_yav_amb_end", 2,1)
SetAmbientMusic(CIS, 1.0, "cis_yav_amb_start", 0,1)
SetAmbientMusic(CIS, 0.8, "cis_yav_amb_middle", 1,1)
SetAmbientMusic(CIS, 0.2, "cis_yav_amb_end", 2,1)

SetVictoryMusic(REP, "rep_yav_amb_victory")
SetDefeatMusic (REP, "rep_yav_amb_defeat")
SetVictoryMusic(CIS, "cis_yav_amb_victory")
SetDefeatMusic (CIS, "cis_yav_amb_defeat")

SetSoundEffect("ScopeDisplayZoomIn", "binocularzoomin")
SetSoundEffect("ScopeDisplayZoomOut", "binocularzoomout")
--SetSoundEffect("BirdScatter", "birdsFlySeq1")
--SetSoundEffect("WeaponUnableSelect", "com_weap_inf_weaponchange_null")
--SetSoundEffect("WeaponModeUnableSelect", "com_weap_inf_modechange_null")
SetSoundEffect("SpawnDisplayUnitChange", "shell_select_unit")
SetSoundEffect("SpawnDisplayUnitAccept", "shell_menu_enter")
SetSoundEffect("SpawnDisplaySpawnPointChange", "shell_select_change")
SetSoundEffect("SpawnDisplaySpawnPointAccept", "shell_menu_enter")
SetSoundEffect("SpawnDisplayBack", "shell_menu_exit")


--OpeningSateliteShot AddCameraShot(0.763622, 0.012544, -0.645455, 0.010603, 125.539291, 63.603477, -17.511637);
AddCameraShot(-0.198788, -0.005538, -0.979647, 0.027292, 149.981628, 64.837639, -60.428387);
AddCameraShot(0.005205, -0.001663, -0.952560, -0.304300, 139.273087, 99.218147, -62.060764);

end

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:54 pm
by THEWULFMAN
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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 9:59 pm
by lucasfart
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. :P
THEWULFMAN wrote: Yeah... no.
No need to be rude....

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:02 pm
by THEWULFMAN
lucasfart wrote:
THEWULFMAN wrote: Yeah... no.
No need to be rude....

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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:06 pm
by Maveritchell
THEWULFMAN wrote:Yeah... no.
Are you sure? Are you really sure? Don't jump on someone when you're not.
Noobasaurus wrote:Yes, but even then if it does apply in luas it does not have a 'then' next to the '='.
What we have here is a failure to communicate. (And understand!)

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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:15 pm
by Noobasaurus

Code: Select all

M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:76: `=' expected near `)'
It says I need this... Although I doubt if I put it in it will not work...

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:24 pm
by Maveritchell
[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.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:32 pm
by lucasfart
Noobasaurus wrote:

Code: Select all

M:\BF2_Modtools\data_GAA\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\GAA\GAAc_c1.lua:76: `=' expected near `)'
It says I need this... Although I doubt if I put it in it will not work...
As before it's probably linked to something else, as i doubt you need to put an "=" near the end of

Code: Select all

if acklay5 = 2 then
--SPAWN ACKLAY! YEZ!
SetProperty("acklay", "SpawnPath", "acklay_spawn")
StartTimer(acklay1)
DestroyTimer(timer)
end,
acklay
)
Although i have been known to be wrong on more then one occasion. :P

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:37 pm
by Noobasaurus
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.
Yes, after reading how you analyzed the first one, I thought that it would be very weird to put one over there. Testing now.

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 10:46 pm
by lucasfart
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...

Re: Chances of happening in a lua

Posted: Mon Nov 07, 2011 11:13 pm
by Noobasaurus
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...
Maveritchell wrote: in line six
In the whole timer script? Or starting from a certain point?

@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 `)'
Now I am going to bed.

Re: Chances of happening in a lua

Posted: Tue Nov 08, 2011 12:23 am
by lucasfart
Well the second you get up, try moving the bracket down a line. :P 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. :)

Re: Chances of happening in a lua

Posted: Tue Nov 08, 2011 12:47 am
by [RDH]Zerted
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:

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
 )
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.