Community Forum

Allow placing on not owned fields

Forum Overview >> Scripting

CategoryScripting
Created23.12.2018 18:47


Dominik C (acert) 23.12.2018 18:47
Hi,

what kind of "Utils.overwrittenFunction" must i use in an extraSource LUA to allow the placement for the current object on a Field i dont own?
I want to be able to place an object on the map on a field i dont own.



Thank U!

Bilbo Beutlin (BBeutlin) 23.12.2018 19:00
There's a mod "placeAnywhere" (or similiar). Should allow also placing on non-owned terrain (don't know exactly).

Espen K. (estyx) 23.12.2018 21:50
Solution:

PlacementScreenController.isPlacementValid = function() return true end

You can use this with PlaceAnywhere mod (which doesn't override farmland check)

Espen K. (estyx) 23.12.2018 22:11
And if you want to place it anywhere without terrain deformation:

Placeable.addPlaceableLevelingArea = function() return true end
Placeable.addPlaceableRampArea = function() return true end

Tested using this with PlaceAnywhere mod

Bilbo Beutlin (BBeutlin) 23.12.2018 22:47
Directly overwriting functions is never a good idea. One should better use the function Utils.
Imagine another mod (or even default procedure) appends, prepends or overwrites the same function. That can lead to unexpectable behaviour.

At least one should use this method only temporarily and restore the original function entry afterwards.

Espen K. (estyx) 23.12.2018 23:25
How one chooses to implement the solution is up to the user, never said it was production ready :)
I have an example of implementing it to PlaceAnywhere which uses overwrittenFunction:

https://gist.github.com/scfmod/ca3f1fef8074f5f930bd158b55b54df4

Dominik C (acert) 24.12.2018 15:21
Thaks for ur Replay!

Ur solution worked fine! I added an LUA to the object i wanted to place. In the LUA i used this code:


++++++++++ START ++++++++++++

-- forcePlaceable by Phoenix Modding. THX to estyx for Support on GDN!

forcePlaceable = {};

function forcePlaceable:loadMap(name)
PlacementScreenController.isPlacementValid = Utils.overwrittenFunction(PlacementScreenController.isPlacementValid, forcePlaceable.isPlacementValid);
end;

function forcePlaceable:deleteMap()
end;
function forcePlaceable:update(dt)
end;

function forcePlaceable:mouseEvent(posX, posY, isDown, isUp, button)
end;

function forcePlaceable:draw(forcePlaceable)
end;

function forcePlaceable:keyEvent(unicode, sym, modifier, isDown)
end;

function forcePlaceable:isPlacementValid(superFunc, ...)
return true;
end
addModEventListener(forcePlaceable);


+++++++++++ END ++++++++++++++

Well i think i need a function, that restore the original function that have overwritten before placing. So do u have a solution for a function i can call AFTER the object was placed to restore original function?


THX and Merry Christmas!


Espen K. (estyx) 24.12.2018 21:10

local forcePlaceableIsEnabled = true

function forcePlaceable:isPlacementValid(superFunc, ...)
if forcePlaceableIsEnabled then
return true
else
return superFunc(self, ...)
end

--------

And then for example use PlacementScreenController.onPlaceableBought() to set forcePlaceableIsEnabled to false.
Or you can use g_currentMission.placementController.camera.isActive to see if placement screen is active (maybe not the best solution but got tired of digging for alternatives when I needed it myself)

function forcePlaceable:isPlacementValid(superFunc, ...)
if g_currentMission.placementController.camera.isActive then
return true
else
return superFunc(self, ...)
end

Or you can use a combination of both etc. Merry xmas :)

Bilbo Beutlin (BBeutlin) 24.12.2018 21:31
For a temporary bypass you may use store and restore the orig. function entries in the manner

- rememberFunction1 = originalFunction1
- originalFunction1 = myNewFunction1
after work restore with
- originalFunction1 = rememberFunction1

If you do this within an update cycle, no problems will occur.

Dominik C (acert) 25.12.2018 11:24
Thank you, i will try it out :-)

But one thing i forgot... its fine to be able place things on not owned fields, but the landscape mode wont work on the not owned fields. i took a look to the GDN LUA Scripting documentation, but i cant find anything about the "landscape" LUAs to force the FieldValidation in the Landscape mode ? any idea too?




Note: Log in to post. Create a new account here.