Page 1 of 1

about script

Posted: 25 Mar 2008, 16:17
by jimao888
I herad that, the sctipc of npc talk of twmsrv is joined to the map logic; I don't know is it true;
if it is true, t want to aks, why not create a script system base NPC system;

Re: about script

Posted: 25 Mar 2008, 16:19
by Jaxad0127
jimao888 wrote:I herad that, the sctipc of npc talk of twmsrv is joined to the map logic; I don't know is it true;
if it is true, t want to aks, why not create a script system base NPC system;
NPCs are script based. What you were reading says that the scripts can be put in the map files itself, or in separate files.

Re: about script

Posted: 25 Mar 2008, 16:34
by jimao888
--------------
-- Map code --
--------------
atinit(function()
create_npc(108, 50 * 32 + 16, 19 * 32 + 16, npc1_talk, nil)
create_npc(110, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, nil)
create_npc(100, 40 * 32 + 16, 18 * 32 + 16, npc5_talk, npc_walker)
create_npc(110, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, nil)
create_npc(100, 50 * 32 + 16, 20 * 32 + 16, npc_tweakster_talk, npc_walker)
end)
--This is the NPC #1 function
--
function npc1_talk(npc, ch)
do_message(npc, ch, "Hello! I am testing .")
do_message(npc, ch, "This message is just here for testing connections.")
do_message(npc, ch, "What do you want?")
local v = do_choice(npc, ch, "Guns! Lots of guns!",
"A Christmas party!",
"To buy.",
"To sell.",
"To make a donation.")
......
( :oops: sorry to reference some one work)


I read some of the Lua script; It seems All the sctipt abut npc Is in the same script of the map area,
so What should I do if move one Npc to another map; or creat tow same Npc in to tow diffent map;

Re: about script

Posted: 25 Mar 2008, 19:43
by Crush
Jimao, your posts are really hard to read. I had to read them a couple of times before I understood what you want and I am still not sure if I understood you correctly. It would make it easier for all of us when you would read your posts and correct typing errors before submitting them.

Regarding your question: LUA allows to declare functions in different lua files. When you would have an NPC which is supposed to be included in a lot of different maps, let's take a potion salesman for example, you would put its talk and walk functions into a separate lua file. Let's call it potionseller.lua. Then you put the line "dofile (potionseller.lua)" at the top of the script of the map you want to put it on. Then you can assign the script functions defined in potionseller.lua to the NPCs created in the atinit function.

Instead of dofile it would also be possible to use the module system of LUA, but I am not that familiar with this so I don't know if it would be a better or a worse way to export commonly used NPC scripts into external files.

Re: about script

Posted: 27 Mar 2008, 22:59
by leeor_net
The Lua scripts also make it possible to include very complex scripts in maps without needing to have the code embedded within the map itself.

Perhaps:

Code: Select all

--------------
-- Map code --
--------------
is what is confusing you. I'm not sure exactly what that means but it could just mean the code that places NPC's into the maps.

Regardless, NPC code is all in Lua. The Lua code could be embedded directly in a map file like this:

Code: Select all

<object name="Elijah the Nomad" type="NPC" x="592" y="2192" width="0" height="0">
	<properties>
		<property name="SCRIPT">
			function npc_handler(npc, ch)
				do_message(npc, ch, "If I only had a tent...")
			end
		</property>
		<property name="NPC_ID">
			128
		</property>
	</properties>
</object>
Or it could refer to an external file containing the Lua code like this:

Code: Select all

<object name="Kalder NPC's" type="SCRIPT" x="0" y="0" width="0" height="0">
	<properties>
		<property name="FILENAME">/npc/kalder.lua</property>
	</properties>
</object>
It's a matter of practicality and perference. In the first case with the script embedded directly in the map, it makes much more sense to have it there. Creating a seperate file to create one NPC that says exactly one line is silly. On the other hand, the second example defines a Lua script file that contains code for a number of NPC's (about 10) some with very complex logic so it makes much more sense to leave it in an external file for easy editing.

Re: about script

Posted: 03 Apr 2008, 14:50
by jimao888
First, I'm sorry about my english, And Thanks a lot; I seems to understand.