0 Favourites

Instance Variable Help

  • Hello, everyone I just wanted to see I could get some help with a problem.

    I have enemies that are referencing an Instance Variable as to what their action should be. Example: Action: 1 = Punch, Action: 2 = Kick Etc.

    however, currently actions are chosen at random, what I would rather is have a list 1,2,3,4,5 this way I can restrict actions for varied enemies like 2,2,2,1 this enemy will mostly kick but sometimes punch.

    My problem is that when I create a variable to control what actions can be picked it must be a text format to allow for multiple numbers but I then can't set the numerical actions to pick as they clash.

    I hope this makes sense other wise

    I want Instance Variable Actions to be set to a number in Instance Variable Available_Actions, where Available_Actions = 1,2,3,4

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Action is a number.

    AvailableActions is text (e.g. "1,2,3,4").

    Set Action to

    int(tokenat(AvailableActions, floor(random(tokencount(AvailableActions, ","))), ","))

    example (r141)

    Explanation:

    int(tokenat(AvailableActions, floor(random(<font color="red">tokencount(AvailableActions, ",")</font>)), ","))

    tells you how many values there are in AvailableActions separated by a comma (in this case <font color="blue">4</font>)

    int(tokenat(AvailableActions, <font color="red">floor(random(<font color="blue">4</font>))</font>, ","))

    give you a random number from 0 to 3 (e.g. <font color="blue">0</font>)

    int(<font color="red">tokenat(AvailableActions, <font color="blue">0</font>, ",")</font>)

    gives you the value in AvailableActions at position 0 (i.e. <font color="blue">"1"</font>)

    <font color="red">int(<font color="blue">"1"</font>)</font>

    converts the text into a number, int("1") = 1

  • Hey, thank you so much this works perfectly, my enemies are now unique with their actions. Thank you for explaining it so well I wouldn't of figured that out on my own.

    If and when I release the game you'll be on the credits for this.

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