Page 1 of 3

The fun .LUA commands thread

Posted: Sun Mar 20, 2011 5:51 pm
by asleeponduty
Hey all,
I noticed there are no individual threads which have useful/fun .lua commands in one place, so heres a few I figured out (mostly with help from the people here at Gametoast)
This topic would be beneficial to those getting familiar with .lua functions
Feel free to share other useful commands

Main Chunk: Kill object

Code: Select all

			    OnObjectKill(
    function(object, killer)
    if GetEntityName(object) == "OBJECTNAME" then
 -- put goodies here
    end
    end
    )
Main Chunk: Enter Region

Code: Select all

	OnEnterRegion(
    function(regIn,character)
   -- put goodies here
    end,
    "REGIONNAME"
    )
Goodies:

Kill an object:

Code: Select all

KillObject("OBJECTNAME")
Respawn an object:

Code: Select all

RespawnObject("OBJECTNAME") 
Old-School teleport (Need the teleport script at the end of the .lua for this)

Code: Select all

MoveEntityToNode(character,"PATHNAME")
Play an Animation:

Code: Select all

    PauseAnimation("ANIMGROUPNAME")  
 RewindAnimation("ANIMGROUPNAME") 
    PlayAnimation("ANIMGROUPNAME")
Activate a region (Need this for earlier main chunk)

Code: Select all

ActivateRegion("REGIONNAME")
Deactivate a region (To stop earlier chunk from executing)

Code: Select all

DeactivateRegion("REGIONNAME")
SetProperty Door unlock Example:

Code: Select all

SetProperty("OBJECTTHATISADOOR", "IsLocked", 1)
Give people a bunch of points (+5, but this depends on points.lua)

Code: Select all

AddAssaultDestroyPoints(character)
These are just a few examples, Feel free to post more

EDIT: All these commands go in the "ScriptPostLoad" section

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 6:34 pm
by THEWULFMAN
This is a great idea. While I don't want to give away all my secrets,here is one KinetosImpetus came up with.

Random CW ATT/DEF

Code: Select all

if not ScriptCB_InMultiplayer() then
CIS = math.random(1,2)
REP = (3 - CIS)
else
REP = 1
CIS = 2
end
Random GCW ATT/DEF

Code: Select all

if not ScriptCB_InMultiplayer() then
ALL = math.random(1,2)
IMP = (3 - CIS)
else
IMP = 1
ALL = 2
end
If you have local teams (like ewoks or wookies), you need to have them reference the REP/CISALL/IMP names instead of ATT/DEF. Otherwise they may attack the wrong team..

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 7:25 pm
by Fiodis
Before someone starts posting one-line LUA functions ("this is how you kill an object, and this is how you set a property") I'll just post this link: link. It's got a list of all LUA functions in BF2, though not their inputs.
Maveritchell wrote:Nice to see them all in one place! Now all we need to do is make a list of inputs for each of those. Nooo problem, yes? :P
If that's what comes of this thread, excellent. :) Unfortunately, that's quite a bit of work.

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 7:55 pm
by THEWULFMAN
I think GT can do it, what do you think asleeponduty? Think we can do this? Although, I am actually pretty bad at lua. :oops:

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 7:58 pm
by ACEwithSPADES
I think we can and ill help out

Code: Select all

                AddUnitClass(team,"unit class",least ammount, total) 
                SetUnitCount(2, 15)
            ATT_ReinforcementCount = GetReinforcementCount(ATT)
            SetReinforcementCount(ATT, ATT_ReinforcementCount + 10)
when addiint a unit in campaign or adding reinforcements! (just gettin started)

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 8:10 pm
by kinetosimpetus
THEWULFMAN wrote:
Hidden/Spoiler:
This is a great idea. While I dont want to give away all my secrets,here is one KinetosImpetus came up with.

Random CW ATT/DEF
[code]if not ScriptCB_InMultiplayer() then
CIS = math.random(1,2)
REP = (3 - CIS)
else
REP = 1
CIS = 2
end[/code]

Random GCW ATT/DEF
[code]if not ScriptCB_InMultiplayer() then
ALL = math.random(1,2)
IMP = (3 - CIS)
else
IMP = 1
ALL = 2
end[/code]

If you have local teams (like ewoks or wookies), you need to have them reference the REP/CIS/ALL/IMP names instead of ATT/DEF. Otherwisw they may attck the wrong team..
On another note...

Code: Select all

if ScriptCB_InMultiplayer() == 0 then
...
I'm not an expert at "lua", so idk if this syntax is better, worse, identical, or a necessary update, although, a quick change like this did fix a problem in an addme lua.

