How do I set a condition based on a variable?

  • Hi guys,

    hope someone can help me with this.

    So basically I have a guy working a farm, producing 1 food ever 2 seconds.

    the way this is set up is basically this: if the farm is empty -> call a function to make minions go to . on collision with it, the minion sprite is destroyed, and a value of 1 is added to farm's instance variable. when the value of 3 is reached, the function changes the behavior of the minions, so they would not be interested in it any longer.

    when the first minion enters the farm, 1 is added to farm's instance variable and also a boolean "isInhabited" set to true.

    the problem is that I have tried to make it so when there is only 1 guy working to add 1 to the global variable "food" ever 2 seconds, when 2 guys are working, i wanted to add 5 to food every 2 seconds or when farm's boolean "isFull" is set to true (meaning 3 farmers), to add 12 to food every 3 seconds.

    don't think i'm skilled enough to think this one through. here's an image with the best idea i've had so far..

    ibb.co/kuoLhH

    hopefully someone can illuminate me!

  • What is wrong with doing :

    farmercount = 1, every 2 seconds, add 1 to food

    farmercount = 2, every 2 seconds, add 5 to food

    farmercount = 3, every 3 seconds, add 12 to food

    or maybe i missed something with the logic of the game.

  • Thanks for the quick answer, plinkie!

    I have not tried it yet like this because I want to later to have 2 farms, for example, and be able to count the farmers for that specific farm (or household eating said food).

    Or is it possible to tie this to a specific instance of a farm? like, for example: if sprFarm.IID then farmercount = 1, every 2 seconds, add 1 to food

    Then again,is it IID or UID I need to look at?

  • I have tried

    farmercount = 1, every 2 seconds, add 1 to food

    farmercount = 2, every 2 seconds, add 3 to food

    farmercount = 3, every 3 seconds, add 12 to food

    works fine up until the second event

    farmercount = 3, every 2 seconds, add 5 to food

    but ignores the following "farmercount = 3" event for some reason and only adds 3 food every 2 seconds even when the farm is full (3 farmers). I've tried with else in-between the statements, still not working...

    I don't know much about the "while" loop. any thoughts if that would do the trick? Maybe something like:

    while farm has less than 2 farmers

    farmercount = 1, every 2 seconds, add 1 to food

    farmercount = 2, every 2 seconds, add 5 to food

    while farm has 3 farmers

    farmercount = 3, every 3 seconds, add 12 to food

    I am not sure how to implement this yet, however. would I have to use an "else" between the "while" events? Do I have to check boolean of farm "isFull" instead of checking farmercount? I will try some ideas now, but if any of you guys have an answer, please let me know

    thanks a lot!

  • Here's another image with the whole function for the "farm"

    ibb.co/fBJE9x

    The function is called when the building is placed on the map. for testing purposes, a building is already placed on the map.

    Essentially I want to have a farm that adds produces food based on how many farmers are working it. In the future I want to add animations for each of the farmers working on the farm, (for each minion destroyed, I can spawn another with the animation set to "farming" at the location of the farm, or can even change the animation of the farm sprite based on this number) but for now I want to make sure the farm is working as intended.

    Any help with this is really appreciated.

    If you have any questions, please let me know! Cheers

  • Since you are using specific counts that are true or false you don't need to use else or while. You were using else wrong in that example as well but you don't really need it. You do not need to make it so complicated in that image trying to embed a bunch of conditions that are not required. You've also embedded while farmercount = 2 when farmercount = 1 which can never be true. You just need :

    farmercount = 1, every 2 seconds, add 1 to food

    farmercount = 2, every 2 seconds, add 3 to food

    farmercount = 3, every 3 seconds, add 12 to food

    As separate events. No weird embedding of whiles and elses please. Try that and see what happens.

  • Hey plinkie,

    I have tried this and still is not working as intended.

    everything is working fine until reaching 3 farmers, where it just ignores the event and even if the farmcount is 3 it will still only collect 3 food every 2 seconds...

    image is here: ibb.co/iYKhUx

    I will also load the .capx if it helps... maybe the issue could be somewhere else?

  • Yes the issue will be somewhere else. Your farmercount will still be 2 because of broken logic elsewhere, you can check what the value of the global var is in debug mode to see if it does actually go to 3. Move this into a private message though as this will just become a spam thread.

  • Thanks for this, plinkie. I'll check the whole code again and repost this in the forums.

    Cheers!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I found the issue after checking with the debug... I had the farm only accept 2 farmers instead of 3... that was ONE STUPID MISTAKE that cost me 1 day!

    live and learn... thanks for the input again!

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