SI's Story Mode, and LUA Scripting Tutorial Thread. -1 reply

  • 1
  • 2

Please wait...

Sidious Invader

Dark Lord Of The Sith

50 XP

9th January 2007

0 Uploads

853 Posts

0 Threads

#1 11 years ago

Your first Lua script.... [COLOR=black]The purpose of this is to help get you familar with proper syntax, and the basics behind the .lua programming language as it applies to EAW/FOC. I use notepad, so lets open it up and begin...[/COLOR] [COLOR=black]All lines that begin with "--" in LUA programming language are comments, and the game engine compilier will ignore them. They are there for the programmer to remind himself, and others, what he is doing. So we'll begin by giving our script a description, and instructions to the user....[/COLOR] [COLOR=darkred]--Landing X-Wing eyecandy by Sidious Invader. 9/28/07.[/COLOR] [COLOR=darkred]-- Please use in your mod, but give me credit in the readme.[/COLOR] [COLOR=black]Since alot of the functions in this game are re-used in other scripts, a library was put together by Petroglyph found in the swfoc/mods/source/data/scripts/library/ folder. Most of your scripts will include accessing these library files. As part of the process of learning LUA it is highly recommended that you look at the library files for the functions that are available to use in your code. This is the proper syntax for including a library file for your script to access, put this in your LUA file now...[/COLOR] [COLOR=darkred]require("PGStateMachine")[/COLOR] [COLOR=darkred]require("PGStoryMode")[/COLOR] [COLOR=black]Now every LUA script will always begin with a "Function Definitions()" thread. This is a chance to assign variables, create lists, and tell the script where to go next. The "DebugMessage()" is just an initialization code, and should be in every Definitions function. The "Define_State" command will tell the script what function to perform next, or if there are more than one "Define_State" command, in which order they should be performed. In this case there will be only one. You will notice that there is two "State_Init" in the parenthesis. One is the function name, the other is the name referenced in the Story.xml(explained later.) The "lander_list" is defining a list of objects that will be used by a command later. In this script we only have one object, an X-Wing. Complete every Function with an "end" statement. [/COLOR] [COLOR=darkred]function Definitions()[/COLOR] [COLOR=darkred]DebugMessage("%s -- In Definitions", tostring(Script))[/COLOR] [COLOR=darkred]Define_State("State_Init", State_Init);[/COLOR] [COLOR=darkred]lander_list = {[/COLOR] [COLOR=darkred]"Luke_Jedi_XWing"[/COLOR] [COLOR=darkred]}[/COLOR] [COLOR=darkred]end[/COLOR] [COLOR=black]Now, in the "Definition" function we told the script the next function is "State_Init." Assigning a variable to State_Init, called message, was just a easy way to code this function to automatically fire when the game starts. You'll see how this is more useful later. The first line is an if\then statement it means "if message is valid then do this..." Message is valid, because in definitions it called for this function to execute. Line two is assigning a variable(move_cycles) to equal 0. Line three assigns another variable(max_cycles) to 25. Typically variables are assigned in definitions, but you'll find some have to be in the same function in order to work. Line four assigns yet another variable(player) to the neutral player. Line five assigns a variable(spawnpoint) to an object. Line six assigns another variable(flyto) to another object. Now that the variables are set, we can begin the command part of this script. We'll get back to the "repeat" command in a second. Line eight is a command that spawns the list defined in definitions, and assigns it a variable name. If you take a look at it, it will make sense. Spawn Xwing at waypoint1 for the neutral player. Don't worry about the false, false in that command yet. Continued Below code.....[/COLOR] function State_Init(message) if message == OnEnter then move_cycles = 0 max_cycles = 25 player = Find_Player("Neutral") spawnpoint = Find_First_Object("WAYPOINT1") flyto = Find_First_Object("WAYPOINT2") repeat flyby = SpawnList(lander_list, spawnpoint, player, false, false) for k,unit in pairs(flyby) do unit.Set_Selectable(false) unit.Prevent_AI_Usage(true) unit.Move_To(flyto) Sleep(13) unit.Play_Animation("takeoff", false) Sleep(5) unit.Despawn() end until move_cycles > max_cycles end end [COLOR=black]... The "for k,unit in pairs(variable)" command is used for moving listed groups around. With the variable "flyby," being the Spawnlist we defined above. We use the next commands to make it unselectable, and prevent AI targeting. The next command "unit.Move_To(flyto)" with the variable "flyto" defined as the waypoint2 marker. We put the script to sleep for a few game seconds to allow the X-wing to fly to it's waypoint. Then it will play it's "takeoff" .ala animation, putting to sleep the script for a few seconds to give the xwing a few seconds to move above the screen. Then we despawn the fighter. Now the final command is the "until" command this loops this script back to the "repeat" command to continually have the xwing fly by from the spawnpoint marker to the flyto marker. Since we already defined move_cycles, and max_cycles, this script will always loop. I can keep it from looping forever if I put the command "move_cycles = move_cycles + 1," in front of the "until" command line. This increments the move_cycles by 1 each pass in the script, until it is greater than max_cycles, or 25. Now lastly you need to understand the 3 "end" statements. This closes a function, if\then, or for k statement. So looking above "Function" has an end, "If" has an end, and "for k" has an end.[/COLOR] So hopefully your still with me, everything in red should be in your .lua file. Save it to data\scripts\gameobjects\xwingflyby.lua We need two markers, so heres the code.... [COLOR=darkgreen][/COLOR] [COLOR=darkgreen] TEXT_NONE [/COLOR] [COLOR=darkgreen]yes[/COLOR] [COLOR=darkgreen] Yes [/COLOR] [COLOR=darkgreen] W_FLAG_MARKER_PURPLE.ALO [/COLOR] [COLOR=darkgreen]false [/COLOR] [COLOR=darkgreen]Neutral[/COLOR] [COLOR=darkgreen]true[/COLOR] [COLOR=darkgreen] MARKER, HINT [/COLOR] [COLOR=darkgreen]Xwingflyby[/COLOR] [COLOR=darkgreen][/COLOR] [COLOR=darkgreen][/COLOR] [COLOR=darkgreen] TEXT_NONE [/COLOR] [COLOR=darkgreen]yes[/COLOR] [COLOR=darkgreen] Yes [/COLOR] [COLOR=darkgreen] W_FLAG_MARKER_PURPLE.ALO [/COLOR] [COLOR=darkgreen]false [/COLOR] [COLOR=darkgreen]Neutral[/COLOR] [COLOR=darkgreen]true[/COLOR] [COLOR=darkgreen] MARKER, HINT [/COLOR] [COLOR=darkgreen][/COLOR] [COLOR=black]Put these two markers in the markers.xml, and save it to data\xmls\.[/COLOR] Now, open your favorite map in the map editor, and place the waypoint1 marker at one corner of the map, and waypoint2 at the other side. While playing the map you will have an X-wing skimming the surface, during your land battle. NOTE: I only put the play animation command in there so you can see a few possibilites of LUA editing, if it does not look good to you. Just have the xwing fly back to waypoint1 using the unit.Move_To(spawnpoint) command. Other tutorials coming soon.... Cinematic scenes, Pop up text windows, story mode scripting for custom campaigns, intro story text scrolls, LUA function list, playing movies.




