Jump to content

Lua Tutorial/Information


  • Please log in to reply
22 replies to this topic

#1 paws1111

paws1111

    Acolyte

  • Members
  • Pip
  • 24 posts

Posted 18 November 2006 - 09:13 PM

sorry for bad spelling but i just updated spelling a little :D
ah shoot the spell checker screwed up my formating!


Ok i had some people ask me about about LUA and.... i thought its about time i share what i have learned about it......
..em..... yea..... i should release this as a download but im to lazy right now plus if there are any other lua coders out there they can add to the list......though i don't think theres more than 3 or 4 of us i sware.

a few extra quick notes about lua
  • Warning Debugging lua is a PAIN in the butt if you do something wrong 90% of the time the script wont run but the game will. il give a few tips at the end for debuging.
    |There IS a debug console for scripts (not normal eaw console) but we cant access it (saw it with a resource hacker once )

  • Variables do not have a defining type (eg not " int somenumber =12" but "somenumber = 12") lua automatically changes variable types

  • Im not sure how the how global variables work i think you need the "require" command to show where to get the variable

  • Comments are defined by a " -- " infront of them there are no brackets or end line statments (like ; in java)



  • quick if statment format ......
    If some expression then
    some code
    else
    some code
    end
hear are extra variable defining things because i dint include them in my original list....but there important
Find_First_Object("")

Find_Hint("") -- finds object with hint located ( YES thats what that is for in map editor )

there are more but i dont fell like finding them



i spelled check this stuff just for you guys

Quote

i know a few commands in lua are easily explanatory but some
aren't known causing us to look through diferent missions
but im gona put them in this list

Lua notes:
1.Variables seem to be global throughout the script and posibly throught all scripts

2.When scripting and you update the script it seems you need to restart the
map (not the game) for them to update (at least in my experiences

3. On many petroglyph scripts you will notice whats called state messages
   (eg: onEnter , OnUpdate , OnExit) these reprent the state of which the
  script is in.... (entering the script.... updating the script.... exiting the script)
90% sure thats right


warning most of these are guses for functions
functions list-----

Find_All_Objects_Of_Type(object)
Replace with object with a type of unit seems to be the name in the xml
Use this to define your object variables

.Activate_Ability("Turbo", false)
you can use this to activea unit abilitys by script! good for AI

Letter_Box_In(int)
WHAT? possibly the size of the wide screen view....

Sleep(int)
This seems to pause current function

MessageBox("string")
string = some text u type in there :) good for gebuging
--UPDATE!!! I know how toget this working.... more details in future
                (cant get working btw)

Fade_Screen_In(int)
cool..... self explanatory

Find_player("factionname")
faction name = name. where name is a faction defined in xml I think
used for putting used factions to variables

.Make_Invulnerable(true)
-- can't be killed
.Prevent_All_Fire(true)
-- can't shoot (not sure if this is for whole level?)
.Set_Selectable(false) -- can't be controlled by player
for these three above before the dot u need a unit variable


.play_animation("deploy", false)
not sure how to use it but its usefull
needs indicator infront of dot like prox_object (somehow defined somewhere
else in code not sure where) and deploy could be a name of animation but i
could be wrong

.Change_Owner(String)    -updated
string = defined some where else in La ie: Rebel_player = Find_player("rebel")
in string you would have rebel_player
needs identifiers like prox_obj

.Despawn()
despawns defined object

Spawn_Ambush_Squad("spawn1",spawn1)
Thats exactly how i found it.... i need to research into these more
note to self: Found in Story_Rebel_ActIII_M09_LAND only known instance... interesting...

.Set_Selectable(boolean)
Self explanitory

.Set_Garrison_Spawn(boolean)
Enables or diables the building garrison even if its multiplayer :D
.Give_Money(int)
Gives cash!! :)