The original way doesn't make a munge error, but it could be a logic error that will always act as if it's true? I'm not sure...

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 8:28 pm
by THEWULFMAN
kinetosimpetus wrote:On another note...

Code: Select all

if ScriptCB_InMultiplayer() == 0 then
...
I'm not an expert at "lua", so idk if this syntax is better, worse, identical, or a necessary update, although, a quick change like this did fix a problem in an addme lua.

The original way doesn't make a munge error, but it could be a logic error that will always act as if it's true? I'm not sure...

I don't see a what the purpose is? Sorry to sound like a noob.

edit-So sorry, I got mixed up. I understood it all along, it was less complicated than I thought is all.

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 8:31 pm
by kinetosimpetus
It depends on whether the function returns true/false or it returns any-integer-other-than-0/0 and whether the "not" keyword can correctly reverse the second type of return.

Logically, they are the same, I just don't know enough about lua to know which one, or if both, is more correct.

Re: The fun .LUA commands thread

Posted: Sun Mar 20, 2011 9:04 pm
by asleeponduty
THEWULFMAN wrote:I think GT can do it, what do you think asleeponduty? Think we can do this? Although, I am actually pretty bad at lua. :oops:
This is doable, its not like theres an unlimited amount of things we can do

Re: The fun .LUA commands thread

Posted: Mon Mar 21, 2011 3:01 pm
by TK432
Objectives:

I know, a objective is a whole bunch of functions. But I explain each. And the existing objective are all spread all around.

CP's:

Capture CP:
Hidden/Spoiler:
--sets objective
Objective1CP = CommandPost:New{name = "cp1"}
Objective1 = ObjectiveConquest:New{teamATT = ATT, teamDEF = DEF, text = "level.ABC.obj.1", popupText = "level.ABC.obj.1_popup"}
Objective1:AddCommandPost(Objective1CP)

--tells what happens on start of your objective
Objective1.OnStart = function(self)


--sets AI-goal
AIGoalATT = AddAIGoal(ATT, "Defend", 10, "cp1")
AIGoalDEF = AddAIGoal(DEF, "Defend", 100, "cp1")

end

-- says what happens after the objective is complete
Objective1.OnComplete = function(self)

DeleteAIGoal(AIGoalATT)
DeleteAIGoal(AIGoalDEF)


end
Hold CP:
Hidden/Spoiler:
--dont oversee that teamATT = DEF and teamDEF = ATT here now
Objective1CP = CommandPost:New{name = "cp1"}
Objective1 = ObjectiveConquest:New{teamATT = DEF, teamDEF = ATT, text = "level.ABC.obj.1", popupText = "level.ABC.obj.1_popup", timeLimit = 60, timeLimitWinningTeam = ATT}
----------------------------------------sets timelimit^^

Objective1:AddCommandPost(Objective1CP)

Objective1.OnStart = function(self)

AIGoalATT = AddAIGoal(ATT, "Defend", 100, "cp1")
AIGoalDEF = AddAIGoal(DEF, "Defend", 10, "cp1")

end

Objective1.OnComplete = function(self)

DeleteAIGoal(AIGoalATT)
DeleteAIGoal(AIGoalDEF)

--sets that you lose if you dont hold it
if self.winningTeam == self.teamDEF then
BroadcastVoiceOver("cor_obj_24")
MissionDefeat(ATT)
elseif self.winningTeam == self.teamATT then
ShowMessageText("game.objectives.complete", ATT)
BroadcastVoiceOver("cor_obj_05")
end

end
Destruction:

Destroy 2 Targets:
Hidden/Spoiler:
--This needs to be defined before the actual objective
MainframeString = "level.myg1.obj.c2-"
--copied out of mygeeto campaign, this sets text ("2 or 1 remaining")

--This defines the props, can be in the objective, too
Yourprop = Target:New{name = "abc_prop_test"}
Yourprop1 = Target:New{name = "abc_prop_test1"}


Objective1 = ObjectiveAssault:New{teamATT = ATT, teamDEF = DEF, text = "level.ABC.objectives.1", popupText = "level.ABC.objectives.1_popup"}


--Sets math for text "2 or 1 remaining" we defined above
Objective1.OnSingleTargetDestroyed = function(self, target)
local numTargets = self:GetNumSingleTargets()
if numTargets > 0 then
ShowMessageText(MainframeString .. (numTargets + 1), 1)
ScriptCB_SndPlaySound("MYG_obj_04")
end

