FurryEskimo Posted August 4, 2020 Share Posted August 4, 2020 So I'm coding a tent that breaks in the heat/rain, and it's working. The structure breaks when I need it to, but there's one, tiny, error. The weird thing is, the variable I'm using to determine when a tent should break is apparently applying to All of the tents, meaning all the tents break at the same time. If I reset the variable upon a tent breaking, all the other tents are reset to perfect condition again. Pretty basic variable: local breaking = 20 --(Test code) But the variable applies to All instances. TLDR: How can a variable be defined, that only applies to one instance? Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/ Share on other sites More sharing options...
FurryEskimo Posted August 5, 2020 Author Share Posted August 5, 2020 I tried giving each instance a component. I think my syntax is wrong though. Any ideas? inst:AddComponent("breaking") --Test code. (Even just this breaks the mod) --inst.components.breaking:Set(100) --inst.components.breaking.maxvalue = 100 This is where I was researching Components: Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1359925 Share on other sites More sharing options...
Mobbstar Posted August 5, 2020 Share Posted August 5, 2020 Hello! I have been summoned. Apparently the forum does that when quoting a thread. I assume you had the `local breaking = 20 --(Test code)` at the top of the file? "local" variables are local to the "scope" they are defined in. That is why `inst` is a new thing everytime the function to make one runs. If you move the "breaking" variable into the same function, it might work, but a component-based approach is probably better yes. If adding a component crashes the game, odds are the component has a syntax error or erronous setup code. Check the exact error in the logs at `Documents/Klei/DoNotStarveTogether/client_log.txt` (for what people joining a server see) and `Documents/Klei/DoNotStarveTogether/[steam_user_id]/Cluster_[saveslot_number]/Master/server_log.txt` (for what the server itself does). (Also, you could store the "breaking" variable from your original approach as `inst.breaking`. This way, it is actually part of the specific tent.) 1 Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1359933 Share on other sites More sharing options...
FurryEskimo Posted August 5, 2020 Author Share Posted August 5, 2020 (edited) While this Master/Server_log seems super useful, it produced 400 lines of reports in 17 seconds. It'll take me time to learn what I need to pay attention to in the report. As for the 'local breaking = 20', I've tried defining it in a few places. When I put it at the top of the document the mod works, but each instance of tent references it, causing the countdown clock to tick down faster and faster the more tents exist. I've been trying to define the instance variable in 'local function common_fn(bank, build, icon, tag, onbuiltfn)', since this seems to be what runs each time I build a tent. Trouble is, when I put the countdown variable inside this build-function, it always seems to crash: (my various attempts) --local breaking = 10 --inst:AddComponent("breaking") --Test code. (Breaks the mod) --local inst.breaking = 10 --inst.components.breaking:Set(100) --inst.components.breaking.maxvalue = 100 I'm able to call the local variable, but until I can define an instance variable, I can't begin trying to call it: if breaking <= 0 then Edited August 5, 2020 by FurryEskimo Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1360075 Share on other sites More sharing options...
FurryEskimo Posted August 7, 2020 Author Share Posted August 7, 2020 @Mobbstar After a lot more testing I've been able to use the finite uses variable, and could use this to control when the ice den breaks, but it's basically cheating. It's a predefined variable for the instance. My brother who understands coding says it's likely in the class, since variables are 101 in all coding, and there must be a way to define and call on new variables. Do you have any clue where variables might be being defined? It just seems like custom variables per instance Must be a thing, since these structures already have individual variables, but how to we add another? Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1360713 Share on other sites More sharing options...
Mobbstar Posted August 7, 2020 Share Posted August 7, 2020 @FurryEskimo To clarify: A variable is a name for a value. eg: breaking = 10 -- breaking is the variable, and 10 is the value A component is a bunch of code ready to be used by whatever prefabs want it. A prefab is a kind of object. eg: You can make two tents, which are two separate objects, but the same one prefab because they're made with the same code. The "finiteuses" component has a lot more than just a variable going on. It can save/load the usage and - if my memory serves - syncronises it to clients. You can look at the full code in scripts/components/finiteuses.lua, but be warned it will involve a little bit of networking. The top of the component file should list all variables defined by the component. You could also copy the component file, put it in your own mods scripts/components/ folder, rename it, and then customise it. But the point of components is to be re-usable. One of the marks of good programming is that little to no code has to be written twice! If "cheating" lets you write less to achieve more (without resulting in explosions or arrest warrant, neither of which should be the case here), then it's productive. So if you got it working with the finiteuses component, that means you got it working. If you can think of any more tests, do those to make sure it works, then you're done here. (Aside: Testing could also be automated, but Don't Starve is not suitable for that. Other programs may have "Unit Tests" and "Integration Tests", which are code specifically testing for you if other code works. Unit tests are typically fast and test only a tiny isolated part of the code, while integration tests essentially use the program automatically. Obviously, integration tests are not feasible for a video game like this, and with mods changing everything, unit tests also do more harm than good. But since testing is a slog, think about how to make testing faster anyways.) Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1360742 Share on other sites More sharing options...
FurryEskimo Posted August 8, 2020 Author Share Posted August 8, 2020 (edited) Well I got it working. Still haven't been able to define a new variable for an instance, but using an existing component worked just fine. I added finite uses and just counted them. I found all the necessary commands in the corresponding components file. Also by adding a random value to the instance's starting number of uses, I was able to ensure that the tents don't all break at the same time come spring. Ps: When finite uses reach zero it automatically calls the function to break, but even if told to drop loot, will drop none. You need to increase the number of uses (eg. to one) and then tell it to drop loot and break. Edited August 9, 2020 by FurryEskimo 1 Link to comment https://forums.kleientertainment.com/forums/topic/120699-defining-a-variable-for-an-instance/#findComment-1361232 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now