Noobs guide to scripting

msmalik681

OpenBOR Developer
Staff member
Update: Found a way to separate my tutorials follow the below links for each tutorial and added a tutorial for the powerful "if statement" once you lean this you can start making custom scripts.

Before you start I want to say sorry if my guide is not very easy to follow this is my first written guide and yes this a very simple introduction if this is useful to anyone then I will be slowing moving onto more complicated scripts in the future.


Lesson 1 – Hello World

Lesson 2 - Hi my name is...

Lesson 3 - If I Ruled the World
 
Last edited:
I know, but I just learned how to run updated.c. I was never used to that at all though I tried with copies (of updated.c) before. This is one that got me to comprehend how updated.c works.
 
Ok nice to know you found it helpful i will do a few more covering the basics but tell the truth my original intention was to make a keyscript tutorial as it would have really helped me when i wanted to learn about them.
 
I have just updated my first post with a second script guide for anyone new to chronoscript (openbor scripting) more guides coming soon.


Quick forum question for DC can we have text hidden with a button to reveal as it would tidy up this post nicely.  And is this post worthy of being sticky ?
 
DintheAbary said:
wouldn't it be "My_hp_is_" + health instead of "My_hp_is_" + hp?

cause the variable is declared as hp instead of health

Code:
int hp = getentityproperty(self, "health"); //get entity hp.

Of course, you can change the variables name but you'd need to consistent with the new name :)
 
msmalik681 said:
Quick forum question for DC can we have text hidden with a button to reveal as it would tidy up this post nicely. 

Hide and code tags conflict with each other, so unfortunately no, you can't do that. Sorry. :(

And is this post worthy of being sticky ?

Let's see how things go before it becomes sticky - if it works well enough, it should probably go in the wiki.

DC
 
Bloodbane said:
DintheAbary said:
wouldn't it be "My_hp_is_" + health instead of "My_hp_is_" + hp?

cause the variable is declared as hp instead of health

Code:
int hp = getentityproperty(self, "health"); //get entity hp.

Of course, you can change the variables name but you'd need to consistent with the new name :)
oh I was reading the wrong end of the tag haha. Thanks for pointing that out!
 
This is a good guide, thanks for writing it! But I have one problem with it: it calls OpenBOR script "ChronoScript". ChronoScript is the project to update OpenBOR script for the Chrono Crash engine; it's not the current script engine in OpenBOR.
 
@Plombo
Cronoscript wording removed thanks for clearing that up.

@Thegr8test
Cool let me know how you get on with it. I have started to work on my mod again but I hope to continue with more tutorials any feed back good or bad is appreciated.

@Everyone
Sorry to anyone following the guide with the example game I uploaded (it crashes on opening) I have uploaded a fixed version and links in the tutorial are updated.
 
Lesson 1 – Hello World

Here is a quick introduction to scripting with openbor with a very simple script to get started. This can be done using any functional openbor project with a data folder if you don’t have one here is an example game to use. To start navigate to "data/scripts/" then make a new text file called "updated.c" or make a text file then rename it (windows may hide file extensions unhide this from “change folder and search options”).  Now open updated.c and your computer may ask for an application to open with any text editor should be fine but I like notepad.  Now type:

Code:
void main()
{
}
This is your main function anything between the braces (braces are {=open and }=close) will run. Now between the braces make a new line and type:
Code:
drawstring (150, 80, 3, "Hello_World");

This will now display text to the screen saying "Hello World" to break this down the function works like this:

Code:
Function Name(X co-ordinate, Y co-ordinate, Font Number, Text String) semi colon ( ; )to end script command.

Now run the game if Hello World displays when you start a level congratulations you have just made your first script. If the game closed as soon as you opened it check the log found at “Data/Logs/ OpenBorLog.txt” to see what went wrong and fix it. Try playing with functions inputs to move where the text is displayed, change the font and change the displayed message. Please note drawstring will normally display and disappear right away but updated.c runs over and over constantly.
 
Lesson 2 - Hi my name is...

This will cover how to extract player properties and some of the data types you can work with.

First there are 3 main data types char = text, int = numbers and float = decimal numbers. openbor will automatically allocate these for you when you use void to create data but some data should be declared with void like pictures, entities and sound files just think of these like shortcuts to data also known as variables.


So lets start open your projects script folder “data/scripts/” and make a new file ”player_update.c” now open the file and make a main function. you could use this example game if you don't already have it.

Code:
void main()
{
}

Next we will get the player who is calling the function now in your main type:


Code:
void self = getlocalvar("self"); //Get calling entity.