codeuser

I follow teh Moo!

50 XP

13th November 2005

0 Uploads

708 Posts

0 Threads

#2 11 years ago

Great tutorial!

I'm sure it will help people with LUA.




17th April 2007

0 Uploads

1,593 Posts

0 Threads

#3 11 years ago

Great Tut this needs sticky and put in to the master sticky file




Sidious Invader

Dark Lord Of The Sith

50 XP

9th January 2007

0 Uploads

853 Posts

0 Threads

#4 11 years ago

Sorry, I just noticed an error! When I first made this script I used an X-wing unit coded for ground battles. So here is the code put in your groundvehicles.xml. And change the lander_list from "luke_jedi_xwing" to "X_Wing_Fighter_Land_squad..." 240.0, 40.0Generic_ShadowTEXT_UNIT_X_WING_SURFACE Y-Wing Z-95_Headhunter_Rebel AT_AA_Walker UC_Empire_Buildable_Anti_Aircraft_Turret TEXT_TOOLTIP_X_WING_SURFACETEXT_ENCYCLOPEDIA_CLASS_FIGHTER23.0rv_xwing.ALO0.98581.2154067.06.5 .2 .2 6.56.51080 Frigate Land Solo 4 125.0125.015.0 false 2000SELECTABLE, POWEREDHUNT, ABILITY_COUNTDOWN, FLYING_LOCOMOTOR,REVEAL,HIDE_WHEN_FOGGED, AMBIENT_SFX,UNIT_AI,TARGETING,WEAPON,STUNNABLE, WIND_DISTURBANCE,DAMAGE_TRACKINGFalseSpeeder Yes Rebel0 Armor_Speeder 2840.00190010009990.0500.0400.020.050.050.0Proj_Ship_Small_Laser_Cannon_Red1.080.2Infantry, 50.0Vehicle, 3.0Air, 5.0Structure, 1.0P_Smoke_Small02.aloSmall_Explosion_Space_EmpireYes0.22.0fSmall_Explosion_Space_EmpireUnit_TIE_Fighter_Spinning_Bytrue10050.1Unit_X_Wing_Fire Unit_Select_X_WingUnit_Move_X_WingUnit_Fleet_Move_X_WingUnit_Attack_X_WingUnit_Guard_X_WingUnit_Assist_Move_X_WingUnit_Assist_Attack_X_WingUnit_X_Wing_Death_SFX Unit_X_Wing_Fly_By 5 10 Unit_X_Wing_Cinematic_Engine_Loop RHD_Unit_Destroyed RHD_Reinforcements_En_Route RHD_Reinforcements_Available RHD_Reinforcements_Cancelled i_button_X_wing.tga Air | AntiStructure Flying 1x1yes1111 Yes Yes300.0300.0300.05.0AntiVehicle200.0 Solo FlyingLuke_Jedi_Xwing_Landing50HUNTTEXT_UNIT_X_WING_SURFACETEXT_TOOLTIP_X_WING_SURFACE Rebel_Trooper Plex_Soldier X_SPEEDER SNOWSPEEDER UC_Rebel_Buildable_Anti_Aircraft_Turret 1 Yes2440Rebel100025 Yes YesNoNo20001010R_Ground_Light_Vehicle_FactoryDUMMY_GROUND_COMPANY3yeswalkerGallofree_TransportX_Wing_Fighter_Land, X_Wing_Fighter_Land, X_Wing_Fighter_Landi_button_X_Wing.tgaRV_XWing.ALO950 0 045RHD_Build_VehicleRHD_Unit_CanceledUnit_Complete_X_Wing225100020Tactical_UnitsTEXT_ENCYCLOPEDIA_CLASS_FIGHTER0.050




