Skip to content

Posts from Scripts

4.8 (8 ratings)
User Avatar
12 years ago
Auto-translated
Hmm, it's strange that I didn't notice that. Thanks, I'll try it now.

Added after 1 hour 42 minutes
if GetPlayerResource(1, k1) >= res1{k1+1} then

It seems there's an error here. "attempt to call table value," or something like that. How can I fix it? (I need an array, there are many components).
User Avatar
12 years ago
Auto-translated
res1{k1+1} - the parentheses are incorrect; they should be [], if accessing by index is intended.
User Avatar
12 years ago
Auto-translated
Ah, yes. I made a mistake. Thanks!

Added 59 minutes later
Then, here's another question: can I use a value from an array for text messages? For example: "your troops + < value=army[3] >"?
User Avatar
12 years ago
Auto-translated
Yes, but they are defined in the script:
{"path to text"; army=army[3]}
which means that in the text, the tag will be replaced with the third element of the array.
User Avatar
12 years ago
Auto-translated
Okay. Thank you very much again!

Added 1 hour 29 minutes ago
I apologize, but I have another question:
-How can I calculate the total number of objects (let's say, those poor, long-suffering peasant huts) on the map?
I can assign a common name to them. But how do I output all of this into a single numerical variable?
User Avatar
12 years ago
Auto-translated
Here's how you can calculate the total number of all peasant huts on the map:
peasant_huts_n = length(GetObjectNamesByType("PEASANT_HUT"))
User Avatar
12 years ago
Auto-translated
Oh yes! I completely forgot about this function for determining the length of the array!

Added 25 minutes later
Ah, no. What if we make the task a little more complicated? Huts that belong only to player 2?
In reply to Heroist
User Avatar
12 years ago
peasant_huts_n = 0
for i,hut in GetObjectNamesByType("PEASANT_HUT") do
if GetObjectOwner(hut) == 2 then
peasant_huts_n = peasant_huts_n + 1
end
end
User Avatar
12 years ago
Auto-translated
and how does this work...?
damn, I really need to figure out how this for loop works. I just can't find the time. It's a useful thing though.
Thanks for the nth time!
In reply to Heroist
User Avatar
12 years ago
Auto-translated
Heroist
how does this work...?
damn, I really need to figure out how this for loop works. I just can't find the time. It's a useful thing.
Thanks again!

A loop with a counter. In the example above, it will execute the code for each element in the GetObjectNamesByType array. i, hut are indices, where hut represents the current index of the array element, and i - I don't know what it's for, but it doesn't work without it.
User Avatar
12 years ago
Auto-translated
i is a variable that, during the loop, receives a constant increment of one. After the loop is executed, i will contain the number of times the loop was performed (I don't remember if there is a "tick" function here, but it may be relevant in languages with this function).
User Avatar
12 years ago
Auto-translated
In this case, the loop will execute the code for all elements of the array. `i` is the key (index) of the array element; it doesn't necessarily have to be a number, and it doesn't necessarily iterate in ascending order, while `hut` is the value of the array element.

Added after 58 seconds
Ment, what kind of thing is that, what does it do, or what should it do?
User Avatar
12 years ago
Auto-translated
break, immediately terminates the loop when called.
User Avatar
12 years ago
Auto-translated
Ah, yes, that is present.
User Avatar
12 years ago
Auto-translated
Thanks for the explanations, everyone! It just seems like "for" works a little differently here than it does with its counterparts in other programming languages.

Added 5 hours and 17 minutes ago
The error "quantity cannot be negative" - could it be caused by the variable being equal to 0? Is 0 considered a negative number in scripts?

Statistics

Welcome our newest member: recijeb