GravForce CnC MoDe
Would you like to react to this message? Create an account in a few clicks or log in to continue.
GravForce CnC MoDe

The Best CnC MoDe Around
 
HomeHome  SearchSearch  Latest imagesLatest images  RegisterRegister  Log inLog in  

 

 Tutorial - Lua, Making Code

Go down 
3 posters
AuthorMessage
Graviton
Recruit
Recruit
Graviton


Posts : 10
Join date : 2007-11-05
Age : 32

Tutorial - Lua, Making Code Empty
PostSubject: Tutorial - Lua, Making Code   Tutorial - Lua, Making Code Icon_minitimeWed Jul 09, 2008 12:52 am

Alright people. Some of you have lua, some of you know how to use it. For those that don't, the best way to learn is practice. Follow these instructions to get a few simple commands working, test it, and get one of the uppy-ups here to see what you're doing and offer ideas and suggestions. I would like to stress that you understand every line of the code so you can rewrite it, not re-copy it over and over. Good luck.

Okay, so just below is a wall code, similar to the one used in my C&C Scripts server. I will explain it piece by piece with pictures. Questions welcome.
Code:
if Message == "!build wall" then
local pos = Get_Position(Get_GameObj(pID))
if Get_Money(pID) < 50 then
InputConsole("ppage %d You need 50 credits for a wall...", pID)
else
pos:AssignX(pos:GetX()+2)
turret = Create_Object("M09_Rnd_Door", pos)
Set_Model(turret, "dsp_wall")
if turret == nil then
InputConsole("ppage %d Error with object!", pID)
else
Set_Money(pID, Get_Money(pID)-50)
end
end
end



The
Code:
if Message == "!build wall" then
part is basically saying this: If a chat message saying "!build wall" then it does whatever follows.

The
Code:
local pos = Get_Position(Get_GameObj(pID))
part is getting the player's location on the map with coordinates, a way to locate that point at any time. Look at this picture:
Tutorial - Lua, Making Code Obj
If the player is located at the white dot in the red circle that, hopefully, you can see...then that location is stored using numbers. The variable, 'pos', now has the value of your location. Variables are just something that have a name, like 'potato', and have a value of '5', or some other number. With Lua, you can use any word (within some limits) to store a value. This just makes it able to set whatever you are making near/on the player.

The
Code:
if Get_Money(pID) < 50 then
is just saying that "If the player's money is less than 50, do the next line."

