Posted by
| Will Sayin
USA (17 posts) Bio
|
Message
| OK, more issues. Sorry for the long post, but I'm trying to be as detailed as possible. I'm adding in Lua scripting (Seems to be a trend around here) and I'm running into some issues:
I can get one script to work, but I can't get another-- very similar-- script to work.
Right now, all I've got going is scripts on objects. I'll be updating it so that most commands and stuff are done through Lua, but for now I just want to see if I can get some simple scripting to work.
The way I have scripts working on objects, is each object has a linked list of script structures, defined as follows:
struct LuaScript
{
char *name;
char *script;
};
The object structure is defined as:
struct dObject
{
char *name;
...
...
LIST luaScripts;
};
The function check_object_script iterates through the list and tries to match a the name of the script to the argument passed by the player:
bool check_object_script( D_OBJECT *obj, char *script )
{
ITERATOR iter;
LUASCRIPT *scriptPointer;
AttachIterator( &iter, obj->luaScripts );
while( ( scriptPointer = (LUASCRIPT*) NextInList( &iter ) ) != NULL )
{
if( !strcasecmp( script, scriptPointer->name ) )
{
run_script( scriptPointer );
return TRUE;
}
}
DetachIterator( &iter );
return FALSE;
}
If check_object_script is able to find a match for the script name, it calls run_script, which simply loads the script into the Lua state, and calls call_va (from PIL).
void run_script( LUASCRIPT *script )
{
luaL_loadbuffer( mudL, script->script, strlen( script->script ), script->name );
lua_pcall( mudL, 0, 0, 0 );
call_va( mudL, script->name, "" );
return;
}
All of this compiles fine, but it only works on some scripts, which is why I think it has something to do with some idiosyncrasy with Lua that I don't know about.
If I've got the following object:
##NEW_OBJ## 14
##SNAME## a pair of dice~
##LNAME## A pair of dice sit in the dust~
##WEARPOS## 16
##DESC## A standard pair of dice~
##ISCONT## 0
##LUA##
roll~
function roll()
comm( "mob", "You roll a " .. rand(1,6) .. " and a " .. rand(1,6) .. "." );
comm( "room", "Will rolls a pair of dice" );
end~
##END_OBJ##
You can see how it's loaded, with simple key pairs. ##LUA## marks the beginning of a lua script, "roll" is loaded as the script name (luaScript->name), and "function roll()..." is loaded as the script (luaScript->script).
This works perfectly fine, and outputs what's expected:
The Void
An inky blackness surrounds you, envelopes you, overwhelms your senses.
Nothing can be seen, nothing can be smelled, nothing can be heard. You
are completely and utterly alone, and it is terrifying.
Exits:
North.
A large oak chest sits in the corner, collecting dust.
[56/100hp Level: 4]eq
You are wearing:
a sturdy backpack on your shoulders.
a pair of dice in your left hand.
[56/100hp Level: 4]roll
You roll a 2 and a 1.
[56/100hp Level: 4]roll
You roll a 6 and a 6.
[56/100hp Level: 4]roll
You roll a 4 and a 6.
[56/100hp Level: 4]
But, giving the next object:
##NEW_OBJ## 13
##SNAME## a sturdy backpack~
##LNAME## A sturdy looking backpack sits on the ground~
##WEARPOS## 13
##DESC## Its a sturdy canvas backpack and it looks like it can hold
quite a lot of supplies.~
##ISCONT## 20
##NEW_OBJ##
##SNAME## A tattered bedroll~
##LNAME## A tattered looking bedroll provides a place for one to rest during their travels~
##WEARPOS## 16
##DESC## The bedroll is old and worn, but provides more comfort than the bare ground for the wandering
traveler. The bedroll consists of a few thick wool blankets and a canvas ground cloth, and keeps who-
ever might use it warm at night.~
##ISCONT## 0
##END_OBJ##
##LUA##
wear~
function wear()
if( ( s = getname() ) == "Will" )
then
comm( "mob", "Hey, Will!" );
else
force( "mob", "remove backpack" );
end --endif
end --end func~
##END_OBJ##
When I activate this trigger, I get told:
[56/100hp Level: 4]rem backpack
You remove a sturdy backpack and hold it in your right hand.
[56/100hp Level: 4]wear backpack
[LOG: error running function `wear': attempt to call a nil value]
You wear a sturdy backpack on your shoulders.
[56/100hp Level: 4]
Everything is done exactly the same in between the two scripts, but one work and the other does not. Any ideas? |
Will Sayin
Developer, Legends of the Darkstone
www.darkstonemud.com:5432 | Top |
|