.Retreat() -- WARNING NOT UNIT COMMAND
In front you put a player variable causes all units to retreat
from that player
.Hyperspace_Away()
Unit command : makes unit hyperspace away(probly space only)
                (does not naturally "retreate " just looks like it used in sigleplayer()
/----------------------------------------------------------------/
With an object variable before the dot you can give units commands via scripts!
i tryd to keep them all hear
\---------------------------------------------------------------/
.Attack_Move()
moves unit to a location(defined with a marker) or object and attacks it
dosint attack friendlys but does attack marker flags
.Move_To()
self explanitory
.Select_Object()
Forces user to temporarily (or if set up right perminatly :D)

.Take_Damage(int)
Well.... maybe not a unit command but still useful

.Override_Max_Speed(int)
Self explanatory... this command looks like so much fun!
(haven't used it >.<)
.Stop()
self explanitiory



/----------------------------------------------------------------/
These commands bellow have to do with cinimatic cammera
\---------------------------------------------------------------/

Scroll_Camera_To(object)
Replace object with some object variable can be markers (markers are objects)
and watch it fly!!

Point_Camera_At(cine_start)
needs to be pointed at an object

Start_Cinematic_Camera()
Self explanatory doesn't seem to have any thing acociated with it

------ These commands have 1 for on 0 for off----

Suspend_AI(1)
self explanitory

Lock_Controls(1)
self explanitory

Examples:

This IS NOT MINE its Petroglyphs but its a great starting script to learn from


-- $Id: //depot/Projects/StarWars/Run/Data/Scripts/GameObject/CreditPowerUp.lua#5 $
--/////////////////////////////////////////////////////////////////////////////////////////////////
--
-- © Petroglyph Games, Inc.
--
--
--  *****           **                          *                   *
--  *   **          *                           *                   *
--  *    *          *                           *                   *
--  *    *          *     *                 *   *          *        *
--  *   *     *** ******  * **  ****      ***   * *      * *****    * ***
--  *  **    *  *   *     **   *   **   **  *   *  *    * **   **   **   *
--  ***     *****   *     *   *     *  *    *   *  *   **  *    *   *    *
--  *       *       *     *   *     *  *    *   *   *  *   *    *   *    *
--  *       *       *     *   *     *  *    *   *   * **   *   *    *    *
--  *       **       *    *   **   *   **   *   *    **    *  *     *   *
-- **        ****     **  *    ****     *****   *    **    ***      *   *
--                                          *        *     *
--                                          *        *     *
--                                          *       *      *
--                                      *  *        *      *
--                                      ****       *       *
--
--/////////////////////////////////////////////////////////////////////////////////////////////////
-- C O N F I D E N T I A L   S O U R C E   C O D E -- D O   N O T   D I S T R I B U T E
--/////////////////////////////////////////////////////////////////////////////////////////////////
--
--              $File: //depot/Projects/StarWars/Run/Data/Scripts/GameObject/CreditPowerUp.lua $
--
--    Original Author: James Yarrow
--
--            $Author: Dan_Etter $
--
--            $Change: 24235 $
--
--          $DateTime: 2005/08/18 16:56:17 $
--
--          $Revision: #5 $
--
--/////////////////////////////////////////////////////////////////////////////////////////////////

require("PGStateMachine")


function Definitions()

   -- Object isn't valid at this point so don't do any operations that
   -- would require it.  State_Init is the first chance you have to do
   -- operations on Object
  
DebugMessage("%s -- In Definitions", tostring(Script))

Define_State("State_Init", State_Init);

player = nil
end


function State_Init(message)

if message == OnEnter then
-- Set up event handling for when an object moves within range
Object.Event_Object_In_Range(object_in_range_handler, 150)
                                    --Paws note that was added: this not the best way of doing object in range
                                    -- usualy beter to do Register_Prox()

elseif message == OnUpdate then
-- Do nothing
elseif message == OnExit then
-- Do nothing
end

end

function object_in_range_handler(prox_object, object)

player = object.Get_Owner()
if player.Is_Human() then
object.Get_Owner().Give_Money(1000)

-- Cancel the object in range event from signaling anymore.
Object.Cancel_Event_Object_In_Range(object_in_range_handler)

Object.Despawn()
end

end


--------------------------
my example:

This is NOT FUNCTIONAL i dont know what hapend to the functioning versian
might be that i had a glitch and started commenting out all the parts

and this script wont work without the proper xml  ask me for it if you want it
--------------------

--*****************************************************
--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
--Paws Scipt:
-- Chronosphere script 0.o :
--
--
--
--
--////////////////////////////////////////////////////////
--*****************************************************
require("PGStateMachine")
--require("PGStoryMode")



function Definitions()
Define_State("State_Init", State_Init);

--1 for on 0 for off...and we start off (animations are not yet included)
Chrono_state=1
--for now no timer between jumps for simplisity and testing perpuses
--
--this is slightly misleading the range is the range around the cronospher to teleport troops
-- yousualy you want this size to be about the size of building of little bigger
range =150



Cronosphere = Find_First_Object("Rebel_Command_Center")
    Cronosphere.Set_Selectable(false)
end


function State_Init(message)

if message == OnEnter then
Cronosphere.Set_Selectable(false)
--becon = Find_First_Object("R_Multiplayer_Beacon")
--timer if statement would go hear
--testing if becon is on the map
--if not TestValid(becon) then
-- If its not valid do nothing
--else
-- The player has summand the cronosphere! Move out!
--chrono_state =1
--end
--if chrono_state == 1 then

--Register_Prox(Object, object_in_range_handler, range, rebel_player)
-- Animation placeholder... thing in quote is animation name. then the false and 0.....
-- no idea what thoughs are for
--self_obj.Play_Animation("cinematic", false, 0)
-- end animation at spawning location
--becon.Play_Animation("cinematic", false ,0 )
--end

`elseif message == OnUpdate then
elseif message == OnExit then
-- Do nothing
end

end

function object_in_range_handler(self_obj, trigger_obj)

--ok this part of script interigates something i havent tryd before :)
               -- trigger_obj.Set_Selectable(false)
-- this is the lazzy way to teleport.... its.....to instant
-- could b e bad for animations that later i will want to play :D
--trigger_obj.Teleport(becon)



end


------------------------------------------
Debugging tips
--------------------------------------------

*Comment out code that seems suspecius if your codes not worrking

* if your not shure if your object is found or not do  (your object hear).Set_Selectable(false)
and if you cant select it the game found the object

* if your haveing problems with number varibles (like i was for my leveling up script in production)
use     (playergoeshear).Give_Money( varible your haveing trouble with) and check the difrence in money each time script sould run )

EDIT!!!!

AH FORGOT VERY IMPORTANT THING ABOUT useing scripts..... to add them to the game you add it through xml..... some of you might know it
its
<Lua_Script>scriptnamegoeshear</Lua_Script>

and you put the lua script in the data/Scripts/* folder the * means you can put it in that directory (not shure) but i recoment the data/Scripts/GameObject folder or data/Scripts/Story
(yes you have to make the folders your self)
also to note that the file must have the .lua tag at end for it to work

also to note that scripts in FOC are encoded probly untel they releace the editor
BUT the demo of foc's scripts are not



(some new foc command it hink will make diplomatsy posible!!!that was in demo)



end edit



and that sums it up for now post any questions you might have :D good luck learning it :D

Edited by Berek-PG, 04 August 2009 - 11:48 AM.


#2 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 18 November 2006 - 09:28 PM

Sticky this shit. This is awesome.

Edited by (DoV) Tokakeke, 18 November 2006 - 09:28 PM.


#3 mastermind

mastermind

    The Wizard

  • Retired Staff
  • PipPipPipPip
  • 597 posts
  • Gender:Male
  • Location:Cambridge, MA

Posted 18 November 2006 - 11:48 PM

Moved to a better forum and pinned.

Posted Image


Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them. - John Hodgman

#4 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 19 November 2006 - 12:33 AM

Question - these functions all seem Petro defined. Is there a standard library that we can use? Or does LUA not work that way?

#5 paws1111

paws1111

    Acolyte

  • Members
  • Pip
  • 24 posts

Posted 19 November 2006 - 12:49 AM

View Post(DoV) Tokakeke, on Nov 19 2006, 05:33 AM, said:

Question - these functions all seem Petro defined. Is there a standard library that we can use? Or does LUA not work that way?
hm.... you mean that petroglyph created the commands? yea i think so but it makes sence why they would because realyhow could the creaters of lua know what the game will need? theres probly some ..... i think there was a few that whernt but its hard to tell

also to note some commands are built into the engine and others are done through other lua scripts

Standard library? im not shure i understand 100% what you mean..... like an archive of diffrent functions and such? no...... thats why i posted my list althogh in petroglyphs dark basment i bet they have alist some where
eh  though..... petroglphs  library folder is that actily if u look at the scripts there functions that are caled its kind of a library of commands if you read it other than that no list of commands

if you start actuly programing in lua alot you sould create a list of usefull commands you used (that you puled from petroglyph lua .... lots of reading to find things) and put it as your own list like i did

usuly the commands are easy to find for example the hyperspace away command

i went through each storyline lua and did a notepad search with words "hyperspace" untel i found it and loandbbehold i found it :D

Edited by paws1111, 19 November 2006 - 12:51 AM.


#6 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 19 November 2006 - 01:11 AM

What I meant is this. In C++, you can define functions like that, but you'll always have a standard library of stuff like outstream and such. Is it possible to use any sort of library of LUA functions, or is the scripts limited to only functions that Petro create?

#7 Mike.nl

Mike.nl

    Mad Computer Scientist

  • Global Moderator
  • 1,611 posts
  • Gamertag:RationalMike
  • Fav. Game:
  • Gender:Male
  • Location:The Netherlands

Posted 19 November 2006 - 04:25 AM

No, I don't think so.
I've tried to use stdlib functions in Lua once, and it didn't work.

And BTW, the OnEnter, OnUpdate and OnExit functions are called from PG's State Machine implementation, which you set up with Define_State.

The Definitions() function is called once when the script is loaded, so you can see that as your script's entry point where you can set up handlers, etc.

And about all that stuff about Lua's syntax; just tell people to read the Lua Manual. I would think we're easier off just figuring out which functions we can use from Lua in EaW and FoC (the API). If people want to learn Lua in general, there are other sites for that.


Oh and paws1111, please, please pay a bit more attention to spelling and grammar.
"sence"? "hear"? "realyhow"? "loandbbehold"?

It would make your posts much more pleasant to read ;)
Million-to-one chances occur nine times out of ten!

#8 Droid

Droid

    Petrotopian

  • Members
  • PipPipPipPip
  • 974 posts
  • Gamertag:Droid803
  • Location:Bring Cash (BC, Canada)

Posted 19 November 2006 - 01:00 PM

Yes this is helpful.
Doesn't tell much about scripting for campaign though...
well thats a lot more simple - naming units in the map editor, referencing them in the LUA, telling them what to do...etc...

Yes, does ANYONE have a LUA debugging tool?
Its REALLY a pain in the ass.
Posted Image
Posted Image

#9 Mike.nl

Mike.nl

    Mad Computer Scientist

  • Global Moderator
  • 1,611 posts
  • Gamertag:RationalMike
  • Fav. Game:
  • Gender:Male
  • Location:The Netherlands

Posted 19 November 2006 - 02:48 PM

You can proofcheck your lua syntax by compiling it with the normal lua compiler (http://www.lua.org).

That should catch most errors. Any errors that will be left when you put the lua in the game afterwards are just logic errors.
Million-to-one chances occur nine times out of ten!

#10 Swgbex

Swgbex

    hmm.....

  • Retired Staff
  • PipPipPipPip
  • 890 posts
  • Gamertag:swgbex
  • Gender:Male
  • Location:California

Posted 19 November 2006 - 11:53 PM

I would advise mike to stop editing tutorials ^^
Nice job btw
Posted Image

#11 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 29 November 2006 - 05:03 PM

Do the LUAs have to be compiled first? Or can you just plunk them in the /scripts/ folder?

#12 Swgbex

Swgbex

    hmm.....

  • Retired Staff
  • PipPipPipPip
  • 890 posts
  • Gamertag:swgbex
  • Gender:Male
  • Location:California

Posted 29 November 2006 - 10:20 PM

No they dont have to be, notice the source EAW" ones are not compiled and can be edited and used in game without any other tool, the game compiles them to my knowledge
Posted Image

#13 Droid

Droid

    Petrotopian

  • Members
  • PipPipPipPip
  • 974 posts
  • Gamertag:Droid803
  • Location:Bring Cash (BC, Canada)

Posted 29 November 2006 - 11:45 PM

The FoC ones need to be decomplied. Trust me I've edited them plain. didn't work.
Posted Image
Posted Image

#14 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 29 November 2006 - 11:47 PM

If they're compiled, I would imagine it would be difficult to edit them, unless you enjoy assembly code.

#15 Swgbex

Swgbex

    hmm.....

  • Retired Staff
  • PipPipPipPip
  • 890 posts
  • Gamertag:swgbex
  • Gender:Male
  • Location:California

Posted 29 November 2006 - 11:51 PM

yeah, FoC hasnt release mod tools YET.. soon enough you will get pre decompiled LUAs :p.
Hopefully
Posted Image

#16 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 30 November 2006 - 12:03 AM

just decompile them with a LUA decompiler

#17 Swgbex

Swgbex

    hmm.....

  • Retired Staff
  • PipPipPipPip
  • 890 posts
  • Gamertag:swgbex
  • Gender:Male
  • Location:California

Posted 30 November 2006 - 12:50 AM

I thought a normal LuA decompiler didnt work, (remember the ones from EAW didnt straight decompile, so mike made a tool that did)
Posted Image

#18 Sidious Invader

Sidious Invader

    Acolyte

  • Members
  • Pip
  • 37 posts

Posted 03 January 2007 - 03:41 PM

Hey Just read this, pretty cool. I am currently learning all of the Story mode scripting commands, and see the need to learn .lua, because some functions, and abilities are not possible with story mode scripting. Can you give me a link to a .lua decompilier. I tried one from lua.org, and got a Lua IDE, but it didn't work. Thanks in advance!

BTW- Letterbox mode is the tactical screen without the GUI command bars.

#19 (DoV) Tokakeke

(DoV) Tokakeke

    V-TEC JUST KICKED IN YO

  • Members
  • PipPipPipPip
  • 744 posts
  • Gender:Male

Posted 03 January 2007 - 04:10 PM

You want one for the story luas that aren't decompiled? I don't think regular LUA ones work for EaW LUAs.

#20 Sidious Invader

Sidious Invader

    Acolyte

  • Members
  • Pip
  • 37 posts

Posted 03 January 2007 - 04:24 PM

View Post(DoV) Tokakeke, on Jan 3 2007, 09:10 PM, said:

You want one for the story luas that aren't decompiled? I don't think regular LUA ones work for EaW LUAs.
Ok, Thanks. I'm just starting to get into this, this is pretty cool.

EDIT: Just a quick question, do they need to be compiled, or just placed in the right directory. I read above the game engine has a compilier, is that correct?

Edited by Sidious Invader, 03 January 2007 - 04:27 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users