The next line,
Code:
InputConsole("ppage %d You need 50 credits for a wall...", pID)
is doing a thing via (through) Renegade FDS (hopefully you know what this is, or you can't really use Lua). InputConsole is saying just that, "Put the next thing into the Console (Renegade FDS)." Parenthesis are just organizing the information, and the quotes are saying what to actually put in. If you typed in ppage <name> <message> into the blue Renegade FDS window, it would do a player page to that player. A player page is just like if you did "/page hohun123 Hi, how's it goin'?" in renegade. It is just a message from the host to the player. The %d is Lua shorthand for Digit, we will be coming back to this in one minute. After the %d is the message that will be sent to the player, namely "You need 50 credits for a wall..." Then it closes the quotes. That is the end of what is put into the FDS to send to the player. Next is a comma, seperating what to do and the declaration of variables. %d is now declared to be pID, which is the player's ID number such as 1, 2, 3... So essentially you would be paging the player number with the message. Confusing, so ask any clarification questions you want to.

Take a break, get up, stretch, then scroll down Wink

Okay, now the easy one:
Code:
else
is saying that "If the last 'if' statement was false, do the next lines." The last 'if' statement was
Code:
if Get_Money(pID) < 50 then
So if you had equal to or more than 50 credits it will do the next bit, which will be making the object.

This line,
Code:
pos:AssignX(pos:GetX()+2)
Says that "Take the players location and change it by __." It changes it by the number +2, but in one specific direction: X. X is the East/West line. A positive change would make it go East of the player's location. A negative,
Code:
pos:AssignX(pos:GetX()-2)
Would make it go West. In short, this line spawns the wall a little to the East of the player, so that he is not stuck in it.


The next line,
Code:
turret = Create_Object("M09_Rnd_Door", pos)
is a bit confusing. We will skip the first part, and start on Create_Object. This command says that it will make the thing next, which is in the parenthesis. It will make M09_Rnd_Door, again the quotes are for names. It will make it at the location 'pos, which we modified in the last line by adding 2 to the players location. So it will spawn the M09_Rnd_Door at position 'pos'. Back to the "turret =" part. This makes the variable 'turret' equal the object and location that is being created. This is used and explained more later.

One of the easiest lines to explain.
Code:
Set_Model(turret, "dsp_wall")
This changes the shape and look of the object made last line to the shape and look of "dsp_wall" which is, you guessed it, a wall.

The next line,
Code:
if turret == nil then
tests to see if the object created, 'turret', is "nil". Nil is like it doesn't exist, can't be found, or it just flat don't work. If it is "nil", then it does the next line.

Code:
InputConsole("ppage %d Error with object!", pID)
That is done if the turret is "nil". If it is "nil", then it pages the player with the message "Error with object!"

Once again, there is another 'else' line. This one says that if the turret is NOT "nil" then it will do the next line.

The next line is
Code:
Set_Money(pID, Get_Money(pID)-50)
Which subtracts 50 credits from the player's money.

The next three lines are 'end's. They end all of the 'if' statements. Each 'if' statement requires an 'end' or else your whole code is corrupt.

One main thing to keep in mind is that each if and else statement does not just execute the very next line, but it executes to an 'else' if it was an 'if' statement, and an 'end' if it was an 'else' or an 'if' without an 'else'. There are 'if' statements without an alternative, just an 'end' at the end of the statement. That way it only does something if it is true.








Now for a more advanced lesson on X, Y, Z placing, pos, and other location stuff.

If the player is at location R (red) in
Tutorial - Lua, Making Code Obj
and you want to place an object North of him like in
Tutorial - Lua, Making Code ObjN
how would you do it? Use X or Y? Positive or negative? The answer is Y, and Positive. So you would use a code like
Code:
pos:AssignY(pos:GetY()+n)
where n you would replace with any number. This would place whatever object you wanted North of the player by a set amount.

Oh, but wait...What if you want it East?
Tutorial - Lua, Making Code ObjE
That would be X and positive. Just use the same code from above, but put Xs in for the Ys. Any time you use South, make it Y and negative. Any time it is West, use X and negative.

Now...what if you want it North East?
Tutorial - Lua, Making Code ObjNE
Well...you could do just 5 units North, and 5 units East, right? Yes, that works. So you would put in the code
Code:
pos:AssignY(pos:GetY()+n)
pos:AssignX(pos:GetX()+m)
where 'n' and 'm' are numbers (decimal is fine).

Now, what if you want to place an object ABOVE or BELOW the player? That is where Z comes in handy. Z is the up-and-down placement axis. Positive Z is up, negative is down.

If you wanted something NorthEast of the player, and about head level then what would you do? You would use the same thing we used for NorthEast,
Code:
pos:AssignY(pos:GetY()+n)
pos:AssignX(pos:GetX()+m)
and just add on the Z:
Code:
pos:AssignY(pos:GetY()+n)
pos:AssignX(pos:GetX()+m)
pos:AssignZ(pos:GetZ()+w)
This would place it NorthEast and above the player. Make sure that you note that if the ground is higher where you are placing the object, even though it would bee above-ground at the player's location, the object may be spawned below. Just to make it easier, don't make any of these numbers greater than 10.

ANY QUESTIONS? I DEMAND QUESTIONS. Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad
Back to top Go down
http://darkgrav.invisionplus.net
-BTF-Sniper
Administrator
Administrator
-BTF-Sniper


Posts : 67
Join date : 2008-04-20
Age : 32

Tutorial - Lua, Making Code Empty
PostSubject: Re: Tutorial - Lua, Making Code   Tutorial - Lua, Making Code Icon_minitimeWed Jul 09, 2008 1:31 am

wow, thx a uber bunch, this helps alot
Back to top Go down
http://gunnz.forumo.biz/index.htm
cncnick13
corporal
corporal



Posts : 58
Join date : 2007-11-02

Tutorial - Lua, Making Code Empty
PostSubject: Re: Tutorial - Lua, Making Code   Tutorial - Lua, Making Code Icon_minitimeWed Jul 09, 2008 1:53 am

lol nice man
Back to top Go down
https://gravforce.forumotion.com
Graviton
Recruit
Recruit
Graviton


Posts : 10
Join date : 2007-11-05
Age : 32

Tutorial - Lua, Making Code Empty
PostSubject: Re: Tutorial - Lua, Making Code   Tutorial - Lua, Making Code Icon_minitimeWed Jul 09, 2008 1:23 pm

I was bored last night lol Smile
bounce bounce bounce bounce bounce bounce bounce bounce bounce bounce bounce bounce
Back to top Go down
http://darkgrav.invisionplus.net
Sponsored content





Tutorial - Lua, Making Code Empty
PostSubject: Re: Tutorial - Lua, Making Code   Tutorial - Lua, Making Code Icon_minitime

Back to top Go down
 
Tutorial - Lua, Making Code
Back to top 
Page 1 of 1
 Similar topics
-
» IRC Tutorial
» Tutorial - Lua, Getting Started

Permissions in this forum:You cannot reply to topics in this forum
GravForce CnC MoDe :: *Server* :: Cool Stuff-
Jump to: