Loading...
  OR  Zero-K Name:    Password:   

learning to code

30 posts, 1037 views
Post comment
Filter:    Player:  
Page of 2 (30 records)
sort
can someone please post an example of a for in pairs loop that edits customParamiters in unit defs... i have spent weeks reading lua tutorials and searching in the games code for examples and i have tried hundreds of things.

and if you will not help me then delete my account so i can never return

or dammit atleast tell me why i suck so bad at code... and i expect a few down votes while im at it..

i want so bad to mod this game but i spend more time trying to figure out how then actually getting the mods done

for name, ud in pairs(UnitDefs) do
if ud.customParams then
for wd in pairs(ud.customParams) do
if wd.pylonrange then
wd.pylonrange = wd.pylonrange * 4
end
end
end
end

.... im really on the edge man.. i think maybe i might take up drinking
+0 / -0
9 months ago
If you are under stress for whatever reason, learning how to program is not the activity I would recommend to relax - just something to keep in mind.

I always think it is best to learn something than to obtain a result fast (helps in the future). What I assume (without knowing much Lua) is that probably UnitDefs and ud.customParams are associative arrays (associate a key to a value), and you would like to change one value of the customParams.

When you go over tables you can right "for key, value in pairs(some_array)" which is not what you have in the second for loop.

So some questions to help understanding:
- do you need to go through all elements of a table to change a specific one?
- if answer to first one is yes, how would you rewrite the second for loop to be more like the first (have a key and a value)?
+3 / -0
9 months ago
ill wait till the alcohol wears off before i look at this.. but thanks.. i cannot promise i will understand you.. but i do promise not to return to zero-k until i can edit custom params.. as to doing something less stressful i got allot of joy out of the example of how to edit unit defs on the games wiki and my stress in life decreased.. but sadly no example exist of how to edit custom defs so when my modifications had bugs i couldnt fix them.. and then people asked my why things were broken so i tried to fix the mods as best i could.. maybe your right.. sigh
+0 / -0
Relevant xkcd for programming and alcohol: https://xkcd.com/323/

Another suggestion for frustrating bugs: put a print after each line with the variables. For your example you could add (not sure about syntax or where it is shown, but probably you can find an example of that in other widgets):
  • print("name is " .. name)
  • print("ud is " .. ud)
  • print("wd is " .. wd)
etc.

Probably it will flood you with too much info, but you can just stop it after first prints and it might help you understand what the code does.
+1 / -0
chaplol
9 months ago
[screams in debugger]
+1 / -0
9 months ago
the way im currently testing code is run the game.. see nothing has changed.. change the code.. repeat hundreds of times
+0 / -0
The solution here is:
for i = 1, #UnitDefs do
local customParams = UnitDefs[i].customParams
if customParams.pylonrange then customParams.pylonrange = tostring(tonumber(customParams.pylonrange) * 4) end
end


UnitDefs is an ordered table so you can use a for i = 1, #table iterator instead. One hang up you may have when analyzing this code is that lua tables are pointers to tables in memory (or.. something like that) so as long as the table exists, referencing it will alter the original table instead of the copy of the table. Mostly the "local customParams" here is done for readability, though it can have some speed up properties. The second part of this is this weird if-then statement here. What this is saying is "if this variable is not nil (not set) then do this". The only time pylonrange will not be nil is if it is used for range of the pylon, which is exactly what you're trying to alter.

I suggest joining #modding and asking questions there or dming me. And please, do not post requests for help containing things like

quote:
and if you will not help me then delete my account so i can never return