Note the 2 forward slashes (//) start a comment anything after // will be ignored by the script these are there for you to keep track of your code and should be used but are optional. Lets break down the above code.


data_type variable(shortcut) = ( get_local_variable_named_”self” ) ; end_code // user_comment


Now you have the entity that called this script you can get various properties from them. Check the script reference manual for most of the properties you can work with. Now to get the players name, hp and z value. In your main function type:


Code:
char name = getentityproperty(self, "name"); //get entity name.
int hp = getentityproperty(self, "health"); //get entity hp.
float z = getentityproperty(self, "z"); //get entity z value.

Now we have these variables let’s make use of them below these add these lines:


Code:
drawstring (10, z-60, 3, "Hi_my_name_is_" + name); //display string with name.
drawstring (10, z-40, 3, "My_hp_is_" + hp); //display string with hp.
drawstring (10, z-20, 3, "My_Z_value_is_" + z); //display string with z.

Note the y input for drawstring is the entity’s z value so the text will move with the entity. Also note the way the variables (shortcut names) are used these make coding a lot simpler. Here is the first line without defining the variables first:


Code:
drawstring (10,getentityproperty(getlocalvar("self"), "z") - 60, 3, "Hi_my_name_is_" + getentityproperty(getlocalvar("self"), "name")); //display string with name.

You will get the same output with both scripts. Now go into any hero or all hero’s data file and open it.  If you’re using my example mod then navigate to “data/chars/mary/mary.txt” now after the header data and before the animation data add this line:


Code:
Ondrawscript data/scripts/player_update.c

You have now made a on draw script it will run over and over when in level so the hp will update as your player is damaged and the z value will update as you move around the screen this will also move the location of the text. Run the game and see the magic at work again check the log if the mod shuts down too see if there are any problems.  I often use this technique to see what kind of data properties hold so its easier to work with them.


Hope this helped at least one person and please reply with any questions or if you need any more help with this.
 
Lesson 3: If I Ruled the World

This lesson covers “if statements” an “if statement” will test a condition if it is true it will run a set script. To follow this tutorial download the example game.

Let’s get started open your example game and open file: Data\CHARS\Shermie\SHERMIE.TXT find "ANIM WALK" add a space before any frame data then copy this:

Code:
@script
void self = getlocalvar("self");//get caller
int map; //variable to hold map number
if(frame==0){map=1;} else {map=0;} //change map based on frame
changeentityproperty(self,"map",map); //apply map
@end_script

Let’s break down the “if statement”:


Code:
if(condition is true){scriptA;} else {ScriptB;}

So the condition checks if the animation is on frame  0 then it will change the map colour to 1 for any other frame map will be map 0.  If you run the mod now when Shermie walk she will flash a different colour for her first frame.  To make a “if statement” you need to how to compare variables here is a list of the operators you can use.


operator. description.example A=10 and B=20.
==equal to.(A == B) is not true.
!=not equal to.(A != B) is true.
>greater than.(A >  B) is not true.
<less than.(A <  B) is true.

>=
greater than or equal to.(A >= B) is not true.
<=less than or equal to.(A <= B) is true.

Extra note on the ! Not operator can check for true or false. True is any value other then 0 and false is 0 or NULL() (this means the variable is empty or uninitialized). Also if you just declare a variable without operators then the variable is checked for true or false values. You can have more than 1 condition by adding the below operators.


operator. description.example A=1 and B=0.
&&AND operator.(A && B) is false

||
OR Operator.(A || B) is true.



A "if statement" can have extra conditions using "else if" and you can run a script if no conditions were met using "else" at the end of your "if statement". Here is a example:

Code:
@script
void self = getlocalvar("self");//get caller
int map; //variable to hold map number
if(frame==0){map=0;
} else if(frame==1){map=1;
} else if(frame==2){map=2;
} else {map=3;}
changeentityproperty(self,"map",map); //apply map
@end_script

When you run the game now she will flash multiple colours when she starts walking. Please note you can have any number of "else if" conditions but as soon as a condition is true it will run the script for that condition then end the whole "if statement" ignoring any other "else if" or "else" conditions. Here is a example of more then 1 conditions:

Code:
@script
void self = getlocalvar("self");//get caller
int hp = getentityproperty(self,"health"); // get caller current hp
int map; //variable to hold map number
if(frame>=0 && frame<=2 && hp==50){map=0;
} else if(frame>=3 && frame<=5 && hp==50){map=1;
} else if(frame>=6 && frame<=8 && hp==50){map=2;
} else {map=3;}
changeentityproperty(self,"map",map); //apply map
@end_script

So all conditions have to be met to change the colour as soon as you hit her the colour changing will stop. lastly let do some exaples with the or operator.

Code:
@script
void self = getlocalvar("self");//get caller
int map; //variable to hold map number
if(frame==0 || frame==5){map=0;
} else if(frame==1 || frame==6){map=1;
} else if(frame==2 || frame==7){map=2;
} else {map=3;}
changeentityproperty(self,"map",map); //apply map
@end_script

You will have a lot of flashing colours with this one.  So there you have it please post any questions and happy scripting.
 
I know this is an old thread, but I just had to say thanks for these tutorials. If learning Openbor scripting is a 20 step process, then this is the critical 0 to 1 step that almost all other resources skip. I think understanding the concept of your scripts are run when certain events occur and this is how you setup that event listener is really crucial but not clear elsewhere. I mean it's in the manual, but the manual is one of those documents that only makes sense when you sorta know the answer already (like many reference manuals).
 
Back
Top Bottom