Difference between revisions of "Helpful Lua Snippets"

From Frozenbyte Wiki
Jump to: navigation, search
(Created page with " This page aims at sharing helpful samples of code you might have written while creating your mods. Any help is welcome. Please use the same templates for each snippet. == G...")
 
(No difference)

Latest revision as of 10:15, 15 June 2018

This page aims at sharing helpful samples of code you might have written while creating your mods. Any help is welcome.

Please use the same templates for each snippet.

Getting Player object

function getCharacter()
        local instanceManager=common.CommonUtils.getSceneInstanceManager()
	local cm = instanceManager:findInstanceByName("TrineCharacterSelectionManagerInst")
	local character = cm:getCharacterInstanceForPlayer(0)
	return character 
end

What it does: Get the object representing the player

Parameters: None

Editor version: v2

--LeonardA-L (talk) 12:06, 15 March 2015 (UTC)

Change object position

function shift(object, dx,dy,dz)
	local toShift = object:getTransformComponent()
	local vec = VC3(dx,dy,dz)

	toShift :setPosition(toShift :getPosition()+vec)
end

What it does: Shifts an objects model

Parameters:

  • Object: the object to shift
  • dx,dy,dz: displacement values (float) for the x,y and z axis

--LeonardA-L (talk) 12:06, 15 March 2015 (UTC)

Display text

Couldn't find an Entity to display text on screen, so I adapted from gui.debug.formattedText. Can easily be tweaked to have your own text as parameter, or update a previously created text.

textWindowToggled = false

-- testing the creation utils...
function createTextWindow()	
	if (textWindowToggled) then
		textWindowToggled = false
		
		gui.WindowUtils.deleteWindow(testWindowCenter)
		testWindowCenter = nil
	else
		textWindowToggled = true
		
		local clickableInput = gui.menu.Menus.getWindowInputByName("clickableOverlayInput")
		
		-- Create a new window
		testWindowCenter = gui.WindowUtils.createWindowAnchoredToTopLeft("CenteredTest", clickableInput, -1, nil, 10, false, nil, true)
		gui.UI.stack:push(testWindowCenter.bottomWidget)

		
		local testText = [[ <b>Awesome!</b> ]]
		-- Create text
		theFormattedText = gui.TextBoxUtils.createNonLocalizedFormattedTextBox("MyTextBox", "gray", testText, true, gui.AlignmentUtils.getRightTopAlignment())

		theFormattedText.widgetForClipper:setDock(engine.gui.DockFillWidth)
		theFormattedText.formattedText:setDock(engine.gui.DockFillWidth)
		
		-- Push to the created window
		testWindowCenter.topWidget:push(theFormattedText.bottomWidget)

		return testWindowCenter
	end
end

What it does: Add a small text field to the top left corner of the page (can be tweaked into centered/right/left/bottom)

Warning: At the moment you can launch it in editor only through the console panel. But it works eventualy when exporting

--LeonardA-L (talk) 16:51, 24 April 2015 (UTC)

Forcing a character change

function setCharacterToThief()
	local cm=common.CommonUtils.getCharacterSelectionManager()
	local cha=trinebase.gameplay.TrineCharacterThief
	cm:forceCharacterChange(0,cha)
end

What it does: Sets the character to thief

Parameters: None

Editor version: v3

--LeonardA-L (talk) 14:52, 25 April 2015 (UTC)