Jump to content

How to get component property from the client?


Recommended Posts

I've read the starting guides to modding and some explications for the replica and classifieds, but still haven't found a way to make it work:

local healthvalue = item.components.edible && item.components.edible.healthvalue 

It worked when playing alone in my server, but if I'm playing multiplayer or in a game with caves it doesn't work.

Link to comment
Share on other sites

I found a workaround in other mod (Item Info), it doesn't seem the best way, but it works for me:

local prefabCache
function getPrefabCopy(prefab)
    if prefabCache[prefab] == nil then
        local isMasterSim = GLOBAL.TheWorld.ismastersim
        GLOBAL.TheWorld.ismastersim = true
        prefabCache[prefab] = GLOBAL.SpawnPrefab(prefab)
        GLOBAL.TheWorld.ismastersim = isMasterSim
    end
    return prefabCache[prefab]
end

-- then use getPrefabCopy("axe").components

 

Link to comment
Share on other sites

hm.. if a popular mod does this I guess it must work, although I have no clue why it works, it looks like "we are not server, so what to do? ah change a variable to pretend we are server!" :D  But yes... also clients can "spawn" things, they just wont be visible for others.  And of course you can not change or get any values this way, you only get default values from an ordinary axe in this case. And since the SpawnPrefab thing is not removed (obviously for cache reasons) you as the client will most likely see a bunch of items at position 0,0,0 (exaclty in the middle of the map).

How to deal with the missing components depends on what you want to achieve. If you want to check if something is a eg a tool or can do sth., you can check for Tags.
And then there are netvars and RPC, like described in that starting guide:
https://forums.kleientertainment.com/forums/topic/47353-guide-getting-started-with-modding-dst-and-some-general-tips-for-ds-as-well/

I always forget how to use them and when to use what, but I alreday made a few mods using this and I can just look up my code from there :D Eg. you can take a look at my Home Base Bonus mod. The server uses netvars to make an icon display at client side if the base is complete. And I think I also use RPC somewhere. https://steamcommunity.com/sharedfiles/filedetails/?id=738448215

Link to comment
Share on other sites

This is my latest revision on the code:

 

local prefabCache
function getPrefabCopy(prefab)
    if prefabCache[prefab] == nil then
        local isMasterSim = GLOBAL.TheWorld.ismastersim
        GLOBAL.TheWorld.ismastersim = true
        local copy = GLOBAL.SpawnPrefab(prefab)
        local cache = {prefab = prefab, components = {}}
        if copy.components.finiteuses then
            local ____ = copy.components.finiteuses
            local current = ____.current
            local total = ____.total
            local consumption = ____.consumption
            cache.components.finiteuses = {current = current, total = total, consumption = consumption}
        end
        if copy.components.tool then
            local ____ = copy.components.tool
            local actions = ____.actions
            cache.components.tool = {actions = actions}
        end
        if copy.components.weapon then
            local ____ = copy.components.weapon
            local damage = ____.damage
            cache.components.weapon = {damage = damage}
        end
        if copy.components.armor then
            local ____ = copy.components.armor
            local absorb_percent = ____.absorb_percent
            local condition = ____.condition
            local maxcondition = ____.maxcondition
            cache.components.armor = {absorb_percent = absorb_percent, condition = condition, maxcondition = maxcondition}
        end
        if copy.components.healer then
            local ____ = copy.components.healer
            local health = ____.health
            cache.components.healer = {health = health}
        end
        if copy.components.edible then
            local ____ = copy.components.edible
            local healthvalue = ____.healthvalue
            local hungervalue = ____.hungervalue
            local sanityvalue = ____.sanityvalue
            local ismeat = ____.ismeat
            local foodtype = ____.foodtype
            cache.components.edible = {healthvalue = healthvalue, hungervalue = hungervalue, sanityvalue = sanityvalue, ismeat = ismeat, foodtype = foodtype}
        end
        copy:Remove()
        GLOBAL.TheWorld.ismastersim = isMasterSim
        prefabCache[prefab] = cache
    end
    return prefabCache[prefab]
end

 

Link to comment
Share on other sites

Please tell me what you want to achieve with this information.
I mean you spawn a new prefab (it is no "copy"), this new prefab has default values and of course 100% finiteuses and so on. So I see absolutely no usecase for this (although I did not look at the code from Item Info, since this mod is working I guess, there must be a usecase)

beside that, since you save the information in a new variable cache, you don't need the spawned prefab anymore. So you can do copy:Remove() at the end of the function.

edit:
had a short look at iteminfo. And it is as I said, the "copy" is only the beginning. Item Info then starts to calculate everything with its own code, so you could say he copied the relevant game code (the one that runs on the server) and calculates it now also for the client and also gets some values from replicas. This also means that if the devs of the game change some calculations, item info must update his calculations to match them. Since it was not updated a long time, chances are high some values it will show are wrong.
So in case you want to make something similar to item info, you still have alot of work to do.

edit2:
I just remembered that using netvars and RPC is no solution, if you want a "client_only" mod.  Some things are impossible to achieve as client_only, or you have to copy alot of calculations from the game code, like item info did.

Edited by Serpens
Link to comment
Share on other sites

On 7/29/2019 at 4:36 AM, Serpens said:

beside that, since you save the information in a new variable cache, you don't need the spawned prefab anymore. So you can do copy:Remove() at the end of the function.

I already do this, look at the end of the function.

 

 

On 7/29/2019 at 4:36 AM, Serpens said:

 

Please tell me what you want to achieve with this information.

 

I use it in my mod to select the best tool for the job, I need not only the usage percent  but also the armor reduction, weapon damage and others. I infer other values from the usage percent as well https://github.com/danielpza/dst-omnikey/blob/master/src/modmain.ts#L176-L185

 

On 7/29/2019 at 4:36 AM, Serpens said:


So in case you want to make something similar to item info, you still have alot of work to do.

The thing I did similar to Item Info is creating a prefab to cache it's components, I don't need/use/calculate other values like Item Info  does

Edited by danielpa
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...