Forum
CS2D General CS2D Lua Scripting Tutorial DevelopmentMy tutorial is usefull only to avoid questions like "How to make that or that?"
You can quote tutorial example in that case...
It doesn't learn much, but explains how Lua is working on CS2D & how it should be used.
How to implement classes, constructors, methods and fields?
Also is it possible to have function overloading?
Quote
can you explain something about metatables?
metatables are descriptor tables for objects and types within lua. Think of them as the metadata behind each object that dictates how the objects will act. Let's say we have a vector
vector = {x=0,y=3}
and we want to be able to do scalar multiplication on the said table via number*vector. We would get
stdin:1: attempt to perform arithmetic on global 'vector' (a table value)
However, if we were to redefine vector as
vector = setmetatable(vector, {__mul=function(vector,scalar) return {x=scalar*vector.x,y=scalar*vector.y} end})
then
vector*3 will yield {x=0,y=9}
If we want to create a vector "class" that generates these vector objects, we could write something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Vector = setmetatable({}, {__call=function(self, ...) return self:init(...) end}) function Vector:init(...) 	local members = { 		x = arg[1], 		y = arg[2] 	} 	local methods = { 		__mul = function(vector,c) return {x=vector.x*c,y=vector.y*c} end, 		__index = members, 	} 	return setmetatable(members, methods) end v1=Vector(3,4) print((v1*3).x) -- Yields 9
Note here that the structure of the class is defined when we initialize the object, from which we can simulate instances of classes. So in this sense, think of Lua tables as more advanced structs in C.
Also, do not misinterpret the metatables as a table of methods. Metatables only define class methods and objects, the object methods go under the members table.
Quote
Also is it possible to have function overloading?
No, by repeatedly redefining the same function, the previous function will only be destroyed and replaced by the new one. However, by using a metatable on _G (or the object table), we can redefine the __newindex(self, name, value) function to allow overloading (albeit explicitly by the naming convention)
if leegao was making tutorials it would be like this:
do this ?^!_X?Z)^'! and you get this %(!'?^=
sorry but some can be teacher and some not .
@YellowBanana - I've created a sample demonstration of functional overloading. Since lua isn't strongly typed (ie: implicit data declaration), you have to append the type of the function along with the name of the function (with a little bit of hacking, you can also do this via the parameters instead)
For example, the following
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function asdf() 	print("A") end function asdf_number(x) 	print(x) end function asdf_number_number(x, y) 	print(y) end asdf() asdf(3) asdf(3,4) asdf(4,5,3)
Will print out
A
3
4
5
Note that this example is considered to be rather advanced compsci material. Don't read too much into it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function string.split(t, b) 	local cmd = {} 	local match = "[^%s]+" 	if b then 		match = "%w+" 	end 	if type(b) == "string" then match = "[^"..b.."]+" end 	for word in string.gmatch(t, match) do 		table.insert(cmd, word) 	end 	return cmd end local __types = {number=1, string=2, table=3, boolean=4, ["function"]=5, ["nil"]=6} local _mt = getmetatable(_G) or {} _mt.__newindex = function(self, key, val) 	--Test key in form fn_type 	if type(val) ~= "function" then 		return rawset(self, key, val) 	end 	 	if not self.__registry then self.__registry = {} end 	 	local keys = key:split("_") 	local fn = keys[1] 	local types = keys 	table.remove(types, 1) 	--print(fn, #types) 	--if not type then raw fn 	for _i, type in ipairs(types) do 		if not type then 			self.__registry[fn] = {[0]=val} 			return rawset(self, key, val) 		end 		if not __types[type] then 			return rawset(self, key, val) 		end 	end 	 	--Pseudo recursive overloading 	--If not type == current, then default back 	local o_fn = rawget(self, fn) or function(...) return arg end 	local n_fn = function(...) 		local args = {...} 		for i, _type in ipairs(types) do 			if type(args[i]) ~= _type then 				return o_fn(...) 			end 		end 		return val(...) 	end 	return rawset(self, fn, n_fn) end _G=setmetatable(_G, _mt)
edited 1×, last 28.12.09 01:16:52 am
I got lost at that part
You are like a javascript obfuscator, not making things easier to understand, but making it hard to understand.
Here's a tool i found today. And completely explains you.
http://www.sugartoast.com/game/super_definition
You are too "causing disorder going or proceeding or coming after in the same direction as an effect or result from a failure to behave in a possible to foretell manner or to a possible to foretell degree or wild delusion (to a clear to the mind greater extent or degree than is common one induced by a capable of producing hallucinations drug)." (or simply confusing).
ilovemyself has written
Problem isn't the code. Problem is how you explain it. Your way of explanation is too complicated (for me).
You are like a javascript obfuscator, not making things easier to understand, but making it hard to understand.
Here's a tool i found today. And completely explains you.
http://www.sugartoast.com/game/super_definition
You are too "causing disorder going or proceeding or coming after in the same direction as an effect or result from a failure to behave in a possible to foretell manner or to a possible to foretell degree or wild delusion (to a clear to the mind greater extent or degree than is common one induced by a capable of producing hallucinations drug)." (or simply confusing).
You are like a javascript obfuscator, not making things easier to understand, but making it hard to understand.
Here's a tool i found today. And completely explains you.
http://www.sugartoast.com/game/super_definition
You are too "causing disorder going or proceeding or coming after in the same direction as an effect or result from a failure to behave in a possible to foretell manner or to a possible to foretell degree or wild delusion (to a clear to the mind greater extent or degree than is common one induced by a capable of producing hallucinations drug)." (or simply confusing).
I understand what the meaning of "confusing" entails. What I do not understand is why you would take such a personal interest in this issue. I have already explicited that I consider Blazzingxx the better tutor. As my code is completely "unexplainable" in your way of reasoning, what do you gain from the not at all subtle hints at my ability to reason? Is it perhaps your own insecurity at the ways that others treat you or do you just have an innate need to provoke others to prove your own point? I have already passively resigned to your conclusion yet you're still pushing the issue. My suggestion is for you to drop the issue.
Your the best leegao.
You can control bots.. On join hook check what players are bots.. then you can control their position / ammoin in the hook: always.
Also, when I do have the IDs for the bots on my server, how do I go at changing their current ammo? I've only found commands to checking what their current ammo is
Some Begginers section lessons.
- Begginers Section -
I.Variables
II.Strings
III.Comments
IV.If-state
V.True or False
VI.Loops
VII.Functions
VIII.Random
IX.Arrays
X.Multiple Arrays
XI.Save & Load File
i remember that ages ago i asked this question and blazzing said he had something like that in his super hero script for the inventory eh?
edited 2×, last 31.12.09 01:36:12 am
What you mean temporary menus? They should disappear after some seconds?
Or you just asking how Lua menus working?
It's not easy to explain this system.
You have to use lists to put an item, such as arrays.
Each player should have different item slots, so it's best to use multiple arrays, matrix.
Arrays in arrays... It's one more lesson...
So, you can try to make function witch initialize 2D arrays.
1
2
2
player_item = [b]func_matrix[/b](player_count,items_count,value) player_item[id][item] -- Practical use
It's a long code, so I should post whole script or you just wait until tutorial release.