I've been reading over the scripting forums a bit and noticed a somewhat disturbing lack of code formatting. I thought it might be good to ask if maybe we could agree to some common ideas for TR scripts so that they might be easier to read for people who come from a programming background like myself :p
I'll give some examples of what I'd suggest as a coding style. Here are a few scripts taken from another topic in their raw form.
begin item_script
; Summon NPC to surprise attack player
; variables
short done
if ( OnActivate == 1 )
if ( done == 1 )
Activate
return
else
if ( GetDeadCount "NPC ID" == 1 )
Activate
return
else
if ( GetJournalIndex "Name of Entry" >= index here )
Set done to 1
Journal name of entry and index
Activate
Startscript "NPC_Script"
else
Activate
return
endif
endif
endif
endif
End
Begin NPC_Script
short doonce
if ( doonce == 1 )
return
endif
if (doonce == 0)
if ( GetJournalIndex "name of entry" == index no )
"NPC ID"->disable
PlaceAtPC "NPC ID" 1 128 1
StartScript "NPC_action_script"
set doonce to 1
endif
endif
end
Begin NPC_action_script
short doneIt
if ( doneIt == 0 )
if ( GetJournalIndex "name of entry" == index no )
if ( GetDeadCount "NPC ID" == 0 )
ForceGreeting
Set doneIt to 1
endif
endif
endif
if ( doneIt == 1 )
return
endif
if ( GetDeadCount "NPC ID" == 1)
if ( GetJournalIndex "Name of entry" < index no )
if ( GetJournalIndex "Name of entry" >= index no )
Journal name of entry and index no
endif
endif
endif
end
and here are what they'd look like using my coding style. I don't know if this'd make it any easier to read, since my style is a bit different from usual styles, but anyway.
begin item_script
; Summon NPC to surprise attack player
; variables
short done
if (OnActivate==1)
if (done==1)
Activate
return
else if (GetDeadCount"NPC ID"== 1)
Activate
return
else if (GetJournalIndex"Name of Entry">= index here)
Set done to 1
Journal name of entry and index
Activate
Startscript "NPC_Script"
else
Activate
return
endif
endif
endif
endif
End
Begin NPC_Script
short doonce
if (doonce==1)
return
endif
if (doonce==0)
if (GetJournalIndex"name of entry"==index no)
"NPC ID"->disable
PlaceAtPC "NPC ID" 1 128 1
StartScript "NPC_action_script"
set doonce to 1
endif
endif
end
Begin NPC_action_script
short doneIt
if (doneIt==0)
if (GetJournalIndex"name of entry"==index no)
if (GetDeadCount"NPC ID"== 0)
ForceGreeting
Set doneIt to 1
endif
endif
endif
if (doneIt==1)
return
endif
if (GetDeadCount"NPC ID"==1)
if (GetJournalIndex"Name of entry"<index no)
if (GetJournalIndex"Name of entry">=index no)
Journal name of entry and index no
endif
endif
endif
end
I agree on the formatting part, to read a script without any formatting is really a pain...
But, in these cases this might not be a problem: This forum seems to erase all tabs and spaces in the beginning of rows in messages. This does not apply to sections withing the "CODE" BB Code (#), that's why everyone who pastes a script here should use it.
I saw that you've deleted all spaces withing the parantheses, and according to SFD this might not be so good. Here's SFDs three major points on general Morrowind scripting syntax:
Use commas between parameters
If the ID contains a space, you must use quotation marks: "Object ID" better to avoid
spaces altogether: Object_ID
Get used to always Leave a space after parantheses and operators, sometimes it seems
to cause problems if you don't: if ( variable == 1 ), not: if (variable==1)
Object_ID1 -> Function, Object_Id2, not Object_ID1->Function,Object_ID2
While this doesn’t matter most of the time it generates weird and almost untraceable
errors sometimes, so you are much better off to get used to always leaving a space.
Tabulators
For your own sake, use tabs for if-elseif constructs – makes it much easier to keep track of
them, so you don’t forget an endif at the end.
Yeah, I recently noticed the scripting engine doesn't handle that very well. Most programming and scripting languages disregard these things, but I guess NetImmerse doesn't. Personally I think their scripting language needs an entire overhaul, but that's just my opinion :p
But aside from that, yeah, I use a standard four tabs, another thing that helps is to use the MB's [ code ] [ /code ] tags (without spaces in between).
That style above was basically my C/C++/PHP style, I remove all spaces except when doing mathematical operations, IE: int i = (5 + 3); or something like that. I also do an almost unheard of putting of the { bracket on the same line as the control structure/function/class definition :p
But anyway, thanks for correcting my mistake, heh. This scripting language is the most un C-like language I've ever seen, short of assembly :p