Object spawning in Lua problems

Status
Not open for further replies.

TehRealSalt

(σᴥσ)
Sonic Team Junior
Kart Krew™️
Code:
//The SOC. This is simple enough to read, so not going in-depth.

freeslot(
	"MT_MOBJSHADOW",
	"S_MOBJSHADOW",
	"SPR_OSHD"
)

states[S_MOBJSHADOW] = {
	sprite = SPR_THOK, //Use SPR_OSHD if the sprite is provided
	frame = A,
	tics = 1,
	nextstate = S_MOBJSHADOW,
}

mobjinfo[MT_MOBJSHADOW] = {
	spawnstate = S_MOBJSHADOW,
	dispoffset = -1,
	flags = MF_NOCLIP|MF2_SHADOW,
}

//If the "player.spawncheck" variable has been set to "false", then the object will spawn.
//If it has been spawned, then it will set the variable to "true" so it does not spawn again.

//If the shadow has been spawned and is valid, 
//then teleport the shadow to the floor (or ceiling if flipped) underneath the player's position.

//If the player dies, then set the variable to "false" and remove the shadow,
//so that a new shadow may spawn when the player respawns.

//Debug prints are included; they are supposed to print when something is working.
//Note that the P_TeleportMove ones are supposed to print every tic.

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.spawncheck == false and (player.mo.flags2 & MF2_OBJECTFLIP) and player.playerstate == PST_LIVE then
			player.spawnedshadow = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.ceilingz, MT_MOBJSHADOW)
			player.spawncheck = true
			print("Spawn check is true; a player spawned on the ceiling and the shadow has spawned accordingly.")
		elseif player.spawncheck == false and player.playerstate == PST_LIVE then
			player.spawnedshadow = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.floorz, MT_MOBJSHADOW)
			player.spawncheck = true
			print("Spawn check is true; a player spawned on the floor and the shadow has spawned accordingly.")
		end
		
		if player.spawnedshadow and player.spawnedshadow.valid and (player.mo.flags2 & MF2_OBJECTFLIP) then
			P_TeleportMove(player.spawnedshadow, player.mo.x, player.mo.y, player.mo.ceilingz)
			print("Teleporting the shadow to the ceiling beneath the player...")
		elseif player.spawnedshadow and player.spawnedshadow.valid then
			P_TeleportMove(player.spawnedshadow, player.mo.x, player.mo.y, player.mo.floorz)
			print("Teleporting the shadow to the floor beneath the player...")
		end
		
		if player.spawnedshadow and player.spawnedshadow.valid then
			print(player.name)
		end
		
		if player.spawnedshadow and player.spawnedshadow.valid and player.playerstate == PST_DEAD then
			P_RemoveMobj(player.spawnedshadow)
			player.spawncheck = false
			print("Spawn check is false; a player died and the shadow can now spawn once the player does.")
		end
	end
end)

I'm trying to create a script that spawns an object below the player that follows the player along the ground (ignoring height differences is intentional). However, I cannot figure out how to get the object to both spawn once per life and to follow the player. The closest I gotten to it is an object spawning every tic at the spawn position of the player, without it following.

Thanks in advance!
 
nil == false will return false, not true. If a value might not yet be initialized, you should use "if not value.entry" rather than "if value.entry == false".

Also, since the idea is (apparently) to spawn a new object if the set one doesn't exist, you could just directly check for "if not (player.spawnedshadow and player.spawnedshadow.valid)" to save yourself a variable.
 
Thank you so much! It works great, and especially thanks for the tip on optimizing the code.

I was experiencing a problem where the shadow would disappear for good if it went into a FOF, but I was optimizing it slightly and solved it by accident, so it's no longer the case.

Thanks again for essentially fixing this for me, because judging from this gif I recorded while making this, I know what I'm doing. :V
https://gfycat.com/UnpleasantOddEarwig
 
Last edited:
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top