Entity swap issues

Place to get help with not working mods / modding interface.
Post Reply
sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Entity swap issues

Post by sinropa »

Code: Select all

surface.create_entity{name="assembling-machine-2", position=ent.position, force="neutral", fast_replace=true, spill=true, raise_built=true, create_build_effect_smoke=true, recipe = ent.get_recipe().name}

So I have a script that grabs an entity (ent) which is an assembling machine 1

The upgrade detailed above works. Machine data (speeds name etc) update correctly. The machine becomes a 2 that looks like a 1.

Is there a reason im missing why a fast replace doesn't update the sprite? Further, GUI side the sprite is updated. So My first thought is that both sprites might remain, but attempts to destroy ent didnt help much.

Several of the properties above and on the entity seem to have no real effect. player, force, variation, spill, fast_replace, entity.color etc. - so im likely missing something.

Any help would be great.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Entity swap issues

Post by DaveMcW »

You must use the same force when fast_replacing.

Code: Select all

ent.surface.create_entity{
  name = "assembling-machine-2",
  position = ent.position,
  force = ent.force,
  fast_replace = true,
}

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

They were both set to "neutral" but I will try reading the property from ent instead. Thanks for the reply

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Entity swap issues

Post by darkfrei »

Just destroy the first one, they are clipping.
ent.destroy()

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Entity swap issues

Post by Deadlock989 »

darkfrei wrote:
Sat Jun 29, 2019 8:38 am
Just destroy the first one, they are clipping.
ent.destroy()
No, that's what fast replace is for, to automatically remove the existing entity but preserve settings, circuit network etc.
Image

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Entity swap issues

Post by darkfrei »

You can test it as console, be sure that the assembler has a recipe

Code: Select all

/c local ent = game.player.selected
ent.surface.create_entity{
  name="assembling-machine-2", 
  position=ent.position, force=ent.force, 
  fast_replace=true, 
  spill=false, 
  raise_built=true, 
  create_build_effect_smoke=true, 
  recipe = ent.get_recipe().name}
game.print(tostring(ent.valid))

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

So in my case im dealing with a list of entities.

Once I swap the entity, is it accurate that I need to remove the old one from the list and add the new entity - that is to ask are they independent internally and need to be managed?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Entity swap issues

Post by DaveMcW »

Yes, ent is no longer a valid entity after it gets upgraded.

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

I've got it working, but im basically destroying the original first.

This kinda makes me feel like the fast replace doesnt work, lol.

If anyone has a working example, I'd like to better understand this part of the API.

Technically, I shouldn't see a failure if I remove something and replace it... but there is a chance it fails and now the building is missing. And im also losing building contents, but my mod drains buildings fast so im not too concerned that the player would care.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Entity swap issues

Post by Deadlock989 »

sinropa wrote:
Sun Jun 30, 2019 12:35 am
I've got it working, but im basically destroying the original first.

This kinda makes me feel like the fast replace doesnt work, lol.

If anyone has a working example, I'd like to better understand this part of the API.

Technically, I shouldn't see a failure if I remove something and replace it... but there is a chance it fails and now the building is missing. And im also losing building contents, but my mod drains buildings fast so im not too concerned that the player would care.
You've only posted a single line of code so far and we can't possibly guess from that. Anything could be happening.

This works just fine for me, preserving settings and circuit connections etc. as you'd expect from fast replace, although the entities concerned are lamps, not assemblers:

Code: Select all

        entity.surface.create_entity{
            name = entity_to_place,
            position = entity.position,
            direction = entity.direction,
            force = entity.force,
            fast_replace = true,
            spill = false,
            create_build_effect_smoke = false,
        }
Image

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

So with your lamps, you did not need to go delete the original?

The issue with the assemblers is you have to delete the one you just made invalid (from fast replace), and you cannot destroy an invalid. So it would appear that the original and new sprite are in the same spot. But the new one is selectable, while the old is visible.

Without providing a player, I have never seen "spill" actually work. All invested resources are destroyed, which is why I felt I was doing something wrong.

I've only left out some optionals, like direction, otherwise our examples are the same.

I'll leave it for now, as playtesting 0.2.4 was fine. Its possible items with inventory behave differently than lamps.


This is what I had to do... so now that I am destroying ent first, I no longer expect fast replace or spill to work, obviously.

The lasting concerns are complete inventory loss (which I could manually perform the transfer) and then if for any reason the valid_swap check fails, we will have a missing building.

Code: Select all

	if ent.valid and target_level == 3 and name =="assembling-machine-2" then
	table.remove(global.machine_entities,k)
	ent.destroy()
	valid_swap = surface.create_entity{name="assembling-machine-3", position=position, force=force, fast_replace=true, spill=true, raise_built=true, create_build_effect_smoke=true, recipe = recipe}
			if valid_swap ~= nil then
					valid_swap.destructible = false
					valid_swap.minable = false
					--prevent player from changing trades
					valid_swap.recipe_locked = true 
					valid_swap.operable =true

			end

	end

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Entity swap issues

Post by Deadlock989 »

sinropa wrote:
Sun Jun 30, 2019 11:38 pm
So with your lamps, you did not need to go delete the original?
No. Why would I be trying to fast-replace something that isn't there any more?
Image

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

What I meant was fast replace didn't work as I expected.

IF fast replace was only meant to transfer inventory, then you would have to delete the original.

I HAD to delete the original, which seems like its not working as intended. So I was just making sure I understand it correctly... in that fast replace should have been a one and done swap.

Without the destroy call, it appeared like I had 2 assembly machines stacked on each other - 1 of which isn't valid. My understanding was this shouldn't be possible.

I didn't remove the protection from the machines (which would allow me to pick them up and see if there are actually two) so maybe ill try that when I loop back around to this issue.

Nonetheless, thanks for all the replies and trying to help.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Entity swap issues

Post by Deadlock989 »

sinropa wrote:
Mon Jul 01, 2019 8:49 pm
I didn't remove the protection from the machines
That's likely the issue, or it's the first thing I'd test. I'd be very surprised if fast-replace worked on an invulnerable entity.
Image

sinropa
Inserter
Inserter
Posts: 24
Joined: Sun Jun 09, 2019 12:36 am
Contact:

Re: Entity swap issues

Post by sinropa »

Ya its possible mineable causes it to fail, because in theory it should be going to a player's inventory.

Post Reply

Return to “Modding help”