Forum

> > CS2D > Scripts > C++ function
Forums overviewCS2D overview Scripts overviewLog in to reply

English C++ function

7 replies
To the start Previous 1 Next To the start

old C++ function

Gajos
BANNED Off Offline

Quote
How I can make, save and use a c++ function in lua script?

C++:
int Add(int x, int y) {
    return x + y;
}

Lua:
print(Add(3,12))

old Re: C++ function

Starkkz
Moderator Off Offline

Quote
Look up on the wiki, there are two tutorials about that from C++ and BlitzMax.

old Re: C++ function

mafia_man
User Off Offline

Quote
user Starkkz has written
Look up on the wiki, there are two tutorials about that from C++ and BlitzMax.

Which wiki are you talking about? He didn't say anything about BlitzMax so why pointing him to it?
Just because cs2d is written in BlitzMax it doesn't mean he needs to write and compile the library using BlitzMax and extern C++ in it, he can write the library in C++ and compile it using gcc or g++.

old Re: C++ function

Starkkz
Moderator Off Offline

Quote
user mafia_man has written
Which wiki are you talking about? He didn't say anything about BlitzMax so why pointing him to it?
Just because cs2d is written in BlitzMax it doesn't mean he needs to write and compile the library using BlitzMax and extern C++ in it, he can write the library in C++ and compile it using gcc or g++.

Firstly, I meant UnrealSoftware wiki. Secondly, there's no need to be hostile, I haven't responded that way, I just mentioned BlitzMax as an option (Since there's a C++ tutorial and a BlitzMax tutorial).

old Re: C++ function

Gajos
BANNED Off Offline

Quote
user Starkkz has written
Look up on the wiki, there are two tutorials about that from C++ and BlitzMax.

Can you send me links to the tutorials?
I must compile this as dll file?

old Re: C++ function

MikuAuahDark
User Off Offline

Quote
1. You even don't need BlitzMax to create LUA modules on CS2D
2. If you write your code on C++, you need to use extern "C" on lua file.

simple lua libraries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define LUA_LIB
#define LUA_BUILD_AS_DLL
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

int Add(lua_State *L) {
	double x=luaL_checknumber(L,1);
	double y=luaL_checknumber(L,2);
	lua_pushnumber(L,x+y);
	return 1;
}

luaL_reg function_list_that_want_to_exported_to_lua[]={
	{"Add",Add},
};

extern "C" int __declspec(dllexport) luaopen_thelibraryname(lua_State *L) {
	luaL_register(L,"LibraryName",function_list_that_want_to_exported_to_lua);
	return 1;
}
You can find more information on Google
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview