Code about add/remove item in period of time?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
bimb2212
Newbie
Posts: 19
Joined: Wed Jul 30, 2014 10:15 am
Contact:

Code about add/remove item in period of time?

#1 Post by bimb2212 » Sat Nov 01, 2014 11:43 am

Like Fading Heart game, when you buy a book, it will be delivered in the next 3 days. I don't know how to make the code like that if we buy the book regardless of the date and time of calendar.

User avatar
Marionette
Regular
Posts: 128
Joined: Thu Apr 21, 2011 12:04 pm
Completed: https://marionette.itch.io/
Projects: Get Meowt of Here
Deviantart: rexx9224
itch: marionette
Location: Ireland
Discord: Marionette#2995
Contact:

Re: Code about add/remove item in period of time?

#2 Post by Marionette » Sun Nov 02, 2014 5:59 pm

I don't know much about your calender code, but you could just have an item (like a reciept) that acts like a counter, with its value at the number of days till delivery, and each day that passes it either decreases the value by one or if the counter hits 0 replace the item with your book.

User avatar
bimb2212
Newbie
Posts: 19
Joined: Wed Jul 30, 2014 10:15 am
Contact:

Re: Code about add/remove item in period of time?

#3 Post by bimb2212 » Mon Nov 03, 2014 5:25 am

Marionette wrote:I don't know much about your calender code, but you could just have an item (like a reciept) that acts like a counter, with its value at the number of days till delivery, and each day that passes it either decreases the value by one or if the counter hits 0 replace the item with your book.
I use digital calendar from http://www.renpy.org/wiki/renpy/doc/coo ... l_Calendar

As you say, we need to define it in new class or in that digital calendar?

This is my amateur code so far, but i still got a problem about it:

Code: Select all

init:
    $ book_buy = 0
    $ book_send1 = 0     ### book_send1 : meaning buy it on theweekday 1(Monday)
    $ book_send2 = 0
    $ book_send3 = 0
    $ book_send4 = 0
    $ book_send5 = 0
    $ book_send6 = 0
    $ book_send7 = 0
    $ book_add = 0
    #### All of them are in Restrictrange at min 0 and max 1

label Shop:
    if book_buy >=1 and book_send2 !=1 and book_send3 !=1 and book_send4 !=1 and book_send5 !=1 and book_send6 !=1 and book_send7 !=1:
        if theweekday == 1:               
            $book_send1 +=1
        if theweekday >=3 and book_send1 ==1:         ### it takes 2 days to send to my home. So i use theweekday 1-3 (Monday- Wednesday), 2-4(Tuesday-Thursday), 3-5....
            $book_add +=1
    if book_buy >=1 and book_send1 !=1 and book_send3 !=1 and book_send4 !=1 and book_send5 !=1 and book_send6 !=1 and book_send7 !=1:
        if theweekday == 2:
            $book_send2 +=1
        if theweekday >=4 and book_send2 ==1:
            $book_add +=1
    if book_buy >=1 and book_send2 !=1 and book_send1 !=1 and book_send4 !=1 and book_send5 !=1 and book_send6 !=1 and book_send7 !=1:
        if theweekday == 3:
            $book_send3 +=1
        if theweekday >=5 and book_send3 ==1:                            
            $book_add +=1
    if book_buy >=1 and book_send2 !=1 and book_send3 !=1 and book_send1 !=1 and book_send5 !=1 and book_send6 !=1 and book_send7 !=1:
        if theweekday == 4:
            $book_send4 +=1
        if theweekday >=6 and book_send4 ==1:
            $book_add +=1
    if book_buy >=1 and book_send2 !=1 and book_send3 !=1 and book_send4 !=1 and book_send1 !=1 and book_send6 !=1 and book_send7 !=1:
        if theweekday == 5:
            $book_send5 +=1
        if theweekday >=7 and book_send5 >=1:
            $book_add +=1
    if book_buy >=1 and book_send2 !=1 and book_send3 !=1 and book_send4 !=1 and book_send5 !=1 and book_send1 !=1 and book_send7 !=1:
        if theweekday == 6:
            $book_send6 +=1
        if theweekday >=1 and theweekday <=5 and book_send6 ==1:
            $book_add +=1
    if book_buy >=1 and book_send2 !=1 and book_send3 !=1 and book_send4 !=1 and book_send5 !=1 and book_send6 !=1 and book_send1 !=1:
        if theweekday == 7:
            $book_send7 +=1
        if theweekday >=2 and theweekday <=6 and book_send7 ==1:
            $book_add +=1
    return

label Start:
    ........
    menu:
        "Buy a Book":
            $book_buy +=1
        "Leave":
            return
    return
    .......
    call Shop

screen inventory:
    if book_add == 1:
         textbutton "book" action [SetVariable("Knowledge", + 1), SetVariable("book_buy", 0)]
We can define them as True, False instead of 0, 1.
First problem: it was long code, and I use near hundred of items, that will make thousand of lines code.
Second problem: I need to call label Shop everyday to check because don't know when player buy it (and my game is about 365 days, it means, I must write 365 time "call shop" at the end or begin of the new day....).

User avatar
Marionette
Regular
Posts: 128
Joined: Thu Apr 21, 2011 12:04 pm
Completed: https://marionette.itch.io/
Projects: Get Meowt of Here
Deviantart: rexx9224
itch: marionette
Location: Ireland
Discord: Marionette#2995
Contact:

Re: Code about add/remove item in period of time?

#4 Post by Marionette » Mon Nov 03, 2014 8:39 am

Firstly if you're gonna have more than just a few books, and you need to keep track of them, you'd be better with an array than trying to maintain a list of variables like that, it'll get unwieldy very quickly.

