Jump to content

Dynamic overwrite existing Assets


Recommended Posts

Dear Don't Starve community,

I do have a problem and I hope for help or information. I want to change the assets of an already existing Character in don't starve: Webber.

But I want to do this dynamically so a user can turn it of or on in the configs menu.

I was thinking about a transformation:

GetPlayer().AnimState:SetBuild("webber_new")
GetPlayer().AnimState:SetBank("wilson")
GetPlayer().SetStateGraph("SGwilson")

 There for I need to load the original assets to insert my own:

GLOBAL.require(addpath.."prefabs/webber")

table.insert(Assets, Asset("ANIM", "anim/webber_new.zip"))

But here is the twist: it doesn't work. Every-time it try to load the "Webber prefab" I don't get the table(Assets)

That means I try to infuse the nil variable "Assets" with the code above and it crashes.

I tried many different things and I have no idea why it doesn't work and I'm not sure what I could do or if this is even possible (It should be I saw many examples) but how?

Link to comment
Share on other sites

Which file are you doing this is? Also, have you defined the table yourself?

'modmain.lua' uses a special environment, and reads asset listings from the 'Assets' table, which is global in that environment (but only if you define it).

Prefab declarations pass their asset listings as the third argument to the 'Prefab' constructor, for example:

Prefab("common/inventory/genericitem", fn, Assets, Prefabs)

(The 'Prefabs' table is optional, and defines dependencies, for example the fire effect for torches.)

Link to comment
Share on other sites

3 hours ago, Arkathorn said:

Which file are you doing this is? Also, have you defined the table yourself?

'modmain.lua' uses a special environment, and reads asset listings from the 'Assets' table, which is global in that environment (but only if you define it).

Prefab declarations pass their asset listings as the third argument to the 'Prefab' constructor, for example:


Prefab("common/inventory/genericitem", fn, Assets, Prefabs)

(The 'Prefabs' table is optional, and defines dependencies, for example the fire effect for torches.)

I have tried the main file, which simply crashes. I tried an extern file and import them (modimport) and it doesn't work at all. At least it didn't crashes...

2 hours ago, Mobbstar said:

um... You can load assets straight from modmain.lua by making an "Assets" table. You can edit existing prefabs using AddPrefabPostIni("prefabname",functionname).

I know. Doesn't change the facts above. It just don't work. I can't access the table and it crashes if I try to. Even If I define a new one, it doesn't work and just crashes with no crash report. I just get some kind of frozen screen with the normal Webber.

I was thinking about a file path error, but all mod and normal assets are accessible though the same path, isn't it?

Maybe someone can hook me up with an example code?

 

Link to comment
Share on other sites

1 hour ago, PoolPartyFizz said:

Maybe someone can hook me up with an example code?

Assets = {
    Asset("ATLAS","images/imagename.xml"), --The files might be different names if you screwed around with custom atlases, but otherwise they should be identical
    Asset("IMAGE","images/imagename.tex")
}

 

Link to comment
Share on other sites

30 minutes ago, Arkathorn said:

Assets = {
    Asset("ATLAS","images/imagename.xml"), --The files might be different names if you screwed around with custom atlases, but otherwise they should be identical
    Asset("IMAGE","images/imagename.tex")
}

 

This is for overwriting Item-pictures, right? Anyway the concept is the same... I did already:

assets = {}
table.insert(assets, Asset("ANIM", "anim/webber_new.zip"))

--or

if assets then
table.insert(assets, Asset("ANIM", "anim/webber_new.zip"))
end

--or

assets = {Asset("ANIM", "anim/webber_new.zip")}

