Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mmyy.hpp"
- #include <stdio.h>
- #pragma comment(lib, "lua51.lib")
- #pragma comment(lib, "mmyy.lib")
- class MyState : public mmyy::State
- {
- public:
- // this has one fix for the Push function in mmyy::State, so if you don't put this here
- // you will not be able to use Push from base if you declare your own Push overloads
- // see http://www.cplusplus.com/forum/general/15761/ for more info
- MMYY_DECLARE_HELPERS
- // redirect output if nesseceary
- void Msg(const char * str)
- {
- printf("%s", str);
- }
- void MsgN(const char * str)
- {
- printf("%s\n", str);
- }
- void OnError(const char * str)
- {
- printf("[lua error] %s\n", str);
- }
- // this will give us some nice functions to handle MyState
- // Push(MyState *var)
- // ToState(int idx, MyState *def)
- // ToState(int idx, check = true)
- // it will use "state" as its metaname
- MMYY_WRAP_CLASS(MyState, State, state)
- };
- // if we only have one state, we assume that this will always be the state used when a lua function is called
- MyState *my;
- LUALIB_FUNCTION(_G, State)
- {
- auto self = new MyState();
- my->Push(self);
- return 1;
- }
- // uh oh
- LUALIB_FUNCTION(_G, GetCurrentState)
- {
- my->Push(my);
- return 1;
- }
- LUAMTA_FUNCTION(state, Error)
- {
- auto self = my->ToState(1);
- self->Error(my->ToString(2));
- return 0;
- }
- LUAMTA_FUNCTION(state, RunString)
- {
- auto self = my->ToState(1);
- my->Push(self->RunString(my->ToString(2)));
- return 1;
- }
- int main()
- {
- my = new MyState();
- my->RunString("state = State()");
- my->RunString("state.hello = 'hello world'");
- my->RunString("state:Error('aaaaaaaaaaaaaaaa')");
- my->RunString("print(state.hello)");
- my->RunString("state = GetCurrentState()");
- my->RunString("state:RunString('aisjdiasjd iasjdi asjidj sad')");
- return 0;
- }
- /*
- [lua error] aaaaaaaaaaaaaaaa
- hello world
- [lua error] [string "aisjdiasjd iasjdi asjidj sad"]:1: '=' expected near 'iasjdi'
- */
Advertisement
Add Comment
Please, Sign In to add comment