If the books arent important, apart from just being a generic book. Then you can simply have a list with the max number of books the person can buy at once and each time they buy one you change the value of one of the fields from being 0 to being the number of days left.

So you end up with something like

Code: Select all

#assuming can buy up to 7 books
init:
  $book_array[0,0,0,0,0,0,0]
  $booksOwned = 0;

label buy_book:
  $buyBook = 1
  $daysToDeliver = 3
  $i = 0
  for each ibook in book_array:
    if (ibook == 0 and buyBook == 1):
      $book_array[i] = daysToDeliver
  
    $i = i+1
    
  if buyBook == 1:
    "failed to buy book"
  return
  
label updateBooks:
  $i = 0
  for each ibook in book_array:
    if (ibook != 0):
      $book_array[i] = book_array[i] - 1
      if (book_array[i] == 0):
          $booksOwned = booksOwned+1;
    $i = i+1
  return

label DayStart:
    ........
    menu:
        "Buy a Book":
            call buy_book
        "Leave":
            return
    return
    .......
    call updateBooks
    jump DayStart

Then if we assume the specific books are important give each a separate index and each day iterate through the list and update them.
In this case since you need to keep track of which ones are owned or not owned you basically have another value that signifies that the book is owned. Using either 1 or -1 would be easiest, so you can say >0 or <0 means its already owned.

Code: Select all

#assuming can buy up to 7 books
init:
  $book_array_names[book1,book2,book3,book4,book5,book6,book7]
  $book_array[0,0,0,0,0,0,0]

And then instead of setting the value to 3 set it to -3 and work towards 1, where a value of 1 means its owned.

Code: Select all

label buy_book:
  $buyBook = 1
  $daysToDeliver = 3
  $i = 0
  for each ibook in book_array:
    if (ibook > 0):
      "You already own this one."
    if (ibook < 0):
      "This one is being delivered."
    if (ibook == 0 and buyBook == 1):
      $book_array[i] = -daysToDeliver
      $buyBook = 0
  
    $i = i+1
    
  if buyBook == 1:
    "failed to buy book"
  return
  
label updateBooks:
  $i = 0
  for each ibook in book_array:
    if (ibook < 0):
      $book_array[i] = book_array[i] + 1
      if (book_array[i] == 0):
          $book_array[i] = 1
          "The following book:[book_array_names[i]!t] has arrived."
    $i = i+1
  return

...  
[insert extra code for picking specific books to buy/read]
...
I dont have the complier to test the code atm, and im not sure the syntax is all correct, but hopefully it'll at least explain the method im trying to suggest enough that you could implement it yourself if you want to. lol

User avatar
bimb2212
Newbie
Posts: 19
Joined: Wed Jul 30, 2014 10:15 am
Contact:

Re: Code about add/remove item in period of time?

#5 Post by bimb2212 » Mon Nov 03, 2014 1:27 pm

Marionette wrote:

Code: Select all

#assuming can buy up to 7 books
init:
  $book_array[0,0,0,0,0,0,0]
  $booksOwned = 0;

label buy_book:
  $buyBook = 1
  $daysToDeliver = 3
  $i = 0
  for each ibook in book_array:
    if (ibook == 0 and buyBook == 1):
      $book_array[i] = daysToDeliver
  
    $i = i+1
    
  if buyBook == 1:
    "failed to buy book"
  return
  
label updateBooks:
  $i = 0
  for each ibook in book_array:
    if (ibook != 0):
      $book_array[i] = book_array[i] - 1
      if (book_array[i] == 0):
          $booksOwned = booksOwned+1;
    $i = i+1
  return

label DayStart:
    ........
    menu:
        "Buy a Book":
            call buy_book
        "Leave":
            return
    return
    .......
    call updateBooks
    jump DayStart
As the code that means we need to "call updateBooks" every new day right? If not jump back to DayStart, and jump to another new label ("label Day_Two" for example), so I need to use "call updateBooks" again?
And I still don't know how to setup "$daytoDeliver = 3" to work with my calendar. "$minutes += 1440" = 1 days

User avatar
Marionette
Regular
Posts: 128
Joined: Thu Apr 21, 2011 12:04 pm
Completed: https://marionette.itch.io/
Projects: Get Meowt of Here
Deviantart: rexx9224
itch: marionette
Location: Ireland
Discord: Marionette#2995
Contact:

Re: Code about add/remove item in period of time?

#6 Post by Marionette » Mon Nov 03, 2014 7:51 pm

bimb2212 wrote:
As the code that means we need to "call updateBooks" every new day right? If not jump back to DayStart, and jump to another new label ("label Day_Two" for example), so I need to use "call updateBooks" again?
And I still don't know how to setup "$daytoDeliver = 3" to work with my calendar. "$minutes += 1440" = 1 days
Yeah call it every day, and if you do it doesn't need to link to your calender, as long as it gets called once a day then you can leave the 3 as a three and it should work, since each time its called, ie once a day, the time will be reduced by 1.

User avatar
bimb2212
Newbie
Posts: 19
Joined: Wed Jul 30, 2014 10:15 am
Contact:

Re: Code about add/remove item in period of time?

#7 Post by bimb2212 » Tue Nov 04, 2014 9:13 am

Marionette wrote:
bimb2212 wrote:
As the code that means we need to "call updateBooks" every new day right? If not jump back to DayStart, and jump to another new label ("label Day_Two" for example), so I need to use "call updateBooks" again?
And I still don't know how to setup "$daytoDeliver = 3" to work with my calendar. "$minutes += 1440" = 1 days
Yeah call it every day, and if you do it doesn't need to link to your calender, as long as it gets called once a day then you can leave the 3 as a three and it should work, since each time its called, ie once a day, the time will be reduced by 1.
Okay, I understand now, thanks for your help, I will try to improve and enhance these code.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]