Objective4.OnStart = function(self)

--adds the props as target
Objective1:AddTarget(Yourprop)
Objective1:AddTarget(Yourprop1)

--adds circle to minimap (optional)
MapAddEntityMarker("abc_prop_test", "hud_objective_icon_circle_circle", 4.0, ATT, "YELLOW", true)
MapAddEntityMarker("abc_prop_test1", "hud_objective_icon_circle_circle", 4.0, ATT, "YELLOW", true)

--ai goals
ATT = AddAIGoal (ATT, "Defend",50,"abc_prop_test")
DEF = AddAIGoal (DEF, "Defend",20,"abc_prop_test1")


--sets health for your props
SetProperty("abc_prop_test", "MaxHealth", 1000)
SetProperty("abc_prop_test1", "MaxHealth", 1000)


end

Objective1.OnComplete = function(self)

MapRemoveEntityMarker("abc_prop_test")
MapRemoveEntityMarker("abc_prop_test1")
DeleteAIGoal(ATT)
DeleteAIGoal(DEF)

end
Defend a Target:
Hidden/Spoiler:
Objective1 = ObjectiveAssault:New{teamATT = DEF, teamDEF = ATT, text = "level.ABC.objectives.Campaign.1",
popupText = "level.ABC.objectives.Campaign.1_popup",
timeLimit = 120, timeLimitWinningTeam = ATT}
--timer^


--defines your target
defendprop = Target:New{name = "tst_prop_test"}

--adds your target
Objective1:AddTarget(defendprop)

Objective1.OnStart = function(self)

--sets health of your prop
SetProperty("tst_prop_test", "MaxHealth", 6000.0)
SetProperty("tst_prop_test", "CurHealth", 6000.0)

--adds marker
MapAddEntityMarker("tst_prop_test", "hud_objective_icon", 3.0, 1, "YELLOW", true)


-- add goals to kill them
-- and let the good guys defend a little, otherwise they stay around the object too much
DEF = AddAIGoal(CIS, "Destroy", 1000, "tst_prop_test");
ATT = AddAIGoal(REP, "Defend", 50, "tst_prop_test");
end

Objective1.OnComplete = function(self)

DeleteAIGoal(ATT)
DeleteAIGoal(DEF)

if self.winningTeam == self.teamDEF then
BroadcastVoiceOver("cor_obj_24")
MissionDefeat(ATT)
elseif self.winningTeam == self.teamATT then
ShowMessageText("game.objectives.complete", ATT)
BroadcastVoiceOver("cor_obj_05")
end


--adds reinforcements
ATT_ReinforcementCount = GetReinforcementCount(ATT)
SetReinforcementCount(ATT, ATT_ReinforcementCount + 25)
end
end
Can be with multiple targets aswell just look it up in "Destroy 2 targets"


Misc


Ambush:

Triggered Ambush (on entering a region):

Code: Select all

SetupAmbushTrigger("regionname", "pathname", 2, 3)
2 is the number of dudes. 3 is the team

Instant Ambush (Good for kill objectives):

Code: Select all

Ambush("pathname", 2, 3)
Again, 2 is the number of dudes. 3 is the team

Setting up teams (just important if you don't know how):
Hidden/Spoiler:
is simple. Just add this below the existing side definitions:
[code]teamname = 3
TST = teamname
--^^ 3-letter name for team (no own side needed)[/code]
next just add units to team, again below existing ones:
[code] tst = {
team = TST,
units = 3,
reinforcements = 3,
soldier = { "imp_inf_rifleman",2, 3},
},[/code]
and finally add enemies and friends below the part above:
[code] SetTeamName(TST, "wave1")
SetTeamAsFriend(TST, IMP)
SetTeamAsFriend(IMP, TST)
SetTeamAsEnemy(TST, ALL)
SetTeamAsEnemy(ALL, TST)
ClearAIGoals(TST)
AddAIGoal(TST, "Defend",100,"cp1_hunt")[/code]

Timer:
Hidden/Spoiler:
timername= CreateTimer("timername")
SetTimerValue(timername, (60)) --60 sekonds
StartTimer(timername)
--Leave the following line out if you dont want to see the timer in-game
ShowTimer("timername")
OnTimerElapse(
function(timer)
ShowTimer(nil)
--what happens after the timer expired
DestroyTimer(timer)
end,
timername
)
Let me know if I forgot something or have a mistake

Hope that helps for your campaign.

Beat this! :)

Re: The fun .LUA commands thread

