String/Int Comparisons

0 favourites
  • 6 posts
From the Asset Store
A collection of a mix of percussion and orchestral string loops, including 20 musical sketches.
  • Has anyone been having trouble with int() and str() comparisons? Lately, if I tried testing to see if a string var that is preset to -1, do <this> function. But it would never work, even though it CLEARLY said -1. I had to put str() around it, even though the variable itself was a string and not a number.

    I've also had to do this with strings that I use tokenat for (ie. I set a dict key to 4-5-Tree (4-5 being tile positions) for a tree tile location, but if I were to check if "4-5-Tree" = that exact key, it doesn't work (even though if I add both to a text, it is the EXACT same). I end up have to create 2 local variables that are integers, and check x & "-" & y "-" & "Tree".

    This probably doesn't make too much sense, and maybe it has always been this way, but I feel like I never noticed it as much as I do now.

  • -1(int) is not equal to -1(string). They are different types of data. You have to convert them to the same type before comparing.

  • - correct, but if I'm setting a dictionary key to "4-5-Tree", then what is each part? A string or an integer?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Dictionary keys are strings.

    If dictionary key = str(tileX)&"-"&str(tileY)&"-"&"Tree" do whatever should work.

    If you were storing the tileXY in variables then make sure they are strings and you do - set variable mytileX to str(tileX). Although i think construct should convert a int to string in a string variable automatically.

  • The numbers can be interpreted as int, float or string. It dependes of what you want. I assume you want to get the coordinates from the key and use it to set the position of the object, so you have to break it down using tokenat expression, but if you want to use the coordinates to set a position then you have to convert it to int or float.

    example: var = 5-6-Tree

    Set X to float(tokenat(var, 0, "-"))

    Set Y to float(tokenat(var, 1, "-"))

    Set objName to tokenat(var, 2, "-")

    objName being an instance variable of the object

    **

    var = 5-6-Tree (string type variable)

    var = Tree (string type variable)

    var = 5 (int or float type variable)

    var = 5.5 (float type variable)

  • - right, that's why I thought. That's why I assumed if I put everything in both comparisons to a str() format, it would work, but it didn't. It wasn't until I set the variables it was checking (which were ints) to a string local variable and THEN checked against them.

    - correct - that's how I've done it in the past. Basically I was doing:

    if ("5-6-Tree") = str(x) & "-" & str(y) & "-" & "Tree"

    CALL FUNCTION

    This or doing str(x & "-" & y & "-Tree") didn't ever call the function.

    It could be that maybe it was a mistake on my end, but I've noticed more and more things not working when I feel like in the past they would have without me doing that. All in all, it's probably good practice to do it that way, regardless.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)