Civ4 Python/import

From Civilization Modding Wiki
Revision as of 10:03, 17 July 2010 by Duha (talk | contribs) (Created page with 'import allow you to add python files to you mod. Example: adding popup to the start of turn: '''Bad way:''' Insert popup code to CvEventManager.py Big files is hard to read....')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

import allow you to add python files to you mod.

Example:

adding popup to the start of turn:

Bad way: Insert popup code to CvEventManager.py

Big files is hard to read. It is not easy to customize this code and merge to others mod.

good way Create file mymod.py Create function that runs popup (run_popup) edit CvEventManager.py:

add string after imports: import mymod.py

and string: mymod.run_popup() there it needs.

It is easy to add / remove this function (just uncomment lines) It is easy to share. It is easy to call this function in other place.

import sintax:

import executes file (if you have only functions and classes loads them to memory.)

import mymod to access functions use: mymod.foo()

from mymod import * to access functions use: foo()

from mymod import foo, goo to access functions use: foo() (some IDE has autocomplite feature and in this way shows only foo and goo. if you don use IDE use *)

from mymod import this_is_long_name_function as foo to access functions us: foo() (It is good idea to have functions with understandable name)

Import rules: Use imports at the file beginning.