Jump to content

Accessing the character via a widget


Recommended Posts

Hey community.  I had a working feature on my character mod break with the last update and am having trouble patching it.  Long story short, I had a widget (a series of buttons) where if you click on the button attributes of the character would change to mimic existing characters. Here's some sample code:

-- modmain
function StatusPostConstruct(self)
    local yAmount = -40
    local pos = {x = 40, y = -200, z = 0}
    for index, name in pairs({"my_custom_character","wilson","wendy","webber","winona"}) do
        self:AddChild(CharButton(self.owner, name, pos))
        pos.y = pos.y + yAmount
    end
end
AddClassPostConstruct("widgets/statusdisplays", StatusPostConstruct)

local CharButton = Class(Widget, function(self, owner, charName, pos)
    Widget._ctor(self, "Character Button")
    local buttonName = charName .. "_button.tex"
    local buttonMouseOverName = charName .. "_button_mouseover.tex"
    
    self.owner = owner
    self.hud_focus = owner.HUD.focus
    self.charName = charName

    self.button = self:AddChild(ImageButton("images/widgets/character_buttons.xml", buttonName, buttonMouseOverName, buttonName))
    self.button:SetOnClick(function() self:ChangeCharacter() end)
    self.button:SetPosition(pos.x, pos.y, pos.z)
    self.button.inst.UITransform:SetScale(.4, .4, .4)

end)

function CharButton:ChangeCharacter()
    playAs(self.owner, self.charName, false)
end

-- my_custom_character.lua
function playAs(inst, name, initial)
 	-- change character attributes blah blah blah
    ...
    inst.AnimState:SetBuild(name)
end

local master_postinit = function(inst)
	inst.playas = "my_custom_character"
end

return MakePlayerCharacter("my_custom_character", prefabs, assets, common_postinit, master_postinit, start_inv)

The self.owner object passed into StatusPostContruct used to be the actual character object that was initialized and returned inside my_custom_character.lua. Now however, it looks like it was replaced with some other representation object since I've noticed components missing from self.owner that used to be there (and self.owner.playas is nil now). The problem is I don't know anyway to have the widget access the actual character object to change stuff anymore. I thought maybe I could use a global variable outside of the callback, but doing require("prefabs/my_custom_character") would just return me a whole new sets of variables...I think? I am a total lua newbie, so let me know if there's something in the language I can do that I'm missing. Thanks for any help

Edited by darkscaryforest
better title
Link to comment
Share on other sites

I got it working by using an RPC:

-- modmain

AddModRPCHandler("ChangeCharacter", "PLAYAS", 
function(player, name)
    player.playAsCB(player, name, false)
end)

local CharButton = Class(Widget, function(self, owner, charName, pos, playas)
    Widget._ctor(self, "Character Button")
    local buttonName = charName .. "_button.tex"
    local buttonMouseOverName = charName .. "_button_mouseover.tex"
    
    self.owner = owner
    self.hud_focus = owner.HUD.focus
    self.charName = charName
    self.playas = playas

    self.button = self:AddChild(ImageButton("images/widgets/character_buttons.xml", buttonName, buttonMouseOverName, buttonName))
    self.button:SetOnClick(function() self:ChangeCharacter() end)
    self.button:SetPosition(pos.x, pos.y, pos.z)
    self.button.inst.UITransform:SetScale(.4, .4, .4)

end)

function CharButton:ChangeCharacter()
    SendModRPCToServer(MOD_RPC["ChangeCharacter"]["PLAYAS"], self.charName)
end

When I originally made this, I used the game's resurrect button widget as an example. It looks like a crap load of stuff changed in there and it is using a similar RPC. Now there's some kind of separation between client and server data here.  Or something. I have no idea. Regardless, the player object you get in the RPC callback has all the components you'd expect from the initialization player_common.lua gives you.

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...