Forum

> > CS2D > General > CS2D Lua Scripting Tutorial Development
Forums overviewCS2D overviewGeneral overviewLog in to reply

English CS2D Lua Scripting Tutorial Development

74 replies
Page
To the start Previous 1 2 3 4 Next To the start

old Re: CS2D Lua Scripting Tutorial Development

Lee
Moderator Off Offline

Quote
I'd be much more interested to see blazzing's tutorials Don't worry, he's much more practical while I'm more theoretical, so his tutorials will be much more helpful.

old Re: CS2D Lua Scripting Tutorial Development

SQ
Moderator Off Offline

Quote
I hope Leegao is right.
My 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.

old Re: CS2D Lua Scripting Tutorial Development

Lee
Moderator Off Offline

Quote
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
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)

old Re: CS2D Lua Scripting Tutorial Development

ilovemyself
User Off Offline

Quote
thanks to god, blazing will make tutorials not leegao.

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 .

old Re: CS2D Lua Scripting Tutorial Development

Lee
Moderator Off Offline

Quote
No, it's the difference between programming as a science and programming as a craft. Most of what I write are either too complicated to use or have no practical use. For example, see the following example.

@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
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
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

old Re: CS2D Lua Scripting Tutorial Development

ilovemyself
User Off Offline

Quote
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).

old Re: CS2D Lua Scripting Tutorial Development

Lee
Moderator Off Offline

Quote
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).


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.

old Re: CS2D Lua Scripting Tutorial Development

Noxic
User Off Offline

Quote
I'm interested in any scripting tutorial or script that can do things current script command appearingly cannot, like changing the properties of an item or clientside settings. I'm especially interested in whether or not you can manipulate bots through some obscure method, or the earlier mentioned example of changing an item property (specifically the ammo it contains)

old Re: CS2D Lua Scripting Tutorial Development

YellowBanana
BANNED Off Offline

Quote
Clientside hooking is not possible. Well it is , but i'm not gonna explain on this site how it's done.

You can control bots.. On join hook check what players are bots.. then you can control their position / ammoin in the hook: always.

old Re: CS2D Lua Scripting Tutorial Development

Noxic
User Off Offline

Quote
Yeah, but I don't feel like developing a complete scripted AI that ticks once for each bot every single frame. If I knew how to execute things clientside, maybe I could identify certain cases when the AI is in a situation where I want them to do something, and have them do that. That would help.

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

old Re: CS2D Lua Scripting Tutorial Development

SQ
Moderator Off Offline

Quote
∗ Development Update
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

old Re: CS2D Lua Scripting Tutorial Development

DoP3
User Off Offline

Quote
Blazzingxx or leegao can you explain a little about temporary menus?
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

old Re: CS2D Lua Scripting Tutorial Development

SQ
Moderator Off Offline

Quote
Oh, my tutorial already include something like this...
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
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.
To the start Previous 1 2 3 4 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview