Author Topic: [Solved] Parsing Magic Tokens as Integers  (Read 3995 times)

CharlieMcF

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
[Solved] Parsing Magic Tokens as Integers
« on: February 20, 2017, 03:16:08 PM »
I'm having some difficulty extracting an integer value from a magic token, hope some kind soul can give me a hand.

I'm designing a layout which requires that elements are randomly displayed on the screen when a game is highlighted. I can write a function to determine if a particular element should be displayed or not:

Code: [Select]
function Getrand()
    {
        local r = rand() + 1;
   r = r % 2;
        return r;
    }

So this will return 0 or 1, then I'll use this value to hide (if Getrand returns 0) or display (if Getrand returns 1) the element.

Now I need this to update with the navigation, happily magic tokens do exactly that, so I wrote this:

Code: [Select]
local randVal = "[!Getrand]";

And if I display this value on the screen with:

Code: [Select]
local showText = fe.add_text(randVal, 900, 870, 0, 0); 

It seems to do exactly what I want, the value randomly changing between 0 and 1 as I scroll through the games. So far so good.

Clearly I now need to parse the value as an integer in order to evaluate it, and this is where my meagre coding skills desert me. I innocently assumed that the magic tokens would return as strings, but tointeger() fails every time so something's not right here. All advice (including any entirely different solutions, I'm not proud) would be very welcome, thanks.


« Last Edit: February 22, 2017, 03:29:25 PM by CharlieMcF »

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #1 on: February 21, 2017, 07:35:13 AM »
Return an empty string if you don't want the item shown.

CharlieMcF

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #2 on: February 21, 2017, 04:35:50 PM »
Thanks for this suggestion - I guess my problem is that magic tokens don't seem to return as strings (or tointerger() would work, right?). Any guidance here would be very welcome, thanks.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #3 on: February 21, 2017, 05:16:16 PM »
Thanks for this suggestion - I guess my problem is that magic tokens don't seem to return as strings (or tointerger() would work, right?). Any guidance here would be very welcome, thanks.

In hospital. I fully understand what you want to do now. I'll write you a function when I get home tonight.

CharlieMcF

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #4 on: February 21, 2017, 06:59:45 PM »
Wow. That's a very generous offer, thanks. If it turns out to be a major hassle and you can give me a couple of pointers I'd be happy to take a crack at it.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #5 on: February 21, 2017, 07:01:35 PM »
Untested, but a good start. Try this:
Code: [Select]
function iRand(max) {
    // Generate a pseudo-random integer between 0 and max
    local roll = (1.0 * math.rand() / RAND_MAX) * (max + 1);
    return roll.tointeger();
}

function bRand() {
  if (irand(1) == 1) return true;
  else return false;
}

function showStopper() {
  bRand() ? return „Hip Hop Horay!“ : return „“;
}
local showText = fe.add_text(„[!showStopper]", 900, 870, 0, 0); 

CharlieMcF

  • Jr. Member
  • **
  • Posts: 10
    • View Profile
Re: Parsing Magic Tokens as Integers
« Reply #6 on: February 22, 2017, 01:59:43 PM »
Thanks so much - very neat solution! I just had to do a bit of tweaking and then adapt the showStopper function:

Code: [Select]
function showStopper() {
  if (bRand()){
Showelement();
return "On";
}
else {
Hideelement();
return "Off";
}
}

The Showelement and Hideelement functions do the obvious thing of showing and hiding the element in question.

Thanks for the prompt and helpful advice, especially since you have clearly got a lot going on in your life right now.