Browse Source

Fixed the sample, and fixed my faulty Lua inheritance.

Nate Starkey 13 years ago
parent
commit
34e1de1a1f

+ 2 - 4
Samples/luainvaders/data/high_score.rml

@@ -47,8 +47,6 @@ function HighScore.OnRowAdd(_,_,document)
 	input = document:GetElementById('player_input')
 	input = document:GetElementById('player_input')
 	if input then
 	if input then
 		input:Focus()
 		input:Focus()
-    else
-        Log.Message(Log.logtype.warning, "highscore.rml, input is nil or doesn't exist.")
     end
     end
 end
 end
 
 
@@ -60,12 +58,12 @@ end
 	
 	
 function HighScore.OnKeyDown(event)
 function HighScore.OnKeyDown(event)
 	if event.parameters['key_identifier'] == rocket.key_identifier.RETURN then
 	if event.parameters['key_identifier'] == rocket.key_identifier.RETURN then
-		Game.SetHighScoreName(event.current_element.value)
+		Game.SetHighScoreName(event.current_element:AsType(Element.etype.input).value)
     end
     end
 end
 end
 		</script>
 		</script>
 	</head>
 	</head>
-	<body template="luawindow" onload="HighScore.OnLoad(element) Game.SubmitHighScore()" onunload="Game.SetHighScoreName('Anon')">
+	<body template="luawindow" onload="HighScore.OnLoad(document) Game.SubmitHighScore()" onunload="Game.SetHighScoreName('Anon')">
 		<datagrid id="datagrid" source="high_scores.scores">
 		<datagrid id="datagrid" source="high_scores.scores">
 			<col fields="name,name_required" formatter="name" width="40%">Pilot:</col>
 			<col fields="name,name_required" formatter="name" width="40%">Pilot:</col>
 			<col fields="colour" formatter="ship" width="20%">Ship:</col>
 			<col fields="colour" formatter="ship" width="20%">Ship:</col>

+ 1 - 1
Samples/luainvaders/data/options.rml

@@ -96,7 +96,7 @@ end
 
 
 	</script>
 	</script>
 	</head>
 	</head>
-	<body template="luawindow" onload="Window.OnWindowLoad(element) Options.LoadOptions(document)">
+	<body template="luawindow" onload="Window.OnWindowLoad(document) Options.LoadOptions(document)">
 		<form onsubmit="Options.SaveOptions(event) Window.LoadMenu('main_menu',document)">
 		<form onsubmit="Options.SaveOptions(event) Window.LoadMenu('main_menu',document)">
 			<div>
 			<div>
 				<p>
 				<p>

+ 1 - 1
Samples/luainvaders/data/pause.rml

@@ -18,7 +18,7 @@
 			}
 			}
 		</style>
 		</style>
 	</head>
 	</head>
-	<body template="luawindow" onload="Window.OnWindowLoad(element) Game.SetPaused(true)" onunload="Game.SetPaused(false)">
+	<body template="luawindow" onload="Window.OnWindowLoad(document) Game.SetPaused(true)" onunload="Game.SetPaused(false)">
 		<br />
 		<br />
 		<p>Are you sure you want to end this game?</p>
 		<p>Are you sure you want to end this game?</p>
 		<button onclick="Window.LoadMenu('high_score',document) document.context.documents['game_window']:Close()">Yes</button>
 		<button onclick="Window.LoadMenu('high_score',document) document.context.documents['game_window']:Close()">Yes</button>

+ 1 - 1
Samples/luainvaders/lua/start.lua

@@ -37,4 +37,4 @@ function Startup()
 	maincontext:LoadDocument("data/main_menu.rml"):Show()
 	maincontext:LoadDocument("data/main_menu.rml"):Show()
 end
 end
 
 
-Startup()
+Startup()

+ 28 - 12
Source/Core/Lua/LuaType.cpp

@@ -279,27 +279,43 @@ void LuaType<T>::_regfunctions(lua_State* L, int meta, int methods)
     //fill method table with methods.
     //fill method table with methods.
     for(RegType* m = (RegType*)GetMethodTable<T>(); m->name; m++)
     for(RegType* m = (RegType*)GetMethodTable<T>(); m->name; m++)
     {
     {
-        lua_pushstring(L, m->name); // ->[3] = name of function Lua side
-        lua_pushlightuserdata(L, (void*)m); // ->[4] = pointer to the object containing the name and the function pointer as light userdata
-        lua_pushcclosure(L, thunk, 1); //thunk = function pointer -> pop 1 item from stack, [4] = closure
-        lua_settable(L, methods); // represents t[k] = v, t = [methods = 1] -> pop [4 = closure] to be v, pop [3 = name] to be k
+        lua_pushstring(L, m->name); // ->[1] = name of function Lua side
+        lua_pushlightuserdata(L, (void*)m); // ->[2] = pointer to the object containing the name and the function pointer as light userdata
+        lua_pushcclosure(L, thunk, 1); //thunk = function pointer -> pop 1 item from stack, [2] = closure
+        lua_settable(L, methods); // represents t[k] = v, t = [methods] -> pop [2 = closure] to be v, pop [1 = name] to be k
     }
     }
 
 
-    lua_newtable(L); // -> table [3]
+    
+    lua_getfield(L,methods, "__getters"); // -> table[1]
+    if(lua_isnoneornil(L,-1))
+    {
+        lua_pop(L,1); //pop unsuccessful get
+        lua_newtable(L); // -> table [1]
+        lua_setfield(L,methods,"__getters"); // pop [1]
+        lua_getfield(L,methods,"__getters"); // -> table [1]
+    }
     for(luaL_reg* m = (luaL_reg*)GetAttrTable<T>(); m->name; m++)
     for(luaL_reg* m = (luaL_reg*)GetAttrTable<T>(); m->name; m++)
     {
     {
-        lua_pushcfunction(L,m->func); // -> [4] is this function
-        lua_setfield(L,-2,m->name); //[-2 = 3] -> table.name = function
+        lua_pushcfunction(L,m->func); // -> [2] is this function
+        lua_setfield(L,-2,m->name); //[-2 = 1] -> __getters.name = function
     }
     }
-    lua_setfield(L,methods, "__getters"); //[methods = 1], methods.__getters = table, pop table
+    lua_pop(L,1); //pop __getters
 
 
-    lua_newtable(L); // -> table [3]
+
+    lua_getfield(L,methods, "__setters"); // -> table[1]
+    if(lua_isnoneornil(L,-1))
+    {
+        lua_pop(L,1); //pop unsuccessful get
+        lua_newtable(L); // -> table [1]
+        lua_setfield(L,methods,"__setters"); // pop [1]
+        lua_getfield(L,methods,"__setters"); // -> table [1]
+    }
     for(luaL_reg* m = (luaL_reg*)SetAttrTable<T>(); m->name; m++)
     for(luaL_reg* m = (luaL_reg*)SetAttrTable<T>(); m->name; m++)
     {
     {
-        lua_pushcfunction(L,m->func); // -> [4] is this function
-        lua_setfield(L,-2,m->name); //[-2 = 3] -> table.name = function
+        lua_pushcfunction(L,m->func); // -> [2] is this function
+        lua_setfield(L,-2,m->name); //[-2 = 1] -> __setters.name = function
     }
     }
-    lua_setfield(L,methods, "__setters"); //[methods = 1], methods.__setters = table, pop table
+    lua_pop(L,1); //pop __setters
 }
 }
 
 
 template<typename T>
 template<typename T>