Make sure to store the character controller somewhere before switching to the god controller. Keep in mind that when you switch to the god controller you will no longer be in a vehicle. Your character controller will still be in the vehicle you entered though. Simply checking if the player left a vehicle to switch back to the character controller won't work.
What you could do is create a button upon switching to the god controller when the character is detected in a control center. If that button is pressed, switch back to the character controller that you stored somewhere. Remember that the character controller is still in the vehicle and as far as I'm aware there's no way to force a character to exit a vehicle. There could be a flag that you flip after pressing the button. If the flag is set, prevent the player from getting switched back to the god controller. Flip the flag back once you've detected the character is out of the vehicle.
FreeER basically covered the things you could do about limiting building and movement range, don't think there's much else to say about that. I will add something about building limits though. You could create a table/dictionary at the start of a game with some predefined limits set. The key would be the entity name and the value would be another table containing limit and count. If you want to have a default limit for a bunch of building types, you can add a metatable with an __index defined such that it checks if the key exists in the table and if it doesn't then create a new table and finally return the value. It could look a little like this.
Code: Select all
local buildings = setmetatable({},
{__index = function(table, key)
if not rawget(table, key) then
rawset(table, key, {limit=100, count = 0})
end
return rawget(table, key)
end})
buildings["assembling-machine"] = {limit=300, count = 0}
-- ...more data
Further expanding on this, we could make getting count slightly easier by giving the subtables a metatable with __len defined to return the count. We could just have a function that tells us if we are at the limit too. It's quite likely we'll never want to modify limit (except perhaps on version updates) and we are probably only going to increment/decrement count by 1. Let's do something about that.
Code: Select all
-- Probably could have a better name - too lazy to come up with one atm
local Counter = {}
function Counter:new(limit, count)
local instance = {}
local limit = limit
local count = count or 0
function instance.getlimit()
return limit
end
function instance.atlimit()
return count == limit
end
function instance.increment()
count = count + 1
end
function instance.decrement()
count = count - 1
end
setmetatable(instance,
{__len = function(table)
return count
end,
__newindex = function() end})
return instance
end
We still have the ability to change the limits in updates by creating a new instance with the count of the previous instance. Let's modify the previous buildings table slightly to reflect the changes.
Code: Select all
local buildings = setmetatable({},
{__index = function(table, key)
if not rawget(table, key) then
rawset(table, key, Counter:new(100))
end
return rawget(table, key)
end})
buildings["assembling-machine"] = Counter:new(300)
-- ...more data
Now we have that out of the way. Let's have a very quick look at how we would use this with onentitybuilt.
Code: Select all
game.onevent(defines.events.onentitybuilt, function(event)
local ent = event.createdentity
local counter = buildings[ent.name]
if not counter.atlimit() then -- or... if #counter < counter.getlimit() then
counter.increment()
else
-- Do stuff FreeER mentioned in the previous post
end
end)
You can take it further by having the limits set per control center instead of for everything. But I'm just going to leave it at that for now. Hope all this was helpful in some way.