[1.12.1] Lua Binding / Object-Oriented Access Question menu

User Tag List

Results 1 to 3 of 3
  1. #1
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1584
    Join Date
    May 2010
    Posts
    1,829
    Thanks G/R
    188/531
    Trade Feedback
    16 (100%)
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    [1.12.1] Lua Binding / Object-Oriented Access Question

    What I'm trying to do is something like this,

    Code:
    player = objManager.GetPlayer(i)
    player:interact()
    My problem is not registering functions, its passing an object to a function.

    Currently, I'm doing this,

    Code:
    player = GetPlayer(i)
    interact(player)
    I have been following some guides like this,
    Programming in Lua : 28.3

    c&p version with less code
    Code:
    struct NumArray{
    	int size;
    };
    static int newarray(lua_State l){
    	int n = lua_tonumber(l, 1);
    	NumArray *a = (NumArray *)lua_newuserdata(l, sizeof(NumArray));
    	a->size = n;
    	return 1;
    }
    static int printarray(lua_State l)
    {
    	NumArray *a = (NumArray*)lua_touserdata(l, 1);
    	printf("%d", a->size);
    	return 0;
    }
    luaL_Reg arraylib_f[] = 
    {
    	{ "new", newarray },
    	{ NULL, NULL }
    };
    luaL_Reg arraylib_m[] =
    {
    	{ "print", printarray },
    	{ NULL, NULL }
    };
    int luaopen_array (lua_State *L) {
    	luaL_newmetatable(l, "LuaBook.array");
    
    	lua_pushstring(l, "__index");
    	lua_pushvalue(l, -2);
    	lua_settable(l, -3);
    
    	luaL_openlib(l, NULL, arraylib_m, 0);
    	luaL_openlib(l, "array", arraylib_f, 0);
    }
    A big problem is I'm limited to Lua 5.0 and some lua functions look to be missing or I have not been able to find them.

    Current ones I have found,
    http://www.ownedcore.com/forums/worl...ml#post3796321 ([WoW] 1.12.1.5875 Info Dump Thread)

    The error I'm getting from,
    Code:
    a = array.new(1)	
    a:print();
    is "attempt to index global `a'(a userdata value)".

    Anyone know what I'm doing wrong? Thanks!

    [1.12.1] Lua Binding / Object-Oriented Access Question
  2. #2
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1584
    Join Date
    May 2010
    Posts
    1,829
    Thanks G/R
    188/531
    Trade Feedback
    16 (100%)
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Solved, missed something from the previous page

    Code:
    struct NumArray{
    	int size;
    };
    static int newarray(lua_State l){
    	int n = lua_tonumber(l, 1);
    	NumArray *a = (NumArray *)lua_newuserdata(l, sizeof(NumArray));
    
    	luaL_getmetatable(L, "LuaBook.array");
    	lua_setmetatable(L, -2);
    
    	a->size = n;
    	return 1;
    }
    static int printarray(lua_State l)
    {
    	NumArray *a = (NumArray*)lua_touserdata(l, 1);
    	printf("%d", a->size);
    	return 0;
    }
    luaL_Reg arraylib_f[] = 
    {
    	{ "new", newarray },
    	{ NULL, NULL }
    };
    luaL_Reg arraylib_m[] =
    {
    	{ "print", printarray },
    	{ NULL, NULL }
    };
    int luaopen_array (lua_State *L) {
    	luaL_newmetatable(l, "LuaBook.array");
    
    	lua_pushstring(l, "__index");
    	lua_pushvalue(l, -2);
    	lua_settable(l, -3);
    
    	luaL_openlib(l, NULL, arraylib_m, 0);
    	luaL_openlib(l, "array", arraylib_f, 0);
    }
    Now this works!
    Code:
    a = array.new(1)	
    a:print();

  3. #3
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1584
    Join Date
    May 2010
    Posts
    1,829
    Thanks G/R
    188/531
    Trade Feedback
    16 (100%)
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Next problem

    If I want to read and write vars like,

    Code:
    a = array.new(1)	
    a.var = 1;
    c = a.var;
    I need to add,

    Code:
    int FASTCALL l_set(lua_State L)
    {
    	NumArray *n = (NumArray*)lua_touserdata(L, 1);
    	const char *key = lua_tostring(L, 2);
    	int val = lua_tonumber(L, 3);
    
    	printf("%s = %d\n", key, val);
    	return 0;
    }
    
    int FASTCALL l_get(lua_State L)
    {
    	NumArray *n = (NumArray*)lua_touserdata(L, 1);
    	const char *key = lua_tostring(L, 2);
    
    	printf("%s\n", key);
    	return 0;
    }
    
    luaL_Reg arraylib_m[] =
    {
    	{ "__index", l_get },
    	{ "__newindex", l_set },
    	{ "print", printarray },
    	{ NULL, NULL }
    };
    But when I do that I can no longer call print, it end up calling l_get with the error

    attempt to call field `print' (a number value)
    Anyone know I how I would go about fixing this?

    -----

    Think I found a solution, yet I cant get my head around it

    Lua/C++ : __index is always invoked even the table's field is known - Stack Overflow

    -----

    Also looking over, Programming in Lua : 28.4 feel like its missing some key things...

    Willing to pay for fix, also its for a free project (oGasai Déjà Vu - WoW 1.12.1 Vanilla Bot)
    Last edited by DarkLinux; 11-10-2017 at 01:42 AM.

Similar Threads

  1. Archaeology world object orientation?
    By gerardolm in forum WoW Memory Editing
    Replies: 29
    Last Post: 12-30-2010, 08:02 PM
  2. [Request] 1.12.1 Lua Patch
    By Saridormi in forum WoW Bots Questions & Requests
    Replies: 2
    Last Post: 08-17-2010, 11:27 PM
  3. wow lua despawning objects
    By alexeng in forum Programming
    Replies: 0
    Last Post: 05-22-2010, 07:18 PM
  4. [MaNGOS]WoW ver.1.12.1 Can't do preQ/access instances!
    By majjer in forum WoW EMU Questions & Requests
    Replies: 3
    Last Post: 04-02-2010, 03:40 PM
All times are GMT -5. The time now is 06:05 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search