• New Horizons on Maelstrom
    Maelstrom New Horizons


    Visit our website www.piratehorizons.com to quickly find download links for the newest versions of our New Horizons mods Beyond New Horizons and Maelstrom New Horizons!

PA course on C

Ah, thought I'd lost this.
Hope you don't mind my attaching it. 8)

In C, variables must be of certain types.
In POTC, the types available are:
int - an integer. -32767 through 0 through 32767.
bool - Subtype of int. Can only be 0 or 1 (true or false). note that in POTC true is #defined as 1 and false is #defined as 0 so you can use those words. In all other respects (use in operations, conversions, etc.) it's a normal int.
float - a `floating-point` number. I don't know offhand the number of decimal places POTC supports. Examples: -20.0, 3.14159, 1.414.
string - a string of character(s): i.e. "b", "the quick brown fox", "32767".
object - an object. Basically, a string that can have attributes (which are strings), that can have attributes, and so on. Check the Plea thread for more details.
ref - check the Plea thread for more details
aref - ditto

You can also convert between the three main types.
Whenever you assign a bool, int, or float to a string (whether to a string xxx variable, or an attribute of an object) it's converted to a string.
If you want to convert a string to a number you can use
int sti(string conv); // converts string to int
float stf(string conv); // converts string to float

note that you use sti for bools since bools are ints; if the string != 0, it's considered true. (have to check if negative values are also true; IIRC they are).

You can use MakeInt or MakeFloat as well; they take anything--string, int, float, bool--and output it correctly.

I've also added functions Roundup and Roundown to utils.c; they take floats and output ints.

And, note that floats and ints automatically convert--i.e. pass an int to a function that expects a float and it gets made a float; or assign a float to an int (int b = 12.2) and it gets automatically MakeInt'd.

So.

When you see a function definition like so:
void DoSomething(float num)
That means whenever you call it you need to pass a float to it (whether directly, or based on a variable)--like DoSomething(12.5) or DoSomething(var_my_float).
If there's more than one argument, it gets separated by commas.
You can then use the arguments in the function.

So in the example you have,
void LAi_NPC_Equip(ref chr, int rank, bool isWeapons, bool isGun)

that means that function takes, in order,
a reference, referred to in that function by chr
an int
and two bools.

So then whenever we call it, we must pass it the correct number of variables, of the correct type.
So again in the example:
LAi_NPC_Equip(chr, sti(chr.rank), true, true);
Chr must have been declared before that, and assigned a valid reference to a character; so chr is OK. sti(chr.rank) outputs an int, so rank is OK.
then two booleans are passed (true and true) so isWeapons and isGun are OK.


A note.
By default, variables passed to functions are passed as copies. Example:
Code:
void switch(int v1, int v2)

{

  int temp = v2;

  v2 = v1;

  v1 = temp;

}

int t1 = 1;

int t2 = 2;

switch(t1, t2);

will do absolutely NOTHING because when switch() is called, the variables passed to it are copied into /new/ variables especially for it.

This is great if you make changes in the function that you /don't/ want saved; but it's not if you do want them saved. And besides it requires enough extra memory to make those copies.

If you /don't/ want copies made, you need to do two things.
1. Pass variables to the function with the & sign in front, i.e. switch(&t1, &t2).
2. In the function definition, use ref or aref instead of the specific variable type: void switch(ref v1, ref v2)

This is why you see the following often:
ref someref = &variable[pos]; that means that the ref is pointing to the variable, not a copy of the variable.

Once you've done that /once/, you can pass the ref itself without the &.

Examples:
1. Passing &variables
Code:
void switch(ref v1, ref v2)

{

  int temp = v2;

  v2 = v1;

  v1 = temp;

}

int t1 = 1;

int t2 = 2;

switch(&t1, &t2);
2. Using refs to &variables
Code:
{switch(ref v1, ref v2) the same as above}

int t1 = 1;

int t2 = 2;

ref r1 = &t1;

ref r2 = &t2;

switch(r1, r2);
 
Once variables are passed to a function, they can be used as if they were declared in the function body--but with the values assigned to them when the function is called.


You also see this with game functions more directly, with PostEvent and SendMessage:
because those two are functions /themselves/, which take as a string the function name to call, they can't pass arguments directly to the function; instead, they do like this:
PostEvent(string eventname, float delay, string params, Extra_Params_here)
where eventname is defined elsewhere by setevenhandler to point to a function. Delay is in milliseconds.
Then, in the function body, it uses GetEventData() to find the data.

