1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

[Solved] AngelScript: A problem with an array

Discussion in 'Modding Help' started by Vermilicious, Feb 5, 2016.

  1. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Hello guys, I'm having a bit of a headache with an array. It's more an AngelScript problem than a KAG one, I think.

    First, I have defined this small class in one file:
    Code:
    namespace UndeadInvasion {
    
      class EntitySpawn {
    
        string mEntityName;
        u8 mSpawnChance;
     
        EntitySpawn() {
     
          mEntityName = "Zombie";
          mSpawnChance = 100;
       
        }
     
        EntitySpawn(string entityName, u8 spawnChance) {
     
          mEntityName = entityName;
          mSpawnChance = spawnChance;
       
        }
     
      }
    
    }
    Second, I have a file with some variables:
    Code:
    #include "EntitySpawn.as";
    
    
    namespace UndeadInvasionVariables {
    
      //Define a minimum number of -1 (disabled) for players on each team for the game to start
      const s8 PLAYER_COUNT_START_MINIMUM = -1;
    
      //More stuff here...
    
      //Define an array of Undead entities to spawn
      const UndeadInvasion::EntitySpawn[] UNDEAD_ENTITY_SPAWN_MIX = {
        UndeadInvasion::EntitySpawn("Zombie", 60),          //Zombie 60% chance
        UndeadInvasion::EntitySpawn("Skeleton", 35),        //Skeleton 35% chance
        UndeadInvasion::EntitySpawn("Devilish Zombie", 5)   //Devilish Zombie 5% chance
      };
    
      //print("Length:"+ UNDEAD_ENTITY_SPAWN_MIX.length); //<-- Causes error: "Expected Identifier"
    
    }
    And lastly, I have this in my RulesCore:

    Code:
    #include "UndeadInvasionVariables.as";
    #include "EntitySpawn.as";
    
    #include "RulesCore.as";
    
    shared class UndeadInvasionRulesCore : RulesCore {
    
    //More stuff here
    
    void spawnUndead(Vec2f position) {
    
        //Check if there are any undead available for spawning
        if(mUndeadAvailable > 0) {
     
          //Get a random value between 0 and 100
          u8 randomChance = XORRandom(100);
    
          //print("Length:"+ UndeadInvasionVariables::UNDEAD_ENTITY_SPAWN_MIX.length); //<-- Causes error: Likely missing ';'
    
        }
    
    }
    
    }
    I'm stumped. All my other const variables work just fine. I also tried declaring the array using the Array<UndeadInvasion::EntitySpawn> approach. No dice. Anyone got a clue about what's going on?

    Solution: The rules core and respawn system was declared using the shared keyword, and could thus not access the array. Removing that keyword fixed the problem.
     
    Last edited: Feb 5, 2016
  2. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    Isn't length() a method?
     
  3. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    For the Template type Array, it is, but for these native-style arrays it's not. At least it's used like this many places in vanilla code, and I've used it too in other situations without problems. But anyway, I still get an error.
     
  4. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    This happens because you can't call a function out like this. You have to call it from a function.

    About the likely missing ";", I don't really understand how could it be problematic. Have you tried using the size() method instead of length?
     
  5. Verrazano

    Verrazano Flat Chested Haggy Old Souless Witchy Witch Witch THD Team Global Moderator Forum Moderator Tester
    1. Practitioners of War Extreme Revolution - POWER

    Messages:
    477
    Both ways of declaring arrays in angelscript end up being the same type of object. Doing u8 arr[]; you can call arr.length()/arr.size() same with array<u8> arr;
    In the Objects.txt it shows all the available properties/functions for arrays.
    [​IMG]
     
    Asu likes this.
  6. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    The print line directly in the variables namespace, was just for testing. I did the same call in my rules core function.

    I'm actually pretty sure the array types are not compatible, because I think I tried several times to replace .length with .length() other places, and I got errors when I did that.
    But really, whatever I type, I get this error. Even doing this:
    Code:
    if( UndeadInvasionVariables::UNDEAD_ENTITY_SPAWN_MIX is null) {
            print("SPAWN MIX IS NULL");
          }
    If I comment out any reference to UndeadInvasionVariables::UNDEAD_ENTITY_SPAWN_MIX, there's no error, and the game runs fine.
     
  7. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Yeah, I realized that might be a cause, but if you had looked closer, I have attempted the same call elsewhere.
     
  8. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Thank you! Removing the shared keyword on the rules core and re-spawn system solved it. I guess I should've thought about that. Not sure why the base classes are declared as shared in the first place, I just assumed it was a good idea to keep the keyword. Tagging topic as solved. Peace.

    P.S. length() also worked. How odd! Thanks for the tip about Objects.txt. I didn't know about that one!
     
  9. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    They're declared as shared so that they're the same type from more than one script (eg interface scripts and rules script). Angelscript has a weird type system.

    Rules core itself is a mess. You'd be better off just writing directly into the rules hooks. I've got an example of this that was a starting point for @kaizokuroof's zombies mod. You could check out the resource there. No need for shared types for a static array of constants then. There are also a lot of examples of this in TR now as well.

    Thanks to fuzzle for being helpful. Less so if the original one was snarky enough to get deleted.

    As a side note length is a property, which is a special combination of method and variable. You can read more about them in the angelscript reference but that's why length and length() both work
     
  10. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    RulesCore might be a mess (you've probably been working much more with it than I), but it works for me. It was a little confusing at first though. I think I've found a set-up I'm fairly happy with considering what limitations there are, and things are starting to take shape. RulesCore and SpawnSystem hides some of the (more or less) gritty details, so I don't have to worry about it anymore, now that I have an idea how it is designed. I think the bigger issue is that it follows a pattern that isn't used elsewhere. There are too many patterns around, and now I'm introducing even more (but in my view better ones) :o)