Factory Issue

Hello,

I hope that everyone is doing well.

I have a weird issue with factories. I am using a factory so player can attack. It works fine, until I move the character around, or jump onto other platforms. Here is the code:

local pos = go.get_position() + vmath.vector3(450,110, 0) 
	local id = factory.create("#attack", pos)
	go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1000, go.EASING_LINEAR, 0.8, 0, function()
		go.delete(id)
	end)

Any ideas?

Thanks!

What exactly is the issue you’re having? Is it just not spawning at all? Any error message?

Is this code attached to the player character script or somewhere else? What is it wrapped in? (Ex: on message, on input, if/then…)

can i ask sth you want animate the attack? or what exactly? why you use factory and not a normal animation inside of atlas with a moving group of frames ? and just call the to do the animation?
and use an input for example of a button and do at update the animation

anim = hash("player_attack")
if anim ~= self.current_anim then
	msg.post("#sprite", "play_animation", { id = anim } )
	self.current_anim = anim
end

thanks

Sorry, I should have specified this, I was in such a rush!

The issue is when I jump to other platforms, or move to spots the code doesn’t like, the factory spawns nowhere near the character. But when I don’t do any of the things that causes these issues, the factory spawns right next to the character.

I did this just because I thought that a factory would be the best way to go to spawn the projectile. Would there be a better option?

I will also take a look your example, thanks!

For a projectile your way makes sense. He was likely talking about an animated melee attack, where his way might make more sense.

Are you calling the factory.create from inside a script attached to the player? Or is the player maybe sending a message to a controller object\script that has the factory component attached? Go.get_position() will get the position of the object that’s calling it. If this script is on some controller/spawner object and not the player itself, the position returned will likely be (0,0,0) plus your offset.

If the script is attached to the player, i feel like this should work as-is, with the info provided. If it is not, then you should use go.get_position(player_url), or if the player is passing a message to the script that creates the factory object, you could take the player’s position (self.position or some other custom property/variable, OR go.get_position()) and pass it as data in the message to the object that is spawning the projectile, and then that object would use message.pos (or however you pass it in the message) instead of go.get

2 Likes

The script is attached to the player, so I’m not quite sure what’s going on here. I am going to give your last idea a shot and pass the player location directly to the spawned projectile. Thank so much for the idea!

Best,
V

1 Like

Hey V, if that doesn’t solve it let me know. I’m not the expert by any stretch, but 2 sets of eyes are better than one, and in my experience the defold community is very dead at this time, what with most active members being in “across the pond” (from me) time zones. Feel free to post or dm a link to the project or a larger code example, I’ll be awake for another few hours yet.

Thanks so much, I really appreciate it! It’s a bit late for me but I will let you know how it goes come tomorrow!

Thank you so much again!

No worries. By tomorrow there’ll be smarter people than me online anyway :smile: but I’ll keep an eye on the thread in any case.

1 Like

Remember that objects will spawn without a parent game object. If the player has a parent game object the position will be wrong. Use go.get_world_position() instead.

4 Likes

yeah for projectile this way is better, but he can see for help also the defold example of war battles

where has the rocket and see how works the projectile.

thanks I hope this helps

In this case player is its own game object. I did have a question though, what is the difference between get position and get world position?

I will take a look at this, thanks! I have been a bit busy today so I haven’t been able to get to much code, but I will return to this thread once something works!

Thanks all!

Let’s say you have two game objects with ids “A” and “B”. “B” is a child of “A”. The parent game object “A” is positioned at v3(20, 20, 0). The child “B” is positioned at v3(10,5, 1) relative to the position of the parent “A”.

go.get_position("A") -- v3(20, 20, 0)
go.get_position("B") -- v3(10,5, 1) 
go.get_world_position("A") -- v3(20, 20, 0)
go.get_world_position("B") -- v3(30, 10, 1)
4 Likes

This makes sense, thanks so much! I have used a combination of everyones ideas and its starting to come together, thanks all!

1 Like

I…I never knew get world position was a thing. I’ve been hard-coding offsets into child GOs. This…changes…EVERYT…a couple of things.

6 Likes