User Tag List

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Thread: Expression Help - Set single digit in global value

  1. #1
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export Module
    CBSection31's Avatar
    Join Date
    Apr 2007
    Location
    Aurora, IL, USA
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Expression Help - Set single digit in global value

    Hi all,

    I'm not very mathematically inclined, but I *think* this is somehow possible...I just can't figure out how.

    I am using a single global value to store a group of numbers, where each number represents a separate variable in my game that can range from 0-9.

    So, for example, if I have the global value "286470", then each number represents a separate variable needs to be extracted and written to individually.

    To make this easier to understand, imagine a player has 6 inventory slots, and the value "286470" tells the engine that the player has item 2 in slot 1, item 8 in slot 2, item 6 in slot 3, and so on. This isn't what I'm doing in my application, but I think it illustrates what I've set up.

    I realize I can store each number as a separate global value. For the sake of simplicity, assume that, for my purposes, this isn't an option in my application.

    Extracting a single number from the value is easy. I can use Mid$ to retrieve the number that I want. So, if I want to extract the item number for inventory slot 3, then I can do Mid$(Str$(286470),3,1) and will get item "6".

    But writing to a number is something that I can't figure out. Say I want to change the item in inventory slot 5 from 7 to 3 without changing the other numbers. I need to somehow come up with an expression that changes number X to number Y, without affecting the other numbers in the value.

    Is this possible using the expression calculator? Hopefully, I'm explaining this well enough. If not, let me know and I can be more detailed.

    Thanks in advance for your time and help.

  2. #2
    No Products Registered

    Join Date
    Dec 2006
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    123456+400=123856. Like this?

  3. #3
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export Module
    CBSection31's Avatar
    Join Date
    Apr 2007
    Location
    Aurora, IL, USA
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    Thanks for the quick response. I forgot to mention in my initial post that it has to be dynamic, so adding a certain number wouldn't work. I edited my main post to more accurately reflect what I'm looking for.

  4. #4
    Clicker Fusion 2.5 DeveloperFusion 2.5+ DLCAndroid Export ModuleSWF Export ModuleUnicode Add-on
    Looki's Avatar
    Join Date
    Aug 2006
    Location
    Karlsruhe, Germany
    Posts
    3,741
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: Expression Help - Set single digit in global value

    Well, with some trickery it is...

    Say you have the number 123456
    To make the 3rd digit from the right the first digit, you divide by 100.
    The general formula for the nth digit from the right is 10^n, but the first digit is 0, not 1!
    To raise a number to a power in MMF, use [font:Courier New]a pow b[/font].

    [font:Courier New]123456/100.0 = 1234.56[/font], now we will remove all digits except the 4.

    You use modulo 10 to keep only the lowest digit (and the decimal ones).
    [font:Courier New]1234.56 mod 10 = 4.56[/font]

    To get rid of the decimal places, you do
    [font:Courier New]Int(4.56) = 4[/font]

    To conclude:
    [font:Courier New](N / Int(10 pow D)) mod 10[/font]
    gives us the digit D (starting at 0) of number N.

    Now, we multiply that value by the 10 potency again.
    [font:Courier New]((N / (10 pow D)) mod 10) * (10 pow D)[/font]

    Then we subtract it from the original number to set that digit to zero.
    [font:Courier New]N - (Int(N / (10 pow D)) mod 10) * (10 pow D)[/font]

    e.g. [font:Courier New]1234 - 200 = 1034[/font]

    The last part is very simple. Now we can easily replace that zero by a new digit.

    Take the computed result and add [font:Courier New]X * (10 pow D)[/font], X being a number from 0 to 9 representing the digit.

    The final formula:

    [font:Courier New]N - (Int(N / (10 pow D)) mod 10) * (10 pow D) + X * (10 pow D)[/font]

    Example:

    [font:Courier New]N = 123456, D = 2, X = 9[/font]
    The 3rd digit from the right of the number 123456 will be changed to 9.

    Calculation:
    Code:
    -> 123456 - (Int(123456 / (10 pow 2)) mod 10) * (10 pow 2) + 9 * (10 pow 2)
    -> 123456 - (Int(123456 / (100.0)) mod 10) * (10 pow 2) + 9 * (10 pow 2)
    -> 123456 - (Int(1234.56) mod 10) * (10 pow 2) + 9 * (10 pow 2)
    -> 123456 - (1234 mod 10) * (10 pow 2) + 9 * (10 pow 2)
    -> 123456 - 4 * 100.0 + 9 * 100.0
    -> 123056 + 9 * 100.0
    -> 123956
    The digit '4' changed to '9'.

    Hope this helped.

  5. #5
    No Products Registered

    Join Date
    Dec 2006
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    One thing you could try is to use multiple alterable strings along with an alterable value to set the final composited string into. For your example 123456, you would use 6 alterable strings, one for each digit, plus one alterable value.

    The expression of setting the strings into the alterable value would look like this:

    Val( Alterable String A( "Active" )+Alterable String B( "Active" )+Alterable String C( "Active" )+Alterable String D( "Active" )+Alterable String E( "Active" )+Alterable String F( "Active" ) )

    -edit, or just use Lookis formula which is better than using 6 strings.

  6. #6
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    Using a single alterable string would be... rather a lot easier (or even converting to a string and using Mid$ to change particular values before converting it back!) - now that Looki's come up with that formula I'd sort of like to see that used, though

  7. #7
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export Module
    CBSection31's Avatar
    Join Date
    Apr 2007
    Location
    Aurora, IL, USA
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    Thanks for the replies! Looki, that formula is brilliant. I will be trying it out as soon as I get home.

  8. #8
    Clicker Fusion 2.5 DeveloperFusion 2.5+ DLCAndroid Export ModuleSWF Export ModuleUnicode Add-on
    Looki's Avatar
    Join Date
    Aug 2006
    Location
    Karlsruhe, Germany
    Posts
    3,741
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: Expression Help - Set single digit in global value

    DavidN, heh. I thought of the Mid$ method, but then I realized that from a programmer's standpoint, that's a hideous solution. Not that mine is extremely efficient, but at least it doesn't convert numbers to strings! :P

  9. #9
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export Module
    CBSection31's Avatar
    Join Date
    Apr 2007
    Location
    Aurora, IL, USA
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    I'm curious now, even though it's not necessary for my application... How would you use Mid$, if the sequence were letters? I've only ever used Mid$ to retrieve values, not change them.

    So, if I had string "ABCDEF", you could somehow use Mid$ to change the "D" to, say, an "S"?

  10. #10
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Expression Help - Set single digit in global value

    You would have to piece the string together with the new letter in place of the old one, and the existing ones around it, and this is beginning to sound a bit worse than I thought already - something like:

    Left$("ABCDEF", 3) + "S" + Right$("ABCDEF", 2)

    More generally, where n is the position to change:

    Left$("ABCDEF", n-1) + [the new letter] + Right$("ABCDEF", Len("ABCDEF")-n)

    Or... something.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Removing last digit
    By King_Cool in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 19th November 2012, 10:58 PM
  2. Single Digit to Double Digit
    By Mantis in forum Multimedia Fusion 2 - Technical Support
    Replies: 9
    Last Post: 9th December 2010, 12:10 AM
  3. Setting global string via an expression
    By MJK in forum SWF/Flash Export Module Version 2.0
    Replies: 5
    Last Post: 14th June 2010, 11:30 AM
  4. digit problem
    By Herbert in forum File Archive
    Replies: 1
    Last Post: 13th February 2010, 10:04 AM
  5. Get a digit from a counter
    By Raylax in forum Multimedia Fusion 2 - Technical Support
    Replies: 11
    Last Post: 13th December 2008, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •