Jump to content

Fourth Meter


Recommended Posts

To make the weapon work like this, you would need to add something like this to the weapon prefab after ismastersim check:

inst.components.weapon:SetOnAttack(function(inst,attacker)
	if attacker and attacker.sptnink then
    	local val = attacker.sptnink:value()
      	if val > 0 then --check the value of your netvar, change it to the limit you want to have
        	attacker.sptnink:set(val-1) -- reduce the value of the netvar by 1, change it what you want
        else
        	--make weapon blunt
        end
      end
end)

I'm not sure what you mean by  using the weapon as a blunt instrument, but I think you will be able to figure this out, otherwise let me know :)

  • Like 1
Link to comment
Share on other sites

14 hours ago, Monti18 said:

To make the weapon work like this, you would need to add something like this to the weapon prefab after ismastersim check:


inst.components.weapon:SetOnAttack(function(inst,attacker)
	if attacker and attacker.sptnink then
    	local val = attacker.sptnink:value()
      	if val > 0 then --check the value of your netvar, change it to the limit you want to have
        	attacker.sptnink:set(val-1) -- reduce the value of the netvar by 1, change it what you want
        else
        	--make weapon blunt
        end
      end
end)

I'm not sure what you mean by  using the weapon as a blunt instrument, but I think you will be able to figure this out, otherwise let me know :)

As in, they attack with the weapon itself as opposed to have the weapon fire projectiles. Pistol whiping, in a sense.

Link to comment
Share on other sites

inst.blunt_mode = false
inst.components.weapon:SetOnAttack(function(inst,attacker)
	if attacker and attacker.sptnink then
    	local val = attacker.sptnink:value()
      	if val > 0 then --check the value of your netvar, change it to the limit you want to have
        	attacker.sptnink:set(val-1) -- reduce the value of the netvar by 1, change it what you want
        	if inst.blunt_mode == true then
          		inst.components.weapon:SetProjectile("your_projectile")
          		inst.components.weapon:SetRange(your_initial_range,your_initial_range)
          	end
        elseif inst.blunt_mode == false then
          	inst.blunt_mode = true
        	inst.components.weapon:SetProjectile(nil)
          	inst.components.weapon:SetRange(nil,nil)
        end
      end
end)

I think this should work, I just added the new variable inst.blunt_mode so that the game doesn't have to reset the projectile and attackrange at each attack.

  • Like 1
Link to comment
Share on other sites

If you have a look at your badge file, you will see that you define self.owner as owner. And the owner is you, so your inst that you have. Name is also wrong, it should be sptnink, as this is as what you defined it before.

self.num.current = self.owner.sptnink:value()

This is how it should look. 

The same goes for every other line where you mention your netvar:

self.percent =  self.owner.sptnink:value()  / self.num.max

 

  • Like 1
Link to comment
Share on other sites

It's working, but the numbers are more than they're supposed to be. sptnink has a max of 100, starts at 10000, goes down by 200 every shot instead of 2. And since 200 is more than 100, the meter never visually depletes, so the player won't know how many more shots they have.

I've done everything I could think of to fix it, but nothing's working. The only thing I could figure out was that the line "self:SetPercent(val,100)" may have something to do with it.

Screenshot (3).png

Link to comment
Share on other sites

Oh I just saw that in fact it wants the percentage for the SetPercent function, so the OnUpdate function should look like this:

function self:OnUpdate(dt)
	if self.owner ~= nil and self.owner.sptnink ~= nil then
    	local percent = self.owner.sptnink:value() / self.num.max
		self:SetPercent(val,self.num.max) --100 is just an example, enter the max value that your badge should have.
	end
end

This should work, if it makes an error because of self.num.max replace it with 100.

  • Like 1
Link to comment
Share on other sites

18 minutes ago, Monti18 said:

Oh I just saw that in fact it wants the percentage for the SetPercent function, so the OnUpdate function should look like this:


function self:OnUpdate(dt)
	if self.owner ~= nil and self.owner.sptnink ~= nil then
    	local percent = self.owner.sptnink:value() / self.num.max
		self:SetPercent(val,self.num.max) --100 is just an example, enter the max value that your badge should have.
	end
end

This should work, if it makes an error because of self.num.max replace it with 100.

It causes the game to hard crash to desktop. No error message or anything. I had to go into the client log to find the error...

