Script formatting

Old and generally outdated discussions, with the rare hidden gem. Enter at your own risk.

Moderators: Haplo, Lead Developers

Locked
Fallout2man
Member
Posts: 7
Joined: Sat Jan 03, 2004 8:05 am
Location: Fresno, California, USA
Contact:

Script formatting

Post by Fallout2man »

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.

Code: Select all

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.

Code: Select all

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
What do you guys think?
Elathia
Member
Posts: 7
Joined: Wed Oct 15, 2003 6:46 pm
Location: Silgrad Tower

Post by Elathia »

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.

Code: Select all

If ( variable1 )
	If ( variable2 )
		[do something]
	endif
endif

is better than

Code: Select all

If ( variable1 )
If ( variable2 )
[do something]
endif
endif


This might be a good standard for us to follow... Any other suggestions?
Fallout2man
Member
Posts: 7
Joined: Sat Jan 03, 2004 8:05 am
Location: Fresno, California, USA
Contact:

Post by Fallout2man »

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
User avatar
Cep
Member
Posts: 103
Joined: Fri Nov 14, 2003 2:28 am
Location: On a boat in the middle of nowhere
Contact:

Post by Cep »

I use a standard tabular breakdown when writing code. Simply because its easier to follow when looking for bugs or mistakes.

However I must admit to not altering my posts on the forum hehe. My apologies. :D
Locked