Jump to content

Python scripts easier handling


Recommended Posts

  • Active+ Member

If you work with Python scripts (uiscript files) you probably were in a situation where you needed to change an element property or add a new element based on a condition/binary macro.
Here are two new functions I wrote to make this job easier for you: (the code is based on official root from 2018 I posted here)
 

## ui.py
## 1. Find: 
class PythonScriptLoader(object):

## 1. Add ABOVE:
def FindElementByName(window, name):
	if isinstance(window, dict):
		if "name" in window and window["name"] == name:
			return window
		for key in window:
			found = FindElementByName(window[key], name)
			if found is not None:
				return found
	elif isinstance(window, list) or isinstance(window, tuple):
		for item in window:
			found = FindElementByName(item, name)
			if found is not None:
				return found
				
	return None
	
def ChangeElementProperty(window, name, propertyName, propertyValue):
	element = FindElementByName(window, name)
	if element:
		if propertyName in element:
			element[propertyName] = propertyValue


## 2. Search:
		self.ScriptDictionary["LOCALE_PATH"] = app.GetLocalePath()

## 2. Add after:
		self.ScriptDictionary["FindElementByName"] = FindElementByName
		self.ScriptDictionary["ChangeElementProperty"] = ChangeElementProperty


And this is how you can use it:
 

## The example is based on minimap.py from the official uiscript

## 1. BEFORE:
window["children"][0]["children"] = window["children"][0]["children"] + [
				{
					"name" : "InGameEventButton",
					...
				},]
## 1. AFTER:
openWindow = FindElementByName(window, "OpenWindow")
if openWindow:
	openWindow["children"] = openWindow["children"] + [
                  {
                      "name" : "InGameEventButton",
                      ...
                  },]



## 2. BEFORE:
window["children"][0]["children"][5]["x"] = 0
window["children"][0]["children"][5]["y"] = 57
## 2. AFTER:
ChangeElementProperty(window, "AtlasShowButton", "x", 0)
ChangeElementProperty(window, "AtlasShowButton", "y", 57)

 

  • Metin2 Dev 2
  • Love 1
  • Love 9
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.