params is a string, detailing the parameters to expect. It is in the form "llsssf" or some such, where:
l = (lower case L) int
s = string
f = float
i = attribute reference (_not_ int)
e = entity (aka object)
Example:
Code:
SetEventHandler("print", "PrintNumTimes");

void PrintNumTimes()

{

  int numtimes = GetEventData();

  string printstr = GetEventData();

  for(int i = 0; i < numtimes; i++) Log_SetStringToLog(printstr);

}

PostEvent("print", 1, "ls", 5, "Hello World");

This will, after a delay of 1 millisecond, print Hello World to the log five times.
 
I do not understand a word that u guys have written i dont think im going to be making mods for this game 8):
 
blimey think of this as a `quick-course` in C. Its can be pretty hard to understand if you have no programming experience already.
I already know how to program in Java, so I really dont have a lot of problems understanding this.
Seems a lot simpler than java actually. And by simple i mean there are less features.

blimey every programming language looks "A LOT" like each other, so If you dont understand this and wish to learn, I suggest you find some tutorials on the net that will teach you the basics of programming. :)

If its any consolation, I too once, would have understood nothing of this.

Well GL to you. :)
 
OctoParrot : the "good" thing about C/C++ is that its so "basic", you can build anything with the basic building blocks it provide. i wont hide that i prefer to program FORM oriented, less headaces and more "easy use" functions but the normal, "colsole" type of programs are quite easy to do once youve gotten some routine, no matter if its in Java or something else entirely. i for exsample, learned the basics of programming in PHP and now ive gotten pretty good with C/C++. the only thing that can really annoy me at times is that there is rarely a "easy" way to do things. if you wish to do a text string the old way with CHAR then its essencially a array, whilst this has its clear advantages at times its a pain in the butt at other times.

Diomed : you might want to "touch" the subject of a class shortly at one point, explaining why you can inherent a function from a parent class so that those "out of the blue" calls doesnt seem as complete black magic, and only works some places whilst not others. :)
 
Cool thing that you have many of the c files with POTC. So no need to download anything for the modder, I was amazed when I saw that after installing the game ^^
 
Well, I stumbled in here this morning, and look what I find! I don't think I've ever been in this topic! Thanks, Diomed, NK, and others... Maybe now this stuff will make more sense to me! (LOL, about time, hm?) :cheers :) :yes :cheers
 
Um, afraid you lost me jack_sparrow_jr...
Never touched either Delphi or VB (or V/C/ for that matter); just a `syntax-highlighting` text editor for me.
POTC itself handles all compiling, etc.
 
nice to know ......
then I can do it in notepad ...... <img src="http://www.piratesahoy.com/forum/style_emoticons/<#EMO_DIR#>/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />
thx
<img src="http://www.piratesahoy.com/forum/style_emoticons/<#EMO_DIR#>/cheers.gif" style="vertical-align:middle" emoid=":cheers" border="0" alt="cheers.gif" />:
 
Though I do certainly recommend _some_ kind of `code-friendly` text editor; I've grown to love mine, that's for sure (not least because I don't make typos anymore because of it...)
 
personally i like to use `DEV-C`++ for most simple programming tasks and scriptediting like for POTC. whereas i use Borland Builder for most other stuff

<a href="http://sourceforge.net/projects/`dev-cpp`/" target="_blank">http://sourceforge.net/projects/`dev-cpp`/</a>

its a Opensourced peice of code, quite nice yet both simple and functional for all purposes.
 
<!--`QuoteBegin-NathanKell`+--><div class='quotetop'>QUOTE(NathanKell)</div><div class='quotemain'><!--QuoteEBegin-->New link for keywords.c:
<a href="http://mods.piratesahoy.net/uploads/nathankell/Keywords.c" target="_blank">http://mods.piratesahoy.net/uploads/nathan...kell/Keywords.c</a>

Has comments inline; but if anything doesn't make sense give a holler.[/quote]

Hey Nathan, whatever happened to this file? I'd love to get my hands on it, but the link's dead.
 
Ooh, sorry about that. Keith cleaned the server a ways back and I probably never `re-upped` it.
Oh, and the most recent one I have is from a couple months back, I may have added more but they were lost in the Great Harddrive Clickdeath of Doom.

So, until I `re-up` it, anybody who needs one give me an email and I'll RSVP it. Yours is already on the way.
 
Whoa, nice tutorial. I just need a good compiler for C. I've written your script in Notepad, but how do I run it?
 
Back
Top