[00:00:29]: [string "scripts/mainfunctions.lua"]:1338: variable 'global_error_widget' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        scripts/mainfunctions.lua(1338,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(26,1) in function 'SavePersistentString'
        scripts/modindex.lua(119,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(106,1) in function 'BeginStartupSequence'
        scripts/main.lua(446,1) in function 'callback'
        scripts/modindex.lua(735,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(709,1) in function 'Load'
        scripts/main.lua(445,1) in main chunk

Link to comment
Share on other sites

function self:OnUpdate(dt)
	if self.owner ~= nil and self.owner.sptnink ~= nil then
    	local percent = self.owner.sptnink:value() / self.num.max
		self:SetPercent(percent,self.num.max) --100 is just an example, enter the max value that your badge should have.
	end
end

Oh sorry I made an error, I forgot to change val to percent, this should be good now.

  • Like 1
Link to comment
Share on other sites

That worked!

It took me awhile to post that cuz I tripled-checked to make sure my eyes weren't playing tricks on me.

Now only one thing, the regen. I'm gunna tinker around on my own first. If I run outta ideas, I'll ask for help. If I do figure it out, I'll post what I did for you to check out. To see if it's stable or not.

Thinkin' when this is said an' done, I oughtta post a quick this-is-how-its-done here so others don't have to read every little thing.

Edited by icantevenname
  • Like 1
Link to comment
Share on other sites

This is because you don't save the values of sptnink.

local function OnSave(inst,data)
	data.sptnink = inst.sptnink:value()
end

local function OnLoad(inst,data)
	if data and data.sptnink then
		inst.sptnink:set(data.sptnink)
	end
end

--masterpostinit

inst.OnLoad = OnLoad
inst.OnSave = OnSave

Add this to your character prefab and it should save the values.

If you already have OnLoad and OnSave functions, add the content of the functions into those.

  • Like 2
Link to comment
Share on other sites

If you just want it to regen over time, just add this to your character master postinit:

inst:DoPeriodicTask(1,function(inst)
	if inst.sptnink then
      	local val = inst.sptnink:value()
      	if val < 100 then
      		inst.sptnink:set(val + 1) -- 1 or whatever value you want it to regen over time, here 1/s
        end
    end
end

we check if val is smaller then 100 otherwise you will get higher values than 100.

You can play with the values and see what works best for you :)

Edited by Monti18
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

 Okay, the this-is-how-to-do-it post.

First off, you'll need to make a folder named "widgets" in the "scripts" folder. In "widgets" create a lua file and name it whatever ya want. Here, we'll call it "changethistoyourownname" so you know what to change. This down here is all you need for the badge to exist.

local Badge = require "widgets/badge" -- badge template to be used 

local changethistoyourownname  = Class(Badge, function(self, owner)
    Badge._ctor(self, nil, owner, { 225 / 255, 64 / 255, 0 / 255, 1 }) -- "nil" here is supposed to be the badge's art, but we couldn't figure it out. It'll use the default badge art without a icon.--  { 174 / 255, 21 / 255, 21 / 255, 1 } = colour in an rbg format
    self.owner = owner
    self.num.max = 100 --This number is the max of the character's badge
    self.num.current = self.num.max --Best you don't mess with this 'less ya got somethin' up your sleeve.

    owner:ListenForEvent("changethistoasimilarnamedirty",function(owner,data) --Yes, your custom name with "dirty" at the end.

        self.num.current = self.owner.changethistoasimilarname:value()
        self.percent =  self.owner.changethistoasimilarname:value()  / self.num.max
    end)     

    self:StartUpdating() -- does uhhhhhgh something
end)

function changethistoyourownname:OnUpdate(dt)
	if self.owner ~= nil and self.owner.changethistoasimilarname ~= nil then
    	local percent = self.owner.changethistoasimilarname:value() / self.num.max
		self:SetPercent(percent,self.num.max) --Don't touch this. FOR THE LUVVA YER DEITY OF CHOICE, DON'T TOUCH THIS! 'Less you wanna jack up yer numbers.
	end
end

return changethistoyourownname

Now in your modmain, put this under your character's info.

changethistoyourownname  = require "widgets/changethistoyourownname"
AddClassPostConstruct("widgets/statusdisplays", function(self)
if self.owner.prefab ~= 'whateveryoucalledyourguy' then
     return
end

self.name = self:AddChild(changethistoyourownname(self.owner))
self.name:SetPosition(-125, 75, 0) --Best to change these all to zero and start the game to see where it is. Then change the numbers one by one to get a better idea of what each number does.

end)

Now to your character's lua, go to their common_postinit and add

inst.changethistoasimilarname = net_ushortint(inst.GUID, "changethistoasimilarname", "changethistoasimilarnamedirty" )
	 --net_ushortint is the typical use of stats

	 --"name" is the name of the netvariable
	 --"namesdirty" is an Event which is called whenever this is changed

	 inst.changethistoasimilarname:set(100)
	 -- inst.name:set(number) [same as] inst.name = number

	 inst.changethistoasimilarname:value()
	 --getting the value of the net_variable 

But like this, the game won't keep track of how much of your meter is left when your save or load, resetting it to max when you load a game. So put in the following outside of any of the other tables

local function OnSave(inst,data)
	data.changethistoasimilarname = inst.changethistoasimilarname:value()
end

local function OnLoad(inst,data)
	if data and data.changethistoasimilarname then
		inst.changethistoasimilarname:set(data.changethistoasimilarname)
	end
end

And at the end of the master_postinit, put

inst.OnLoad = OnLoad
	inst.OnSave = OnSave

'Less I'm forgettin' somethin', that's it! Yer shiny new badge is all ret-2-go! It can do jus' about anythin' at this point! But that's too broad for this post, so I'm not going to cover it here. If ya ever need help, jus' ask. Not me though, I keep overestimating my programming abilities. Though I will add this, if you need to change your meter's value, it's

inst.changethistoasimilarname:set(val + 1)
--Be sure to change "inst" when necessary
--It doesn't only have to be +. It could be -. Or *. Or /. Depends on what y'er doin'.

 

  • Like 2
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...