Posted: Wed Mar 23, 2011 7:16 pm
by asleeponduty
Random Commands
(Commands to cause random events)

When I play a map, I want different things to happen every time I play it, be it the sky or the units, its really fun to have in place.

In this example, the name of the random event is Mousetrap, and it loads different sides.

Code: Select all

function ScriptPostLoad()
mousetrapvariable= math.random(1,3)  -- runs it once, with 4 options
mousetrap()   -- says the name of the command to run outside of ScriptPostLoad

--bla bla bla

end

function mousetrap()
if mousetrapvariable== 1 then
RReadDataFile("SIDE\\rep.lvl", "rep_inf_ep3_rifleman")
elseif mousetrapvariable== 2 then 
ReadDataFile("dc:SIDE\\DEL.lvl", "rep_inf_ep3_rifleman")
elseif mousetrapvariable== 3 then 
ReadDataFile("dc:SIDE\\BOB.lvl", "rep_inf_ep3_rifleman")
end
end  
*NOTE: each of the sides has units of the same name, so REP, DEL, and BOB all have rep_inf_ep3_rifleman units. this is so it will not go spastic when it sets up teams.

You can do the same with weather! (Suprise!)
At this link is the tutorial: Random Sky/FX/Lighting tut

EDIT: I found some more fun stuff, and this stuff actually is fun to kill people with:

Make the Locals your enemy if you shoot them:
*the locals are on team 3

Code: Select all

functoin ScriptPostLoad()
--bla bla bla
OnObjectDamage(function(object,attacker)
    --cancel if there was no attacker (maybe fell off a tower...)
    if attacker == nil then return end

    --cancel if the damaged object is not on the 3rd team
     if GetObjectTeam(object) ~= 3 then return end

    --3rd team is now an enemy of the attacker's team
    SetTeamAsEnemy(3,GetObjectTeam(attacker))
end)

--bla bla bla
end
EDIT1: The Blas have been de-blaad, tks wulfman

Re: The fun .LUA commands thread

Posted: Wed Mar 23, 2011 8:58 pm
by THEWULFMAN
I would edit those bla bla bla sections to be like //bla bla bla

I don't want to see a topic start, about some with a CTD, only to find out that they did not remove the bla bla bla from their script. :plokoon:

Re: The fun .LUA commands thread

Posted: Wed Mar 23, 2011 9:38 pm
by asleeponduty
THEWULFMAN wrote:I would edit those bla bla bla sections to be like //bla bla bla

I don't want to see a topic start, about some with a CTD, only to find out that they did not remove the bla bla bla from their script. :plokoon:
That would be a pain...
Okay, I fix!

Re: The fun .LUA commands thread

Posted: Wed Mar 23, 2011 10:54 pm
by THEWULFMAN
asleeponduty wrote:
THEWULFMAN wrote:I would edit those bla bla bla sections to be like //bla bla bla

I don't want to see a topic start, about some with a CTD, only to find out that they did not remove the bla bla bla from their script. :plokoon:
That would be a pain...
Okay, I fix!

Much Thanks :plokoon:

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 8:12 am
by skelltor
THEWULFMAN wrote:This is a great idea. While I don't want to give away all my secrets,here is one KinetosImpetus came up with.

Random CW ATT/DEF

Code: Select all

if not ScriptCB_InMultiplayer() then
CIS = math.random(1,2)
REP = (3 - CIS)
else
REP = 1
CIS = 2
end
Random GCW ATT/DEF

Code: Select all

if not ScriptCB_InMultiplayer() then
ALL = math.random(1,2)
IMP = (3 - CIS)
else
IMP = 1
ALL = 2
end
If you have local teams (like ewoks or wookies), you need to have them reference the REP/CISALL/IMP names instead of ATT/DEF. Otherwise they may attack the wrong team..
What exactly would this do ingame? randomize the cp set the team starts with or what?

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 8:35 am
by kinetosimpetus
Yes.

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 8:39 am
by skelltor
Ok I tried it out on a map and it works great I might use this in bfsm if thats allright? Also what about vehicles won't this mess their spawning up?

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 9:24 am
by DarthD.U.C.K.
not if you set the vehiclenames for both Attacker and Defender e.g. ImpATK and ImpDEF .

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 9:36 am
by skelltor
? I am talking about on stock maps.

Re: The fun .LUA commands thread

Posted: Thu Mar 24, 2011 9:45 am
by DarthD.U.C.K.
yeah, you can check the vehiclespawns there with ZE, when the names are entered for both ARK and DEF it will work. otherwise you will have zo set this property via lua.