Author Topic: Problem trying to parse [Favourite] Magic Token  (Read 4232 times)

enterthepuffin

  • Newbie
  • *
  • Posts: 5
    • View Profile
Problem trying to parse [Favourite] Magic Token
« on: October 22, 2016, 12:58:46 AM »
I'm trying to display an image (a small star) when a selected game is in the favorites list.

I can access and display the [Favourite] magic token, which appears to be "" or "1" depending on whether the game is in the list. Also, though it behaves like a string, I can't seem to call the .tointeger() method so that I can test it as an integer. What is the proper way to test strings in squirrel? If someone could tell me where the flaw is in either of the following bits of code, I'd be very thankful:

//Try to test as string to display image
local Fav = "[Favourite]";
if ( Fav == "1" ) fe.add_image( "star.png", 220, 160, 16, 16 );

//Try to test as integer to display image
local Fav = "[Favourite]";
local FavValue = Fav.tointeger();
if ( FavValue == 1 ) fe.add_image( "star.png", 220, 160, 16, 16 );

Am I making a bonehead syntax error? Is [Favorite] not of type string? Is there a debugger that can tell me what errors I've thrown in code?


keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Problem trying to parse [Favourite] Magic Token
« Reply #1 on: October 25, 2016, 05:01:02 PM »
Image names, as well as the messages displayed by Text and Listbox objects, can all contain one or more "Magic Tokens".

Magic tokens can also be used to run a function defined in your layout or plugin's squirrel script to obtain the desired text. These tokens are in the form [!<function_name>].

Use a magic function and fe.game_info().

enterthepuffin

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem trying to parse [Favourite] Magic Token
« Reply #2 on: November 03, 2016, 11:50:41 PM »
Thanks. That works, but it doesn't update when I select a new game. It just displays the image based on the favourite flag of the game that is selected when attract mode launches. How do I get it to update dynamically as I select a new game?

Here's my attempt to implement your suggestion.

function FavStar() {
local Fav = fe.game_info( Info.Favourite );
if ( Fav == "1" )
   fe.add_image( "star.png", 220, 160, 16, 16 );
}

[!FavStar()];


keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Problem trying to parse [Favourite] Magic Token
« Reply #3 on: November 04, 2016, 05:17:04 PM »
This is how i do it. Using your example as a base.

Code: [Select]
function FavStar() {
if (fe.game_info( Info.Favourite ) == 1) return "star.png";
}
local Fav = fe.add_image("[!FavStar]", x, y, w, h);

enterthepuffin

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem trying to parse [Favourite] Magic Token
« Reply #4 on: November 04, 2016, 09:15:55 PM »
Thanks so much for the help! That worked... almost. fe.game_info returns a string type, so to make it work I needed to modify your code to be:

Code: [Select]
function FavStar() {
if (fe.game_info( Info.Favourite ) == "1") return "star.png";
}
local Fav = fe.add_image("[!FavStar]", x, y, w, h);

Is there any documentation on magic functions? Do they need to be used as a parameters inside of one of fe's add_ functions in order to update dynamically?

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Problem trying to parse [Favourite] Magic Token
« Reply #5 on: November 05, 2016, 03:06:01 AM »
^^^^ i believe so. I must have forgot the quotes. I made that example from memory on my cell phone while watching Eraser on vhs with a kid on my back and a baby standing between my legs. Lol