nason

thats me

50 XP

3rd September 2006

0 Uploads

734 Posts

0 Threads

#5 11 years ago

sweet this can help people




Big J Money

Galactic Fool

50 XP

2nd September 2007

0 Uploads

16 Posts

0 Threads

#6 11 years ago

This is awesome stuff! Will be used. =$= Big J Money =$=




general_kerr

(Couldn't find a title)

50 XP

14th February 2007

0 Uploads

2,089 Posts

0 Threads

#7 11 years ago

I'll look over this in detail later, PM me and i'll give you a link to my tutorial forums, so you can post this there. :)




general_kerr

(Couldn't find a title)

50 XP

14th February 2007

0 Uploads

2,089 Posts

0 Threads

#8 11 years ago

HELP! All of my FoC lua's are compiled, i can't edit them! How can i use them? (sorry for double post)




Sidious Invader

Dark Lord Of The Sith

50 XP

9th January 2007

0 Uploads

853 Posts

0 Threads

#9 11 years ago

The LUA's in your swfoc\mods\source\data\scripts\ folder are not compiled, the ones in your .meg files are.




general_kerr

(Couldn't find a title)

50 XP

14th February 2007

0 Uploads

2,089 Posts

0 Threads

#10 11 years ago

Crap, i loaded all of the .meg files over the source folder..........




  • 1
  • 2