View Single Post
Old 03-23-10, 04:43 PM   #2
reaper7
sim2reality
 
Join Date: Jun 2007
Location: AM 82
Posts: 2,280
Downloads: 258
Uploads: 30
Default Part 2 Tutorial

Ok onto Scripting:

Open up an empty script like:- Periscope.py (\data\Scripts\Menu\Page Attack Periscope.py)
The reason for this is that we need to tell the periscope to display the TDC Page when we go to that station.

Note: Do not just copy and paste as the scripts will not work unless proper formatted, use of proper tabbing must be used (Not spaces).


This is what is displayed when opened

**IMPORTANT NOTE** Where [TAB] is shown in scripts is just to show here in the forum that a Tab is used to indentate that line in the script to not type in [TAB]. Tabulation is very importatnat in pyton scripting to work correctly.

def InitializeScript():
[TAB] pass

def StartGame():
[TAB] pass

def EndGame():
[TAB] pass

def UnloadScript():
[TAB] pass


Nowsave a copy of this as Page TDC.py, That will setup the TDC page to be used as an overlay screen that can be imported into other screens like the Periscope and UZO.

Next open up the Script file for the screen you want your new TDC script to open in.
For example if you want your Dial that you created in the TDC Page to show in the Periscope view, we need to tell the Attack Periscope script to import our TDC page as follows:
Ok lets open up (\data\Scripts\Menu\Page Attack Periscope.py) and change it to this:

Note: Do not just copy and paste as the scripts will not work unless proper formatted, use of proper tabbing must be used (Not spaces).


# Globals

ShowTDC = True

#Page attack periscope.py

from menu import *

def InitializeScript():
[TAB] Menu.PageActivated += Menu_PageActivated
[TAB] Menu.PageDeactivated += Menu_PageDeactivated
[TAB] Menu_PageActivated( Pageattackperiscope )

def StartGame():
[TAB] pass

def Menu_PageActivated( page ):
[TAB] if page == Pageattackperiscope:
[TAB] global ShowTDC
[TAB] if ShowTDC:
[TAB] [TAB] from PageTDC import PageTDC
[TAB] [TAB] PageTDC.Visible = True

def Menu_PageDeactivated( page ):
[TAB] if page == Pageattackperiscope:
[TAB] global ShowTDC
[TAB] if ShowTDC:
[TAB] [TAB] from PageTDC import PageTDC
[TAB] [TAB] PageTDC.Visible = False

def EndGame():
[TAB] pass

def UnloadScript():
[TAB] Menu.PageActivated -= Menu_PageActivated
[TAB] Menu.PageDeactivated -= Menu_PageDeactivated


Now what’s that all about then...... Let’s break up into all its bits:

First of all anything with a # in front of it is a comment and is ignored by the script.
e.g:
#Page attack periscope.py

This is just a note showing the name of the script.
Next we have:
# Globals

Here were going to set our Globals (Variables).
We have set up one global – ShowTDC and have given it the Value True (ShowTDC = True).
Next:
def InitializeScript():
[TAB] Menu.PageActivated += Menu_PageActivated
[TAB] Menu.PageDeactivated += Menu_PageDeactivated
[TAB] Menu_PageActivated( Pageattackperiscope )

This is just initialising the script.
def Menu_PageActivated( page ):
[TAB] if page == Pageattackperiscope:
[TAB] global ShowTDC
[TAB] if ShowTDC:
[TAB] [TAB] from PageTDC import PageTDC
[TAB] [TAB] PageTDC.Visible = True

Here we are telling are importing the Page TDC into the Page Attack Periscope and making it visible.
def Menu_PageDeactivated( page ):
[TAB] if page == Pageattackperiscope:
[TAB] global ShowTDC
[TAB] if ShowTDC:
[TAB] [TAB] from PageTDC import PageTDC
[TAB] [TAB] PageTDC.Visible = False

Now the opposite we are unloading The Page TDC and making it hidden, for when we close the Page Attack Periscope screen.
def UnloadScript():
[TAB] Menu.PageActivated -= Menu_PageActivated
[TAB] Menu.PageDeactivated -= Menu_PageDeactivated

And last of all we unload the script.

So far we have only edited 2 script files:
Page TDC.py - (\data\Scripts\Menu\Page TDC.py) and,
Page Attack Periscope - (\data\Scripts\Menu\Page Attack Periscope.py)
You can back these up into another folder for use in your own Mod.


Ok that’s done let’s launch the game and head to the Periscope to see our newly installed Dial.





Are we done yet? Nope. We have a new dial on PageTDC. The game will update it. But the game will not switch it between states - Heading/Rudder.
Why? Because the original dial's 'Toggle Rudder Heading' ID is hard coded in the game. The game recognizes 'events' from that ID and responds to them. Your new 'Toggle Rudder Heading' ID means nothing to it. So you have to get creative and create some python code to make it switch states!
Where do you place this python code? In PageTDC.py.
Reopen the Page TDC.py file from earlier and edit to look like the following:
#Page TDC.py

# Globals

HeadingShown = False

def InitializeScript():

[TAB] PageTDC_HeadingRudder.Visible = True
[TAB] PageTDC_HeadingRudder_ToggleRudderHeading.Clicked += ToggleRudderHeading
[TAB] PageTDC_HeadingRudder_ToggleRudderHeading.Visible = True
# set the dial to Heading mode
[TAB] ToggleRudderHeading( None )

def StartGame():
[TAB] pass

def ToggleRudderHeading( sender ):
[TAB] global HeadingShown
[TAB] HeadingShown = not HeadingShown
[TAB] PageTDC_HeadingRudder_Heading.Visible = HeadingShown
[TAB] for item in PageTDC_HeadingRudder_Heading.Controls:
[TAB] [TAB] item.Visible = HeadingShown
[TAB] PageTDC_HeadingRudder_Rudder.Visible = not HeadingShown
[TAB] for item in PageTDC_HeadingRudder_Rudder.Controls:
[TAB] [TAB] item.Visible = not HeadingShown

def EndGame():
[TAB] pass

def UnloadScript():
[TAB] PageTDC_HeadingRudder_ToggleRudderHeading.Clicked -= ToggleRudderHeading
[TAB] Pass


Now on going back into the Game you should have a fully toggle type Dial. Click in the bow just under the Dial to switch between Heading and Rudder.


If you run the game in windowed mode and set the following file to
allow developing mode:
(\*Your User Name*\Documents\SH5\data\cfg\Main.cfg)


[DEVELOPING]
Modding=Yes
MenuEditor=Yes
DebugScripts=Yes


Now if you run the Game you can right click the Top bar and select Scripting Editor, this allows you to edit your scripts ingame.



And here’s Your fully clickable dial in game on the Periscope Screen.





Good Luck. And keep the Mod’s flowing.



Written by Reaper7 for the Silent Hunter 5 Community.



This was all possible by the continued help of TheDarkWraith


Check out his excellent Mod’s on the SH5 Mod’s Forum.



Last edited by reaper7; 03-24-10 at 09:36 AM. Reason: Inserted The following [TAB] to show use of tabs
reaper7 is offline   Reply With Quote