Jump to content

Need Some Help (Pretty sure where the problems are, but not sure how to solve them)


Recommended Posts

I was using Rezecib's throwable spears code to make other weapons throwable, but was doing so in an inefficient manner and eventually ran into the problem where only a certain number of weapons would actually be throwable. I've been trying to condense it for a while now, and while I've made progress, it still has issues, though I'm pretty sure I've found all the potential spots that need fixing. I'm just having a hard time figuring out how to fix them, so any and all help is appreciated!

Here's the stuff from the modmain:

At Line 134: 

local fn = function(inst)
inst:ListenForEvent("equip", function(inst,data)
	if data.eslot==EQUIPSLOT.HANDS and data.item:HasTag("throwable") then
		local throwable = GLOBAL.SpawnSaveRecord(inst._item)

I want to make sure that this function is running every time an item is equipped, that the equip event is the right one to use (someone mentioned using equipped instead), and that throwable is being defined properly. What it should be doing is checking every time the player equips something that the item was in the hands slot and had the throwable tag, and then throwable should be defined as that item. I wrapped the rest of the functions of the modmain in this function, which hopefully is not causing any problems.

At line 171:

AddPrefabPostInit("throwable", function(inst)               --maybe this line doesn't work as intended?
	if not GLOBAL.TheWorld.ismastersim then return end			--I expect that it won't replace throwable with the actual weapon since it is a string
	inst:AddComponent('weaponthrowable')
	inst.components.weaponthrowable:SetRange(8, 10)
	inst.components.weaponthrowable:SetOnAttack(weaponthrow_onattack)
	inst.components.weaponthrowable:SetProjectile("throwable_projectile")
end)

As you can see from my comments, I'm worried about this function not working properly because throwable is a string here and I'm guessing it won't refer to the prefab of the weapon, but will instead look for a prefab called throwable. I don't know how to make sure it does that if it isn't working properly.

From the component (the comment are just leftovers from when I used to replicate rezecib's code for spears for each weapon individually):

	    local proj = SpawnPrefab(self.projectile)
	    if proj then
            proj._throwable = self.inst:GetSaveRecord() --#rezecib added to save axe data for respawning at destination
            if proj.components.projectile then
    	        proj.Transform:SetPosition(attacker.Transform:GetWorldPosition() )
				--#rezecib had to add the line below because projectile refers back to weapon instead
				proj.components.projectile.onhit = self.onattack
    	        proj.components.projectile:Throw(attacker, target, attacker)
            elseif proj.components.complexprojectile then
                proj.Transform:SetPosition( attacker.Transform:GetWorldPosition() )
                proj.components.complexprojectile:Launch(Vector3( target.Transform:GetWorldPosition() ), attacker, self.inst)
            end
            self.inst:Remove() --#rezecib added to remove the axe
	    end

I'm pretty certain that the third line there is the issue, since I never defined throwable in the component. Should I use the same function from the modmain, or is there a better way to do it here?

modmain.lua

Link to comment
Share on other sites

Hello, I'm not sure, but, I would try to put all the code you put inside the AddPrefabPostIni inside the event onEquip, if its throwable, you add the tag, and add all components like in the AddPrefabPostIni, the way it is now, it would only work if you had one prefab called "throwable" to add the components values, or you could just add these functions inside the MakeThrowable function.

Also, I'm not sure what you have inside the component file "weaponthrowable.lua" since you are calling functions of these inside you modmain.lua, and I don't know what its doing, but inside the code you post, you are not defining the component value "projectile" which you are using in the last code, if its declaring inside the weaponthrowable it should work fine after the first change I said.

If it's not working try to post the weaponthrowable file too.

Link to comment
Share on other sites

Thanks for the quick response! I've attached the component and prefab, as well as the current version of the modmain for the mod so that way there's no mysteries. I tried moving the code from the AddPrefabPostInit, but still no dice. As for the projectile thing, I never changed any of that stuff, so that shouldn't be the problem. My concern with the component is the line: proj._throwable = self.inst:GetSaveRecord(). Throwable isn't defined in the component, but I don't know if it refers to the modmain when the function that line is in is called because I've never gotten any errors saying it is undefined.

throwable_projectile.lua

weaponthrowable.lua

modmain.lua

Link to comment
Share on other sites

I took a look at the code, but dont got much time to test it out, but I didnt find anywhere that "saves" the record so you can get with "GetSaveRecord".

Not sure what this function does, but I think it would get some record that got saved beforehand, so it should save the record when it equips the weapon probably.

If you can wait, I could test it out but it would take some days for me have time for that, or you could look into the rezecibs mod and try to find out where it saves the record to get from.

Link to comment
Share on other sites

That get save record used to be 

local spear = GLOBAL.SpawnSaveRecord(inst._spear)

and in the component, it appears as

local proj = SpawnPrefab(self.projectile)
	    if proj then
            proj._spear = self.inst:GetSaveRecord() --#rezecib added to save spear data for respawning at destination

Can't say I'm too familiar with what self.inst refers to. I'm gonna try adding some lines that print text to check where my code is having issues.

Link to comment
Share on other sites

local fn = function(inst)
inst:ListenForEvent("equip", function(inst,data)
	if data.eslot==EQUIPSLOT.HANDS and data.item:HasTag("throwable") then
		print("*****************Weapon is valid****************")
		local throwable = GLOBAL.SpawnSaveRecord(inst._item)
	inst:AddComponent('weaponthrowable')
	inst.components.weaponthrowable:SetRange(8, 10)
	inst.components.weaponthrowable:SetOnAttack(weaponthrow_onattack)
	inst.components.weaponthrowable:SetProjectile("throwable_projectile")

So that line was never printed to my log, which means the problem is here. It either isn't checking at all, or is not listening for the right event.

Link to comment
Share on other sites

So I added checks here:

local function MakeThrowable(inst)
	inst:AddTag("throwable")
	print("Tag was added")
end

and here:

local fn = function(inst)
inst:ListenForEvent("equip", function(inst,data)
	print("Item was equipped")
	if data.eslot==EQUIPSLOT.HANDS and data.item:HasTag("throwable") then
		print("*****************Weapon is valid****************")
		local throwable = GLOBAL.SpawnSaveRecord(inst._item)
	inst:AddComponent('weaponthrowable')
	inst.components.weaponthrowable:SetRange(8, 10)
	inst.components.weaponthrowable:SetOnAttack(weaponthrow_onattack)
	inst.components.weaponthrowable:SetProjectile("throwable_projectile")

So despite there being well over 20 weapons, it only printed "Tag was added" 6 times, with a 7th one being added after I spawned. It's possible that it may be adding the tags only when it needs to. I haven't checked, but I would imagine that there's 6 of them available to the player right away, with the 7th being the fact that I chose woodie so lucy became a thing. Despite equipping a weapon, "Item was equipped" was never printed. Should I be using that listen line outside of that function? I'm beginning to think that whole function isn't running in the first place. Or maybe inst isn't referring to the right thing.

Edited by robert5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...