On_PrePlayer_Mined_Entity entity cannot be table.removed?

Place to get help with not working mods / modding interface.
Post Reply
iUltimateLP
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sun May 24, 2015 4:41 pm
Contact:

On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by iUltimateLP »

Hey guys, Im working at a simple teleporter mod. I already did all the mechanics such as linking teleporters with a specific tool and going through the teleporters, but now I got a problem.
So I carry all teleporter links inside a table called "links". A entry inside this table looks like this: links[1] = {a=teleporter_entity_1, b=teleporter_entity_2}.
Nothing special. But now I need to break a teleporter link once I mine a teleporter. So I think the right event for that would be the On_PrePlayer_Mined_Entity cause it gives me the entity which Im going to remove, unlike the On_Player_Mined_Item . I need this entity handle to remove it from the links. So heres the code:

Code: Select all

game.on_event(defines.events.on_preplayer_mined_item,
	function(event)
		game.player.print(tostring(event.entity.name))
		if event.entity.name == "TeleporterEntity" then
			if isLinked(event.entity) then
				game.player.print("link detected! breaking...")
				breakLinks(event.entity)
			end
		end
	end
)
The isLinked function:

Code: Select all

function isLinked(teleporter)
	state = false
	for k,v in ipairs(links) do
		if teleporter == links[k].a or teleporter == links[k].b then
			state = true
		end
	end
	return state
end
The breakLinks function:

Code: Select all

function breakLinks(someTeleporter)
	if isLinked(someTeleporter) then
		table.remove(links, findBside(someTeleporter))
		table.remove(links, someTeleporter[1])
	end
end
So that should remove first the opposite linked teleporter with findBside and then the entity itself, and I gave it the event.entity variable coming from the event, but it keeps saying:

Code: Select all

Error while running the event handler: __Stuff__/control.lua:46: bad argument #2 to 'remove' (number expected, got table)
Can anybody help me? Thanks!

Edit: Oh and does anybody know how I can story the links within the map so I dont need to relink those portals at every load again?

User avatar
ThaPear
Fast Inserter
Fast Inserter
Posts: 226
Joined: Fri May 30, 2014 8:05 am
Contact:

Re: On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by ThaPear »

The function table.remove accepts an index and removes the object at that index from the table.

Imagine you insert entity 1-5 into a table, you'll have the table

Code: Select all

ents = {ent1, ent2, ent3, ent4, ent5}
To remove ent3 you'll have to do table.remove(ents, 3).
The table is now:

Code: Select all

ents = {ent1, ent2, ent4, ent5}
To then remove ent5 you'll have to do table.remove(ents, 4).

To retain the links after a load or save you can use the 'global' table, this table is saved and restored every time the game is saved/loaded.
You could do this by replacing any reference to the 'links' table to 'global.links'.

iUltimateLP
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sun May 24, 2015 4:41 pm
Contact:

Re: On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by iUltimateLP »

ThaPear wrote:The function table.remove accepts an index and removes the object at that index from the table.

Imagine you insert entity 1-5 into a table, you'll have the table

Code: Select all

ents = {ent1, ent2, ent3, ent4, ent5}
To remove ent3 you'll have to do table.remove(ents, 3).
The table is now:

Code: Select all

ents = {ent1, ent2, ent4, ent5}
To then remove ent5 you'll have to do table.remove(ents, 4).

To retain the links after a load or save you can use the 'global' table, this table is saved and restored every time the game is saved/loaded.
You could do this by replacing any reference to the 'links' table to 'global.links'.
Wow really thanks! Now my breaking function:

Code: Select all

function breakLinks(someTeleporter)
	if isLinked(someTeleporter) then
		table.remove(links, arrayGetIndex(links, findBside(someTeleporter)))
		table.remove(links, arrayGetIndex(links, someTeleporter))
	end
end
and the array get index:

Code: Select all

function arrayGetIndex(array, item)
	for k,v in ipairs(array) do
		if v == item then return k end
	end
	return nil
end
Just writing for maybe further users with this question. About saving and loading, I will post when I updated my script!

OK did it now, but I get this error: Error while running the event handler: __Stuff__/control.lua:31: bad argument #1 to 'ipairs' (table expected, got nil)
at line 31this is called:

Code: Select all

	for k,v in ipairs(global.links) do

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by orzelek »

In the on init and on load events write line like this:

Code: Select all

global.links = global.links or {}
It will initialize the global.links table if it's not yet initialized.

iUltimateLP
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sun May 24, 2015 4:41 pm
Contact:

Re: On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by iUltimateLP »

orzelek wrote:In the on init and on load events write line like this:

Code: Select all

global.links = global.links or {}
It will initialize the global.links table if it's not yet initialized.
Great thanks! Now will do all that multiplayer related stuff like changing game.player into game.players[event.playerindex]

iUltimateLP
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Sun May 24, 2015 4:41 pm
Contact:

Re: On_PrePlayer_Mined_Entity entity cannot be table.removed?

Post by iUltimateLP »

So I have another problem with my teleporter mod... How do I make this function for multiplayer?
So the main functions with teleporting are made inside the ontick event, but that does not give the playerindex back, so how do I get the player(index) to make things with him?

Post Reply

Return to “Modding help”