Include numbers in gb.popup

Understanding the language, error messages, etc.

Include numbers in gb.popup

Postby STUDIOCRAFTapps » Mon Oct 10, 2016 6:03 pm

Hey!
I'm trying to include numbers in popup but i get a lot of error...

Code: Select all
      int Level = 3;
     
      if(gb.buttons.pressed(BTN_A))
        gb.popup(F("You beat the level " + Level), 20);


I don't know how F() works.
I alway get this error :
Code: Select all
 in expansion of macro 'F'


...
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Include numbers in gb.popup

Postby Duhjoker » Tue Oct 11, 2016 2:08 am

I can't "I" am going to say this,lol. But you should really check out the reference for the functions found here....

http://gamebuino.com/wiki/index.php?title=Reference

where you are trying to add an argument for the level is actually for duration of the pop up. Instead it would be easier to write a popup for each level and then have it called once per level.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Include numbers in gb.popup

Postby STUDIOCRAFTapps » Tue Oct 11, 2016 11:50 am

DON'T LAUGH ABOUT MY ORTHOGRAPH... edit: And grammar
Last edited by STUDIOCRAFTapps on Tue Oct 11, 2016 11:54 am, edited 1 time in total.
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Include numbers in gb.popup

Postby STUDIOCRAFTapps » Tue Oct 11, 2016 11:54 am

Duhjoker wrote:I can't "I" am going to say this,lol. But you should really check out the reference for the functions found here....

http://gamebuino.com/wiki/index.php?title=Reference

where you are trying to add an argument for the level is actually for a duration of the pop-up. Instead, it would be easier to write a popup for each level and then have it called once per level.


Write a popup for each level? If we replace level by score, I need to add a lot of pop-ups.
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Include numbers in gb.popup

Postby rodot » Tue Oct 11, 2016 12:19 pm

Popups only accept stings stored in PROGMEM so it doesn't eat up all the RAM. When you use F("your string"), it calls a macro that will make sure the string is stored in flash memory (aka PROGMEM) and *not* transferred to RAM when the function is called.

Pros : it saves RAM
Cons : content must be set at compilation time, it can't be dynamically changed at running time. So you can't use if for level or score, except if you create a separate string for each level as Duhjocker suggested.

Alternative solution : make your own popup function using the Arduino String class. Takes more RAM, but it would be more versatile.

Here is a short example I just made you :

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

String myPopupText;
byte myPopupTimeLeft;

void setup(){
  gb.begin();
  gb.titleScreen(F("Custom Popup"));
}

void loop(){
    if(gb.update()){ //update everything
      gb.display.println(F("Custom popups"));
      gb.display.println(gb.frameCount);

      if(gb.buttons.pressed(BTN_A)){
        myPopup(String("Frames = ") + gb.frameCount, 40);
      }

      myPopupUpdate();

      if(gb.buttons.pressed(BTN_C)){
        gb.titleScreen(F("Custom Popup"));
      }
    }
}

void myPopup(String text, byte time){
  myPopupText = text;
  myPopupTimeLeft = time;
}

void myPopupUpdate(){
  if (myPopupTimeLeft){
    gb.display.fontSize = 1;
    gb.display.setColor(WHITE);
    gb.display.fillRoundRect(0,LCDHEIGHT-gb.display.fontHeight-3,84,gb.display.fontHeight+3,3);
    gb.display.setColor(BLACK);
    gb.display.drawRoundRect(0,LCDHEIGHT-gb.display.fontHeight-3,84,gb.display.fontHeight+3,3);
    gb.display.cursorX = 4;
    gb.display.cursorY = LCDHEIGHT-gb.display.fontHeight-1;
    gb.display.print(myPopupText);
    myPopupTimeLeft--;
  }
}
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Include numbers in gb.popup

Postby naed » Thu Oct 13, 2016 12:26 am

great post Rodot and something i really needed to look at to for my current Gamebuino project
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 22 guests

cron