Every-time nil or crash... (except the middle one of course... in this case it didn't worked at all.) :/

 

Here my code: 

local function webberRetroLook(inst)
  if GLOBAL.IsDLCEnabled(GLOBAL.REIGN_OF_GIANTS) then
    if GetPlayer() and GetPlayer().prefab == "webber" then
      
     if type(assets) == "table" then
      print(assets)
	 else
	  local assets = {}
     end
	
	 if assets == nil then
		print("Something is going horribly wrong!")
     else
	 	table.insert(assets, Asset("ANIM", "anim/webber_new.zip"))
     end

      if type(assets) == "table" then
        GetPlayer().AnimState:SetBuild("webber_new")
        GetPlayer().AnimState:SetBank("wilson")
        GetPlayer().SetStateGraph("SGwebber")
        print(">>>true")
		-- I tried many different combinations of this commands... even without them it crashes.
      end
      
      print("table: "..assets)
      print("type"..type(assets))
      
   end
  end
end

 

Link to comment
Share on other sites

5 minutes ago, PoolPartyFizz said:

This is for overwriting Item-pictures, right? Anyway the concept is the same... I did already:

Every-time nil or crash... (except the middle one of course... in this case it didn't worked at all.) :/

Dumb question, but did you spell it all lower-case? Because the game only recognises assets in modmain.lua if the table is spelled "Assets" with a capital A.

Link to comment
Share on other sites

24 minutes ago, Mobbstar said:

Dumb question, but did you spell it all lower-case? Because the game only recognises assets in modmain.lua if the table is spelled "Assets" with a capital A.

Edit: Sorry I miss understood that what you said. I feel a bit stupid now. I'm sorry.

I tried Capital and lowercase. Doesn't matter. Crash. I feel extremely dumb. I have no idea why it crashes

Link to comment
Share on other sites

On 8.2.2016 at 10:53 PM, Arkathorn said:

Does 'anim/webber_new.zip' exist?

Also, what is your error? If it doesn't show you one, it can still be accessed from the log (found in 'Documents/Klei/DoNotStarve/log.txt').

Thank you so much^^! Yes the file exists and is working (because If I try to overwrite the original file with the name "Webber" and no code, it shows the result I want to see).

I tried to fix some of my code... I do not fully understand some of the mechanics Don't Starve is providing/using, some of my code doesn't work in separate files... anyway:

Spoiler

local function webberRetroLook(inst)
  if GLOBAL.IsDLCEnabled(GLOBAL.REIGN_OF_GIANTS) then
    if GetPlayer() and GetPlayer().prefab == "webber" then
      
      if type(Assets) == "table" then
        table.insert(Assets, Asset("ANIM", "anim/webber_legacy.zip"))
        GetPlayer().AnimState:SetBuild("webber_legacy")
        GetPlayer().AnimState:SetBank("wilson")
        --GLOBAL.require("stategraphs/SGwilson")
        --GLOBAL.GetPlayer().SetStateGraph("SGwilson")
        print(Assets[1].file)
      end
      
   end
  end
end

if GetModConfigData("nvw_d") == 1 then
  -- replace webber with Retro Webber
 AddPrefabPostInit("webber", webberRetroLook)
end

 

Here I have my code snip... OMG I'm so stupid. I adjusted the first argument of the function in the hope to catch the assets table... OF COURSE NOT... I just renamed the argument and insert my asset into the instance- table... The main problem but still exist.. I-can't-access the Assets table... :/

Spoiler

Could not find anim build webber_legacy
scripts/mods.lua(44,1) error calling PrefabPostInit: webber in mod Webber's lost Ability: 
...pps/common/dont_starve/data/scripts/entityscript.lua:20: attempt to concatenate local 'name' (a nil value)
LUA ERROR stack traceback:
        C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/entityscript.lua(20,1) in function 'LoadStateGraph'
        C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/entityscript.lua(576,1) in function 'SetStateGraph'
        C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/../mods/Webber's lost Ability/modmain.lua(175,1)
        =(tail call) ?

I never moved it and the .zip is in my modfolder: "anim/Webber_legacy.zip" it just can't access them because the table isn't accessible. 

 

Link to comment
Share on other sites

Hey @Mobbstar

Thank you for you help. I have now a dynamic Assets overwrite on wish. I used this before but I messed up a bit in my xyz code XD

I have just one last question: I moved my code in different code segments, that means for example I split my code up into specific files.

For example:

modmain.lua 

modimport "scripts/LegacyCode.lua"

Now if I load my code up it doesn't crashes or something... it just don't work. And because the log.txt doesn't say anything about an error, I don't know why... Is there something I should know about using other files in other dictionaries?

Link to comment
Share on other sites

@PoolPartyFizz using "modimport" to load files from modmain.lua is like loading a seperate, second "modmain". You still need to use GLOBAL, you need to re-declare all variables you want to use and (I think) you have access to mod-environment functions.

Use print() to check whether your code works:

print("this shows up in the log") -- this shows up in the log

print("hypothetical variable x:",x) -- hypothetical variable x:     4

Link to comment
Share on other sites

@PoolPartyFizz, it would generally be better to use this code, as long as you don't need to access modmain only functions/variables:

require "filenameminusextension" --This loads a file, and returns the return value of that file (if any). If it has already been loaded, then it will return the same value, without running the code again.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...