Functions - add, subtract

0 favourites
  • 6 posts
From the Asset Store
Add SubRip (SRT) subtitles to your videos in Construct 3
  • Hello Constructniacs!

    As I work on my "Country Management" I have few issues with my projecct wwhat I'm solving right now. But there is one what I can't solve y myself and I'd like to hear some opinions of yours!

    I'm using functions for buildings (Array XYZ for building costs and production per 2s) but I use also this array for Military layout (Soldier price, -gold per 2s, -food per 2s).

    When I had only buildings, everything was fine. My GoldTX showed me CurrentGold & " Gold" & newline & array.At(1,GoldUp,1) & " Gold each " & TickForProd & " seconds"

    When I create new conditions for Soldiers - 1 soldier cost 2 people, 20 gold and every TickForProd cost 20g and 15 food, my GoldTX show me NaN Gold and NaN food........ Any idea how to solve this?

    Army script is now just this: System -> Army > 0 ->> Subtrac from tokenat(array.At(11,0,1+Army1),1,",") from CurrentGold ; Subtract from tokenat(Array.At(11,0,1),2,",") from Food

  • Tokenat returns a string. Cant substract/add from/to a string. Got to translate the string to a number first. With int().

    No sure why you use a string for this in the array. And not sure why it has to be 3D.

    For me, it is easyer to store numbers in the array. Like.

    Store an ID on the X-axis that pairs with the house or the soldiier. So you know wich place on the X-axis is witch house or soldier.

    Now, if we write the positions in the array as (a,b) .... (yes just a 2D array) ...

    Then all values on the X-axis lay on positions .... (a = some ID, b = zero.)

    Now 'b' represents a whole big world we can use.

    Just note the 'b' positions down.

    Lets say

    position (ID,1) = price

    position (ID,2) = buildtime

    position (ID,3) = range

    position (ID,4) = color

    So storing the the range for soldier number 15 .... is easy as ...

    Array > Set at XY(15,3) to Range

    Retreiving the color for soldier number 12 ...

    color = Array.at(12,4)

    Hope this helps you.

  • After I arrive from work I will explain

  • Well, I use XYZ array because of this:

    (1,0,0) means - 1 for Gold, 0 for level, 0 for info for production.

    As example:

    tokenat(Array.At(1,GoldUp,0),0,",")

    tokenat(Array.At(1,GoldUp,0),1,",")

    Is for Goldmine, depending on level cost - Like: Build GOLDMINE lvl 1, for 150 gold, 150 lumber.

    And those zeros in (1,GoldUP,0) are for production per TickForProd - like 50 gold per 2s.

    That's why I use Array.

  • Tokenat is used as follow:

    tokenat(string, index, separator)

    Return the Nth token from the string by separator.

    For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

    So i suppose you store on the position (1,GoldUp,0) in the array a string like "50,gold,"

    Now tokenat(Array.At(1,GoldUp,0),0,",") will return the string "50"

    If it is not string or the string does not contain a "," then the tokenat is totaly useless. So i assume that is what you do.

    Tot make calculations with the string "50", we need to convert it to a number. Int() is the way to do that.

    So ... int("50") returns 50

    or in general it is int(tokenat(Array.At(1,GoldUp,0),0,","))

    But, you make it yourself very very very difficult.

    If GoldUP = goldproduction/tick then the array needs as many positions on the Y-axis as the maxium ever producable gold.

    Again (1,GoldUp,0) means no more then a position in the array. Position 1 on the x-axis, position GoldUp (wich can be zero but also 9999999999999999999)on the Y-axis, 0 on the Z-axis.

    On that postion you can store a value. But those values you will never ever find back. Because GoldUp keeps gooing and gooing. All youre values are from one second to the other second on another place in the array.

    It is ofcourse up to you to find a way to make that work. And maybe one day, when you have this working you can explain this to me again. (if i am still here)

    In the mean time, i suggest to look at my suggestion again. Or take some Array tutorials if you dont beleive me, all fine for me.

    Example ... GOLDMINE lvl 1, for 150 gold, 150 lumber ... and lets do it as you wish with a 3D array

    Make a global variable with the name 'GoldMine' = 1

    Make a global variable with the name 'Gold' = 1

    Make a global variable with the name 'Lumber' = 2

    I assume you have a variable 'level'.

    Now store the values in the array.

    Array > Set at XYZ(Goldmine,level,Gold) to 150

    Array > Set at XYZ(Goldmine,level,Lumber) to 150

    Retreive the values from the array.

    BuildCostInGold (for a Goldmine) = Array.at(Goldmine,level,Gold)

    BuildCostInLumber (for a Goldmine) = Array.at(Goldmine,level,Lumber)

    (and ofcourse, if GoldUP > BuildCostInGold, build the thing, substract BuildCostInGold from GoldUP)

    Okay now, lets do another building.

    Make a global variable with the name ''RandDunit" = 2

    Now store the values in the array.

    Array > Set at XYZ(RandDunit,level,Gold) to 400

    Array > Set at XYZ(RandDunit,level,Lumber) to 300

    And so on ..............

    As you see, the value of 'level' > you change. Meaning the values for different levels are stored on a different place in the array. And they can always be found back.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm using those positions :

    1-Goldmine

    2-Lumbermill

    3-Laboratory

    11-Soldier1

    12-Soldier2

    13-Soldier3

    21-GovBuilding1

    22-Gov2

    23-Gov3

    31-Research1

    32-Res2

    33-Res3

    Etc...

    Those array positions are good for me and I know where is what. I just had question about adding resources per TickForProd (2 seconds) and if is possible to create subtract function what will subtract from current production and from stored resources.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)