This is just bad behavior and I do not want to see these kinds of things as I think they reflect poorly on us as a community. I get coding is frustrating, but you need to hold your composure and approach coding like it's a puzzle. You ever play something like Factorio or the Zacktronics games? Something like that. I'm willing to tutor if you want it or I can stream some of my dev work I do around here.
+4 / -0
9 months ago
quote:
the way im currently testing code is run the game.. see nothing has changed.. change the code.. repeat hundreds of times
If you just try too random things you will just get more (and more) frustrated. You need to try to understand small things (like what does pairs do? => allows you in a for loop to get key, values pairs => your second for can't work at all) and so on.

Sure, people will give you the code that does what you ask for simple things, but it will come a time where you would need to understand what is possible to add fancier things.
+1 / -0
thanks.. for the help.. sorry i think maybe i was getting a bit hysterical.. the community is not bad.. i was wrong to feel as-though nobody cared..

i think coding has made me confused and angry.. i dont feel like i have been myself lately.

im going to start back at the beginnings of learning lua and try to get a better base understanding

but in the meantime atleast even if i dont fully understand it i can use the code you wrote to implement some fixes to my mods

i plan to make many new mods.. that is after i take a few days off too cool down and reset
+3 / -0
[Spoiler]
i hate to be that guy so soon.. but this doesnt seem to work i cant see any typos and it runs ingame without errors

also i played factorio since alpha =D back when trains were new

i am interested in adding to this game so in order to do that i will need to learn allot.. to this end i am looking into training. and will most likley pay a tutor for weekly lessons
+0 / -0
chaplol
When I took a high school programming class, the way the class worked was that we would literally transcribe code from a textbook into a compiler. More often than not, the verbatim code never worked. Regardless, the goal was to make a program that could run, so were effectively forced to understand what the code was doing so that we could correct the typos and logic bugs. Weirdly, my classmates and I would were successful at picking up the language despite just transcribing. Not sure if that was the goal, but it was nonetheless a very, very useful learning method whose effectiveness surprises me to this day.

In other words: Griping about their code will get you nowhere. But fixing their code gives you an important opportunity to learn. Take the opportunity.
+1 / -0
being honest i find coding difficult.. but i am very good at some things.. we all have our preferred area of expertise.. so i found some code that does work inside someone elses mod its all i needed to fix my mods.. as thats all i wanted i am grateful. the goal others have for me im sure is a gift of sorts and indeed they may have good intentions but please understand who i am.. im a creative spirit that wanted simply to design new things.. programming was a means to this but it is not what i enjoy.. im not much of a mathematician.. sorry and thanks anyway.
+0 / -0
chaplol
Been writing code for about 20 years now and.. yeah, it's pretty tough even still. Just one of those things. For sure, syntax and similar stuff gets easier, but a lot of that overall difficulty doesn't go away. Like ZK, there's no skill ceiling. But the game wouldn't be fun if it was easy, eh? It's the same thing.

Try to find ways to make your life easier while you're writing code -- if that means writing some program to automate installation, etc, then go for it! A lot of that difficulty you feel comes from trying to figure out many things at once. So working on reducing how much stuff is going on in your head can go a very long way. Maybe that's a pen and paper, or maybe just try: https://en.wikipedia.org/wiki/Rubber_duck_debugging

You already seem persistent in getting farther, so -- keep trying! You'll get there.
+2 / -0
9 months ago
It is particularly helpful as a beginner to ask e.g. chatGPT. Just type "LUA for in pairs loop on an ordered table" and you will get a code example.
This may contain errors, but usually provides the correct syntax.
You also can ask for additional printouts on console to add to the code.

+1 / -0
my current this doesnt work list:

quote:

for i = 1, #UnitDefs do
    local customParams = UnitDefs[i].customParams
    if customParams and customParams.pylonrange then
        customParams.pylonrange = customParams.pylonrange * 4
    end
end


quote:

for name, ud in pairs(UnitDefs) do
    if ud.customParams then
        for key, value in pairs(ud.customParams) do
            if key == "pylonrange" then
                ud.customParams[key] = value * 4
            end
        end
    end
end


quote:

for name, ud in pairs(UnitDefs) do
    if ud.customParams and ud.customParams.pylonrange then
        ud.customParams.pylonrange = ud.customParams.pylonrange * 4
    end
end


quote:

thats enough for today.. 3 weeks and hundreds of attempts.. is enough

im thinking maybe i should just give up altogether
the code from others mods did not work and even ai seems to think the code is valid

these mods will never be made by me..

everything can everything
everything is 10x smaller
everything explodes based on costs



+0 / -0
9 months ago
For me the 3 code examples look like they do the same thing.

"doesnt work list" is too generic. There are multiple possible reasons, and it is important to distinguish between them:
  • "code runs, does not give an error, but does not do what I want"
  • "code runs, gives an error sometimes"
  • "code does not run, gives an error all the time"

So, not sure are you asking:
  • "how to modify the range of pylons in ZK" OR
  • "how do I modify the field pylonrange from associative array customParams from associative array UnitDefs"

+1 / -0
i think im asking how do I modify the field pylonrange from associative array customParams from associative array UnitDefs

my code runs no errors no effect on pylon range

im trying to modify pylonrange for all units in unitdefs where pylon range exsist by a factor of 4

well i was trying..
+0 / -0
i asked ai and it said this:

To modify the pylonrange field within the customParams associative array of each element in the UnitDefs associative array, you can use a loop to iterate through the elements and update the value. Here’s how you can achieve this in Lua:

Lua

for name, ud in pairs(UnitDefs) do
if ud.customParams and ud.customParams.pylonrange then
ud.customParams.pylonrange = ud.customParams.pylonrange * 4
end
end

AI-generated code. Review and use carefully. More info on FAQ.
In this code:

We iterate over each element in the UnitDefs associative array using pairs.
We check if ud.customParams exists and if pylonrange is defined within it.
If both conditions are met, we multiply the existing pylonrange value by 4.

this does not work.. so maybe im confused and i dont know how to proceed

can somone give me some code that works please?
+0 / -0
9 months ago
It is possible that the code does what you want but in order to modify "the pylon range" you must do something else as well (like zerok specific, not programming specific).

Also, in unknownrankShaman's example he did type conversions (from string to number and back). Do you know what type should pylonrange be? If you set a string "4" and the code expects the number 4, then it might not work.

For example (not saying that is the issue, just an example): if some part of the code reads pylon range field at some point at start, only once, before your code executes and you modify the value in unitDefs after, then you will not see the effect, although: the field is correct and the code is correct.
+1 / -0
thank you for helping me i will never forget your kindness

it matters far less if the code works then if people care

yes the ai also said that about the number/string when i asked about shamans code..

"Additionally, you’re converting the value to a string using tostring and then back to a number using tonumber. This conversion is unnecessary if the original value is already numeric"

however the key value is a number and not a string like shown bellow:

quote:

return { 
   energypylon = {
     customParams = {
       pylonrange = 500,
     },
   }
}

+1 / -0
Page of 2 (30 records)