Erabasic tutorial

From Era Wiki
Jump to: navigation, search

This document is a quick tutorial on erabasic, the language that era games use. I barely know it myself, so doublecheck everything I say here. Feel free to edit this too.

Other things to read[edit]

What makes erabasic different[edit]

Very quick summary of what makes erabasic different from other programming languages out there.

Available datatypes are numbers (64 bit signed integers), strings, and arrays (1 dimensional, 2 dimensional, etc). No floating point numbers.

There is no way to allocate new memory, as far as I know. You got your arrays, and that's it, there is no way to get more. I'm not sure if strings got a maximum size cap.

While in most programming languages you call other functions, in erabasic it's possible to jump to them by doing JUMP FUNCTION_TO_JUMP_TO. This feature is pretty rare in commonly used programming languages, but not as rare in visual novel engines like nscripter, kirikiri, or RenPy. In novels, jumping one way is pretty common.

Adding extensions to existing era games is possible with the TRYCALL command, which takes a string, and calls function with said name if it exists anywhere. Not the fastest way to do it, but that's what era does.

Quick history lesson[edit]

erabasic was created for the game called erakanon. It was a command line program, it worked from command line, you played by typing command numbers on keyboard, there was no mouse support. Program that run that game was called eramaker, and source code for that never was made available. Link: http://cbaku.com/b/2010/12/eramaker/

erakanon was a slave trainer game, a game where you bought slaves, bought your sex toys and tools, trained them to become more obedient and lewd, and sold them back to get new slaves. If you wonder why every era game starts in a SHOP, this is why.

EmuEra is a similar program that added extra features to eramaker, this is the program that modern era games are made with. It supports mouse input (command lines are now clickable and are called buttons), graphics, various new features, and compatibility options for running old erabasic games. It is written in C# and source code is available. Link: https://ja.osdn.net/projects/emuera/

EmuEra-Anchor is a patched version of EmuEra that allowed easier english translation of games, and text hooking for quick text extraction. Link: https://gitgud.io/era-games/EmuEra

Emuera.EM+EE is a modified EmuEra that added sound functionality and something else. Link: https://gitlab.com/EvilMask/emuera.em

EmueraLFD is EmuEra by koreans, again I don't know what is different about that. Link to version 8: https://arca.live/b/breaking/72249797

Hello World[edit]

Let's make our first erabasic "game" now.

Download a random modern era game, for example from this very wiki https://wiki.eragames.rip/index.php/Main_Page , and copy EmuEra executable from there to a separate folder. Create folders ERB and CVS in that folder.

