Page 1 of 1

Suggestion: attachScript, detachScript, isAttached

Posted: Tue Nov 06, 2012 8:55 am
by xantier
I am currently working on the sources about this. Instead of attaching only one script per object, every object should have their own list of scripts.

Why would we need this ? Guess you have a weapon, just a katana. You have an enchanter item, when you enchant a weapon with it, XX script is attached on the katana which contains special damage triggers in it. Well we can do it by setting only one script, but what if that katana had another script attached that contains general fixes and modifications about ALL weapons before ?

Guess you have a general script for all NPCs like NPC.js, but when you tame a pet, it will also include another script of TamedPet.js, while including NPC.js which contains important modifications for all NPCs.

And a few implementation notes about it like : last attached script is triggered first, if a trigger ends with a special keyword (i haven't decided yet), the same trigger in other scripts won't be called so it will be some kind of overriding functions.

base functions in cBaseObject.cpp

Code: Select all

SCRIPTLIST CBaseObject::GetAttachedScripts( void ) const
{
	return attachedScripts;
}

void CBaseObject::attachTrigger( UI16 trig )
{
	attachedScripts.push_back(trig);
}

void CBaseObject::detachTrigger( UI16 trig )
{
	SCRIPTLIST_ITERATOR it;
	it = std::find(attachedScripts.begin(), attachedScripts.end(), trig);

	if (it != attachedScripts.end())
		attachedScripts.erase(it);
}
cbaseobject.h

Code: Select all

SCRIPTLIST GetAttachedScripts( void ) const;
	void attachTrigger( UI16 trig );
	void detachTrigger( UI16 trig );

typedefs.h

Code: Select all

typedef std::vector<UI16>								SCRIPTLIST;
typedef std::vector<UI16>::iterator			SCRIPTLIST_ITERATOR;
typedef std::vector<UI16>::reverse_iterator			SCRIPTLIST_RITERATOR;

if possible and needed, can you help implementing this. it came a bit complicated to me, changing everything for it, iterating through attached scripts etc.

Re: Suggestion: attachScript, detachScript, isAttached

Posted: Tue Nov 06, 2012 5:50 pm
by Xuri
Having support for something like this would be awesome, and would potentially solve a lot of problems which would otherwise require a huge/complex global-script and/or cloning and duplicating functionality in a lot of different individual scripts.

I'm not sure how much help I can provide in achieving this, as I'm not really a coder :O but any help I can give, I will. I'll be in the #uox3 IRC channel most days after work (so, between ~17:30 and 02:00 EST probably) if you want to talk anything over.

Moving this to the Coder's Corner for now, btw.

Re: Suggestion: attachScript, detachScript, isAttached

Posted: Wed Nov 07, 2012 7:04 pm
by xantier
first, there must be some changes in core scripting. every trigger returns only true on success like "return ( retVal == JS_TRUE );". I think, if the trigger exists, it should return the &rval so user can override everything if a return 1 is put under the trigger. for example

onDamage(var attacker, var attacked, var basedamage)
{
...
do custom calculations, new armor damage system combat etc.
...

return 1 // so original damage checks after this trigger are halted.
return 0 // let internal calculations happen after this trigger.
return 2 // something special lol
}


i didn't think about the possible conflicting triggers in different scripts attached to the same object.

after planning everything about the modified scripting, i will start to code it.

Re: Suggestion: attachScript, detachScript, isAttached

Posted: Sun Nov 11, 2012 11:25 pm
by Xuri
One way of avoiding conflicting events in different scripts (without requiring a rewrite of all return values for all events, and possibly also a bit cheaper performance wise) could be to abort looping through the scripts attached to a given object after the desired event has been found. Meaning, if the object has four scripts attached, and each of those scripts contain the onUseChecked() JS event, code stops looping through them after it finds the first instance of onUseChecked() in whatever script gets looped through first.

Alternatively, some form of priority system where code will prioritize events in certain scripts over events in other scripts - either on a script-per-script basis, or on an event-per-event basis (somehow). This is highly hypothetical though, I don't know how such a system would/could be implemented.

Re: Suggestion: attachScript, detachScript, isAttached

Posted: Wed Nov 21, 2012 7:04 pm
by Mindless Automaton
Not as fancy as attach/detach, but..

Synchronet BBS uses JS for scripting similar to UOX. The bbs has a "load" function that prepends js to js more or less.

http://cvs.synchro.net/cgi-bin/viewcvs. ... iew=markup

I believe it is the js_load function.

I don't really know enough C to figure out how to hack such a thing into UOX though.

I was thinking about something like this for the various fruit/veggie script which all include a timer function for crop growth..

Drop the timer out into a 2nd script and then just load it in.. who knows..