Creating specific object from an array

0 favourites
  • 15 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hi everyone,

    I was wondering if there is a way to create an object selected from an array and then delete it from it when it's created to avoid some duplication.

    In other words I have this table loaded on an array:

    That's planets I want to create in my game. the sprites object are created and they all are in a family named "SpaceMap" with 2 variables: Name (corresponding to the 1 column) and the Set (corresponding to the 2nd column)

    To create my planets sprite on the layer, I use a function:

    In the end, when the object is created, I delete it from the array.

    Seems I'm missing something, but don't get what for now.

    Thanks if you can provide some help!!

    Depending of the type of Set (A,B,C,D…) I want to create a specific planet. So I used a parameter to my function (SetType)

    Seems I'm missing something because it doesn't work.

    If anyone can provide some help, it will be great! Thanks!

    Tagged:

  • When you create the family object "SpaceMap" it doesn't automatically know which object, you need to create the specific object by name. You store the name of the object in the array but then try and create the family instead. You can create array.at(loopindex,0) or however the values are stored in the array by row/column.

    Pick last created Family can still be used but may not work in the same tick and loop. It may need to be triggered elsewhere but you can test this.

    The array deletion row you don't need to cycle through the array again as you are already at a row in the array and can delete the current row 'loopindex'. The problem with this however is that rows will move up and some objects will be skipped, to resolve this sometimes you can run reverse through the array from bottom to top.

    I saw you create a probability table but don't use it?

  • Ok I understand my mistake about the object by name. So I changed a bit my function with your suggestion:

    But I still don't have the objects created.

    About my probability table, I don't use it for now but my first idea was to add all the entry for the "B" set for example, and then make a shuffle to randomise the selection. I planned to pick the last item of this table to create my object like,

    -> System: Set PlanetSpriteSelected to AdvancedRandom.Weighted

    In the end I know how to create a sprite from an entire array and delete it from the table once it's created but that mean I have to create an array for each set. I'm looking for a solution where I can have all objects with different set in the same array and then select the objects with a specific set called.

  • Oh, and I changed of course my array to fit with the object's name:

  • That logic isn't going to work because in one instance you use 0,loopindex then you use loopindex,0. I would start small and try and pick out the array elements first so you know it is logging the right things. An example of the array in debug view would help, not the array file. The rows/columns matter here, I think you've imported it in an opposite way to what I usually do where the objects are rows (X) and the stats are columns (Y), and you have it where the objects are columns (Y), you just need to make sure the array.at actions are in the correct format.

  • I see, so even when I try little I can see there is a problem with the advancerandom table. If I ask it to add the entry from the array, it doesn't work

    And still my Array is loaded correctly. Non sense…

    It understand that there is 25 entry but won't display the datas from it

  • For that particular issue also check that the array is loaded before you run the functions, it can all happen fast and we can't see the event where you load the file into the array.

    edit : this is more likely the issue because I see you run the functions 'on start of layout' and the table is only showing 0,0. Try moving the functions to a mouse click that can occur after the array is filled.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok thanks, that was the issue.

    So now I'm getting close to what I want to do. I can save in my advancedrandom probability table the datas with the corresponding set.

    Now, is there a way to shuffle this table? so I can take a random entry (the last of the table for exemple) and create the object.

    I don't even know if this solution is a good way to do what I want in the end…

  • A probability table is automatically random so it will pick a random element from it when you use for example Create Object by Name : AdvancedRandom.Weighted. If you want to use this it depends on the design of the game and what you are trying to achieve with the spawning of these objects.

  • I get it, now my code looks like this and it's supposed to work correctly:

    But it doesn't. Seems that the problem come from this line

    -> System: Set PlanetSpriteSelected to AdvancedRandom.Weighted

    as it don't copy the data in the variable. I tested it also not as a local variable but as a global variable and the is "0" instead of the sprite's name as it is set on the table.

    I also try to test it when i click on the mouse button but the result is still the same.

    I really don't know where the problem is because I already used a similar code in another sheet and it works very well.

  • The weight in the table is meant to be a number but you're adding a character like B or C. The weight is there as a way to define how rare something is to be picked, if it's even for all planets then you can set it to 1. That might be the cause of the output 0.

  • Thanks! I didn't know that!

    So now it's working, I can finally create my requested planets sprites on the map. So here is my last move, if I want once the planet is created, delete it from my array:

    + Planets_Array: Value at (0, LoopIndex("Y")) = PlanetSpriteSelected

    -> Planets_Array: Delete index Planets_Array.CurY from Y axis

    This seems to works fine but it seems that if I repeat my function like this:

    + System: Repeat 3 times

    -> Functions: Call CreatePlanets (SetType: 3)

    it make some clones of my planets, even if they are deleted.

    Do you think it will works better if I repeat the creation of the planets directly inside my function? Like with a variable?

  • I think i just get what you mean:

    The array deletion row you don't need to cycle through the array again as you are already at a row in the array and can delete the current row 'loopindex'. The problem with this however is that rows will move up and some objects will be skipped, to resolve this sometimes you can run reverse through the array from bottom to top.

    Seems that it delete also some other datas…

    I'm not sure now that my code line delete the correct data row

  • Deleting from the array isn't going to do anything now as you are picking from the probability table. You can remove the entry from the probability table but again start simple with a trigger once to make sure it is working then add the repeating functionality afterwards.

    Also you need to repeat from within the function eventually not outside it as you are creating the probability table and adding to it inside the function, or else each repeat will start a new table which means you can end up with repeated selections.

    Logic could be something like : -

    build probability table

    repeat 3 times :

    set variable to arandom.weighted

    create object 'variable'

    remove variable from prob table

  • Your logic is correct! I finally make it works perfectly:

    Once again thank you lionz for your great advices and help!

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