In the ERB folder, create file SYSTEM.ERB (name doesn't matter), and put this in it:

@EVENTFIRST
PRINTW Hello world!
BEGIN TITLE
@EVENTFIRST means "function EVENTFIRST"
PRINTW Hello world! means "print with wait 'Hello world!'"
BEGIN TITLE means "go back to title screen"

Upon launching EmuEra.exe, you will see your usual title screen, but without any strings that are usually there, and two options to [0]Start the game and [1]Load, in japanese. Click option [0], and you will see Hello world!, before returning back into the title screen.

Local Variables[edit]

There are multiple ways to use variables in erabasic. One is to use LOCAL array of numbers, which is always available.

@EVENTFIRST
LOCAL:0 = 111
LOCAL:1 = 222
PRINTFORML LOCAL:0 is {LOCAL:0} right now.
PRINTFORML LOCAL:1 is {LOCAL:1}.
PRINTFORMW And their sum is {LOCAL:0} + {LOCAL:1}.
BEGIN TITLE

PRINTFORMW is same thing as PRINTW, but it allows this fancy way of adding variables.

You can also access LOCAL:0 as LOCAL, same thing.

@EVENTFIRST
LOCAL:0 = 111
LOCAL = 222
PRINTFORMW Both LOCAL and LOCAL:0 are {LOCAL:0} right now.
BEGIN TITLE

Output is: Both LOCAL and LOCAL:0 are 222 right now.

A more modern aproach to making a local variable is by using #DIM. Using LOCAL array is actually discouraged now.

@EVENTFIRST
#DIM something
something = 5
PRINTFORMW Something is {something}
BEGIN TITLE

Interesting thing about local variables set like this, is that value inside them gets preserved between function calls, kind of like C static variables. To demonstrate that, try this code.

@EVENTFIRST
CALL SECONDFUNCTION
CALL SECONDFUNCTION
BEGIN TITLE

@SECONDFUNCTION
#DIM SOMETHING
PRINTFORMW At first, something is {SOMETHING}
SOMETHING = 5
PRINTFORMW Then, something is {SOMETHING}
Output is:
At first, something is 0
Then, something is 5
At first, something is 5
Then, something is 5

String Variables[edit]

We know how to work with numbers, but how do we work with strings? Simple, just use LOCALS:0 for those instead of LOCAL:0. S stands for String.

@EVENTFIRST
LOCALS = Hi there
PRINTFORMW LOCALS is %LOCALS%
BEGIN TITLE

You can use #DIM method too, but for strings you should use #DIMS instead.

@EVENTFIRST
#DIMS SOMETHING
SOMETHING = Hi there
PRINTFORMW Something is %SOMETHING%
BEGIN TITLE

Global Variables[edit]

Now, let's learn how to use global variables.

Inside of the ERB folder, create file ERH.ERH (exact filename doesn't matter, anything will work), and put this in it:

#DIM SOMEGLOBALVARIABLE
#DIMS SOMEGLOBALSTRINGVARIABLE

Now you can use those variables anywhere in your code. If you want your variable to be included in save data, add SAVEDATA after #DIM.

#DIM SAVEDATA THISWILLBESAVED
#DIMS SAVEDATA THISSTRINGWILLBESAVED

Array variables[edit]

We already used arrays with LOCAL and LOCALS, now let's learn how to create our own. Here is an example of creating and using a 1 dimensional array.

@EVENTFIRST
#DIM SOMEARRAY,10
SOMEARRAY:0 = 5
SOMEARRAY:1 = 6
SOMEARRAY:2 = 7
PRINTFORMW SOMEARRAY:0 is {SOMEARRAY:0}
PRINTFORMW SOMEARRAY:1 is {SOMEARRAY:1}
PRINTFORMW SOMEARRAY:2 is {SOMEARRAY:2}
BEGIN TITLE

To create 2 dimensional array, use this.

@EVENTFIRST
#DIM SOME2DARRAY,10,20
SOME2DARRAY:0:0 = 5
SOME2DARRAY:0:1 = 6
SOME2DARRAY:0:2 = 7
PRINTFORMW SOME2DARRAY:0:0 is {SOME2DARRAY:0:0}
PRINTFORMW SOME2DARRAY:0:1 is {SOME2DARRAY:0:1}
PRINTFORMW SOME2DARRAY:0:2 is {SOME2DARRAY:0:2}
BEGIN TITLE

You can use variables as array indexes.

@EVENTFIRST
#DIM SOME2DARRAY,10,20
LOCAL = 3
SOME2DARRAY:LOCAL:LOCAL = 5
SOME2DARRAY:(LOCAL+1):(LOCAL-1) = 6
PRINTFORMW SOME2DARRAY:3:3 is {SOME2DARRAY:3:3}
PRINTFORMW SOME2DARRAY:4:2 is {SOME2DARRAY:4:2}
BEGIN TITLE

What can you do with numbers[edit]

Usual arithmetics

@EVENTFIRST
PRINTFORMW {5 + 2}
;prints 7
PRINTFORMW {2 - 5}
;prints -3
PRINTFORMW {5 * 2}
;prints 10
PRINTFORMW {5 / 2}
;prints 2
PRINTFORMW {221 % 2}
;prints 1
BEGIN TITLE

This

LOCAL += 5 ;same as LOCAL = LOCAL + 5
LOCAL -= 5 ;same as LOCAL = LOCAL - 5
LOCAL *= 5 ;same as LOCAL = LOCAL * 5
LOCAL /= 5 ;same as LOCAL = LOCAL / 5
LOCAL %= 5 ;same as LOCAL = LOCAL % 5

Convert them to string

@EVENTFIRST
#DIMS TESTS
#DIM TEST
TEST = 777
TESTS = %TOSTR(TEST)%
PRINTFORMW TESTS is %TESTS%
;prints TESTS is 777
TESTS = {TEST}
PRINTFORMW TESTS is still %TESTS%
;prints TESTS is still 777
BEGIN TITLE

Logical operators

TODO

There is also a long list of functions that can be used in expressions here https://wiki.eragames.rip/index.php/Emuera/exmeth , the most interesting ones are

What can you do with strings[edit]

You can convert them to numbers

TODO

You can add strings to each other

@EVENTFIRST
#DIMS TESTS
#DIM TEST
TEST = 777
TESTS = ZZZ
TESTS = %TESTS%{TEST}
PRINTFORMW TESTS is %TESTS%
BEGIN TITLE

You can get substrings

TODO

You can search for substring

TODO

IF ELSE and SWITCH[edit]

Creating functions[edit]

There are two kinds of functions in erabasic. Ones that return nothing, and ones that return a number or a string.

TODO

User Input[edit]

TODO

SVG[edit]

TODO

What exactly is BEGIN TITLE[edit]

TODO

Mention that @EVENTFIRST is usually placed in SYSTEM.ERB or EVENTFIRST.ERB, and @SHOW_SHOP in SHOP.ERB