Forum

> > CS2D > Scripts > MetaLibraries
Forums overviewCS2D overview Scripts overviewLog in to reply

English MetaLibraries

8 replies
To the start Previous 1 Next To the start

moved MetaLibraries

Informatixa
User Off Offline

Quote
MetaLibraries is a project that allows life easier for developers to add their functionality and simple function.

Current Rev: 81
Development Rev: 83

• Download:
     > file cs2d MetaLibraries (r81)

√ Initialize Library

1
dofile('sys/lua/libraries.luac')

√ Predefined Constants
     _WIN32                          -> Defined for applications for Win32 and Win64
     _WIN64                          -> Defined for applications for Win64
     __LINUX __, __UNIX__     -> Defined for applications for Linux

     BOTSDIR     -> ./bots
     GFXDIR       -> ./gfx
     MAPSDIR     -> ./maps
     SFXDIR       -> ./sfx
     SYSDIR       -> ./sys
     LUADIR       -> ./sys/lua
     SERVERDIR -> The server root directory under which is running

     __FILE__     -> The full path and filename of the file
     __DIR__      -> The directory of the file

     
1
2
3
4
5
6
7
8
--> ./sys/lua/server.lua
dofile(LUADIR .."/test/init.lua")
print(__FILE__)		-- return ./sys/lua/server.lua
print(__DIR__)		 -- return ./sys/lua

--> ./sys/lua/test/init.lua
print(__FILE__)		-- return ./sys/lua/test/init.lua
print(__DIR__)		 -- return ./sys/lua/test

Example (Old version r81 and less):
Spoiler >
edited 78×, last 13.12.13 01:16:19 am

old Re: MetaLibraries

Lee
Moderator Off Offline

Quote
The idea is novel, yet the implementation is a bit arcane, you may want to look into simplifying a few method names so that they can easily degrade back to the natural CS2D convention. Besides that, impressive work.

A few annotations on style:
Lua is a nontraditional language, as such it does not normally follow the strict naming conventions of more traditional languages such as Java or the C family. I recommend using a more or less loose hybrid between case-consistency and underscoring for consistency with the rest of the Lua stdlib.

Also, the concept of a pseudo OOP abstraction over Lua is a relatively new one. Make sure that people can adequately differentiate between static and instance methods outside of the arcane declaration of each; the following is an excerpt from player.lua:
1
2
3
4
5
6
7
8
9
function player.GetBots()
	local Table = {}
	for _, ply in pairs(player.GetAll()) do
		if ply:IsBot() then
			table.insert(Table, ply)
		end
	end
	return Table
end
This is an implicitly static declaration of a GetBots method under the player namespace.
1
2
3
function meta:UserID()
	return self.ID
end
implictly declares an "instance" method under the Player class. As both are methods under a single common object type (a pseudo class if you will), it is more rational to group both within the same namespace. (This should be easily achievable as your objects are already designed this way.)

On another note, it is usually not a good idea to litter temporary variables names throughout your global namespace without explicit cleanup after initiation.

old Re: MetaLibraries

YellowBanana
BANNED Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
function player.GetAll()
	local Table = {}
	for _, v in pairs(_player(0, "table")) do
		table.insert(Table, player.GetByID(v))
	end
	return Table	
end

-->

function player.GetAll()
	return _player(0,"table")
end

It seems like you're just copying a table.
player(0,"table") returns only existing players.

old Re: MetaLibraries

Lee
Moderator Off Offline

Quote
YellowBanana has written
It seems like you're just copying a table.

Not exactly, the table encloses a set of "instances" of his player class. But you are definitely on to something.

The danger lies in the static method
1
2
3
4
5
6
function player.GetByID(id)
	if id == 0 or not tobool(_player(id, "exists")) then return nil end
	local Table = meta
	Table.ID = id
	return Table
end

Which, due to the way that Lua passes references, will in turn render 'meta' into a static class. I'll illustrate this problem with the following example:

1
2
3
4
obj = {id=3}
proxy = obj
proxy.id = 4
assert(proxy.id ~= obj.id)

The above code will raise an assertion error because obj.id now becomes 4. The object references are passed along with the variable name so that in the above environment, the following state will actually be true:

1
assert(proxy == obj)

because you see, the reference 'proxy' and the reference 'obj' points to the same object (Think of this as borrowing references instead of owning them, something that's relatively common in all C-derived paradigms).

old Re: MetaLibraries

Apache uwu
User Off Offline

Quote
Oh thought it was Metal Libraries

1
2
3
hook.Add("join", "Welcom", function(ply)
     ply:PrintMessage(HUD_PRINTCENTER, "©000128128Welcom to my server.")
end)

Looks awesome, so...potentially using:

1
Welcom(1)

it should msg them?

old Re: MetaLibraries

Informatixa
User Off Offline

Quote
user Apache uwu has written
Oh thought it was Metal Libraries

1
2
3
hook.Add("join", "Welcom", function(ply)
     ply:PrintMessage(HUD_PRINTCENTER, "©000128128Welcom to my server.")
end)

Looks awesome, so...potentially using:

1
Welcom(1)

it should msg them?


No name is to identify when you want to remove a hook
1
hook.Remove("join", "Welcom")

but otherwise to recover the function
1
2
3
4
5
6
7
function Welcom(ply)
	ply:PrintMessage(HUD_PRINTCENTER, "©000128128Welcom to my server.")
end
hook.Add("join", "Welcom", Welcom)


Welcom(player.GetByID(1))

Sorry for my bad English

old Re: MetaLibraries

Apache uwu
User Off Offline

Quote
Oh cool, if this were on the file archive I would like it

I skimmed over some of the files--lots of finals !!
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview