PtokaX forum

Archive => Archived 5.1 boards => HOW-TO's => Topic started by: Naithif on 25 July, 2006, 19:29:47

Title: How to link scripts
Post by: Naithif on 25 July, 2006, 19:29:47
Hi all

I would like to ask that how can i link scripts together?  ???
I've thought of something like that there's a main lua script in the scripts folder, and some more "part" scripts in a directory, and i would like the main script to execute them, so PtokaX only load directly 1 script, but uses every function from every script.

I'll try to show it, as it's not really clear i think -->

1.) There's a main.lua in the PtokaX\scripts folder
2.) There're some other luas in a directory let's call it PtokaX\scripts\part
3.) Upon starting the hub, it executes main.lua which is the only script in PtokaX\scripts
4.) main.lua calls the other scripts in PtokaX\scripts\part directory, with all of their function working

I've only found a "require" function referring to the "part" script, but i doubt it would be enough alone, or is it?

Thanks for any help / advice  :)

(edit: correcting my english)
Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 19:41:28
require does openm the file, and executes it (same as dofile) if no other statements are given.
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 20:08:21
Hi all

So require should be enough? Thanks b?stya_elvt?rs  ;)

Good luck for everyone :)
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 20:24:04
Hi all

Errm, maybe i'm totally noob at this (that's right) but if i call a require for each script, then it only accepts the last  :(

Is there a way where all scripts can be loaded in the same require function, or it's done some other way? ???
I need to load a whole bunch of scripts (7 at least)

Thanks for any help / advice :)
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 20:57:40
Hi all

Eerrm no change, only the last is loaded :(

I've used multiple times
Ex.
dofile("path/scriptname1.lua")
dofile("path/scriptname2.lua")
dofile("path/scriptname3.lua")
dofile("path/scriptname4.lua")
To surprise, the same as with require...
Only the last worked, i've swapped them, and then the actual last worked...

I've no intention to merge all of "my-own-made" scripts into one "huge" script, i only would like to have them separated, it's easier for me to see through - if only this problem could be solved...

Let's see again, i have made some scripts, and i want to have them separated, and not to have PtokaX load 7-10+ script, i would like to use a "server" or "spider" script, that would execute all of the scripts, thus starting them...
Is there any way?  ???

Thanks for any help / advice :)
Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 21:29:16
Well, Lua 5.1 module system makes this possible. For instance, you can load scripts with declaring them as modules, placing right to the top:

module("scriptname",package.seeall) -- package.seeall is needed to tell the module that it can see the global environment

Now all under this (functions, etc.) will get into the namespace scriptname, thus can be called like scriptname.NewUserConnected(user). To call them in your spider all at once, you need to run through _G (the global environment) and check whether the given table in _G contains a table which has a NewUserConnected field that is a function value. Now these can be stuffed into another table which is looped on NewUserConnected in the spider. Hope I was clear.

-- // EDIT

It only executes the last because you keep redeclaring PtokaX global events (you always redeclare NewUserConnected for example, thus the last guy wins. :P)
Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 21:32:41
We can speak @ ro.4242.hu if English is too hard for you, but on the other hand, Hungarian is too hard for me (at least in such contexts).  8)

I am not aware of your skills in LUA, but be warned that this is not an easy part.
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 22:30:04
Hi all

Thanks, but it's not my english what's the problem here i've even made some "bit complicated" games/utils in VB and VC++ (mostly VB)...
(i've made some progz that had 2000lines of data there, but i've seen things clearer there then in lua...)

I would like to ask for an example to help thing clear up, i'm not really the one who learned lua from books, i've learned mostly by reading this forum and trying-till-it-won't-work ;D
(ATM i'm on a main script, it has many parts, that's why i need this "example")
I've started bytranslating, then modding, then making my own scripts... I've not really used the lua handbook, i mostly learned by examples of how's things done...

So, clear up, i ask for a "straight-forward-example" ( ;D) of for example 4 scripts loaded by one "spider" script. (please  :D )

(I was wondering that "daisy chain" way should work? I don't wanna try it, just asking)

Thanks for any help / advice :)

(edit: even i make mistakes writing, but every time???  ;D ;D corrected type errors)
Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 22:47:42
Sure thing. Attached.
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 22:53:54
Hi all

LoooooooooooooooooL
I don't think i even had 1% chance to get it done, even with the handbook  ;D


Thanks so much for your help B?stya :)
Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 22:57:00
Quote from: Naithif on 25 July, 2006, 22:53:54
Hi all

LoooooooooooooooooL
I don't think i even had 1% chance to get it done, even with the handbook  ;D


Thanks so much for your help B?stya :)

Well, that Main() function in the mainscript has been given birth just now, haven't experimented with package.loaded before, but this is also the new way LawMaker is gonna be structured.  ;D

-- //EDIT

I expect you to post back the results...
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 23:42:33
Hi

Eeeerrr how to tell you

There's a little problem with this script...

Well, here's what working:

script1 - not working
script2 - not working
script3 - not working
script4 - not working
script5 - unknown
script6 - it works, but the registration of the bot that's regged here's gone (appears offline) (???)
script7 - looks like it's working

Now i'm really confused  ;D

Thanks for your help anyway :)


Title: Re: How to link scripts
Post by: bastya_elvtars on 25 July, 2006, 23:52:54
Well, this is hard to track down without the code. :-P
Anyway, I am off to sleep, till then attach those scripts and we'll look into the problem tomorrow.
Title: Re: How to link scripts
Post by: Naithif on 25 July, 2006, 23:58:00
Hi

I don't really like the idea of posting my script half made  :(

Maybe i'll when i'm ready, but until then looks like i must use them separated :(
I simply don't want to merge them into 1 script :(

Thanks anyway for your help :)