[Suggestion]Create Object from String or Variable

0 favourites
  • 8 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Evening, all! I do believe this is my first forum post. Been at C2 for a little while now, and I've been able to accomplish a lot. That said, I'm bringing up a suggestion that I've seen visited a few time in the past (for instance,

    --Pardon the lack of a link, still new in the forums so the rep is low).

    I'd like to have the ability to dynamically create an object, such as a sprite (though I bet other items such as arrays would benefit from this as well) by checking a variable as opposed to clicking to select it each time. It would serve many purposes that aren't possible without the functionality.

    For example: let's imagine a game like Castlevania, SotN. Items may be equipped in either right or left hand, and activated by that button. There are many, many items, and each one holds a fair amount of animation --especially if (unlike SotN swords) you plan to have the equipped item always visible, even when not in use. This means additional animations for walking, jumping, crouching, etc.

    The most prescribed method is creating a single sprite object, and creating many animation cycles/frames. This has a number of drawbacks. For one (and this may only apply games containing many items with high res sprites) it goes against best practices as I've come to understand them from a lot of time in tuts. Large spritesheets can cause the loading bar at launch to hang for a bit, then suddenly jump. Occasionally, this can cause a player to think their game has locked up before it's loaded. It may seem Trivial, but it has been suggested that while using spritesheets to cover multiple objects is helpful, it can be overdone.

    Secondly, it can quickly become difficult to organize and keep track of player equipment, and lacks automation. Instead of storing your object variables neatly inside their instances, you will need to create separate arrays. Now, while I do love arrays, I think many people will agree that after a certain size they can be difficult to keep track of without separate references outside of the program, like a list or spreadsheet to track object IDs (unless you're good at remembering that "Epic Chicken Slicer Sword of Justice" has an object ID of 73.

    Then there's the fact that the sprite can quickly become cluttered with animations. You're loading all of them each time, despite not needing most of them, and tracking them is even more difficult.

    Heaven forbid you accidentally apply an image point change to all animations while tired. That can lead to a lot of repair work if not caught immediately.

    Consider this:

    On equipping an object, a variable is set. We'll say it's the right hand, and call our variable EquipRightHand. Perhaps it's global, or perhaps it's a player instance variable. Let's say you equipped a rapier. The method of equipping isn't important in this example. The result is: set EquipRightHand:EquippedObject. Variable becomes EquipRightHand:WeaponRapier

    Now, with one action in the same event, we create new object: Sprite.EquipRightHand at the player's position.

    Additionally, if the animation names and origin points of the objects are set to match the player's, you could set every weapon/item sprite to mimic the player's position, animation and frame in one event and have every other object line up from that point on. If the player's animation frame is PlayerWalk02, the Rapier (also at PlayerWalk02) will line up perfectly on top of the player. Change the EquipRightHand variable to Scimitar, and your work is already done, provided the Scimitar's animation names and image points line up. Imagine the amount of animation you would need in one sprite if you did this via frames, and the amount of array reference you could avoid otherwise.

    The only other way to avoid a single, very large sprite sheet, as I understand, is to create many, many different events for each object, resulting in a lot of code. (IE: If WeaponEquip:"WeaponRapier", then DoSomeAction. If WeaponEquip:"WeaponScimitar", then DoSomeAction.) For this example, I'm not taking into account plugins, which are useful, but may depreciate as C2 evolves. Also, I love to see how much functionality I can squeeze out of anything in vanilla flavor.

    I realize this has been brought up and dismissed before, but I feel the usefulness of this proposed function is there. I use strings all over the place for dynamic coding, and this is one place I am sorely missing that functionality.

    One thing I've learned over the years, whether it's code, music, Photoshop, etc. is that everyone has a different method to get from A to B, and there's always something you can learn from someone else. I'd love to see how others accomplish what I'm talking about in a way we maybe haven't considered.

    I hope I haven't lost you with the long post!

    Thoughts?

  • i agree..

    for example it's hard to pick objects by their family, and random spawn them. on the corners of the window.

    in my 1 lvl game i had stuff moves from upper part to lower, and i have to spawn objects and then set each object on -y coordinate. because i can't use random object name from a list to spawn that object i have to pick exact object and spawn it, and then set it's y correctly. when i spawn something random from family i can't know which one was spawned, that's why i would see great benefit from spawning from variable / string / random in list of strings,

  • While I'm looking for as much functionality in Vanilla C2 as I can get, there's a plugin that may work well for you.

    As rexrainbow pointed out last night, the Nickname plugin lets you add some extra functionality to spawning instances, and some other cool things. I also recommend having a peek at some of the other plugins on his page.

    Nickname can be found here:

    http://c2rexplugins.weebly.com/rex_nickname.html

    (you may need to past that into your URL bar until I get some more posts under my belt.)

  • You can save and import objects from string. The only downside is that they are being imported with their original uid and z-order. Oh and I just remembered Ashley can you do something about exporting objects to ajax, so when they are imported back from string, they will be given new uid, and z-order, optionally? Cheers.

  • megatronx This sounds intriguing, though I'm not entirely clear. Could you elaborate? Would you be saving via localstorage and loading when needed...?

    As an aside, it seems the functionality is missing due to the way C2 finalizes and exports your game. Object names are not saved (with good reason).

    https://www.scirra.com/forum/viewtopic.php?t=68324&start=0

    This isn't supported because as part of minifying and obfuscating your project on export, object names are removed to make it even more difficult to reverse engineer a project.

    Allowing creating objects by a variable would then force C2 to include the original names of all objects, possibly making it easier to reverse engineer the project. TBH I think the best solution is a series of subevents like you've shown.

    Ashley what about using a UID in place of an object name?

    If I come up with an efficient way to do this, or even an event template, I'll post it as a Tut.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • My mistake, you can store object state as json and then recreate it trough json. But you can create from json. Thought I am almost sure there was option to do that a while ago.

  • I would create an object for each type of weapon that that has similar properties. For example, a single object might contain swords and daggers, and another might contain bows. Each frame in that object would be the variations of looks of that type of weapon, basically reskins. This would separate out your total weapons into several spritesheets, aiding in the loading issue.

    As for your reference/object id problem, you can use Constants if you like to refer to your object by name rather than number

    ie.

    Rapier=72

    ChickenSlicer=73

    You can use these in function, or anywhere -

    On Call EquipBlade(ChickenSlicer) - Create BladeObject, Set BladeObject animation frame to (param0). This would create the object and set it to frame 73. Constants are also easy to change in case your animation frame changes, besides which it is also pretty easy to reorder your frames in the editor.

    If all of your weapons have unique properties, it would make a lot of sense to use an array to store the data. When you get to a huge amount of unique weapons, it becomes much more manageable to keep track/edit in a spreadsheet program, and import it via project files as a CSV or XML to parse into an array on load.

    If you're worried about loading a whole bunch of weapons you don't need at once, again, you can use project files to load only the sprites you need on demand at runtime.

    Basically if you're objective is to create objects via expressions and strings, your best bet still is to utilize constants and animation frames.

    In the end, you mention best practices, but it is a pretty common advice here regarding optimization - unless you can notice a difference in practice, it is best not to worry about it and focus first on your game mechanics. I don't think it would be a big deal/difference to download 10 or 30 or 50 weapon sprites/frames in one object versus splitting them into 2 or 10 or 50 different objects. How many unique weapons do you have?

  • Davemon

    I remember reading somewhere in the forums recently that creating an object via expression -- not by forcibly picking the object -- is a security flaw when Construct 2 obfuscates the javascript code during export. The object name and properties would be revealed and your game can be reversed engineered, so Construct 2 opted to remove this feature. The answer was from an official Construct 2 developer.

    I do wish there was an option in the preferences that we can check 'I DONT CARE about people stealing my game code' that enables this feature, but I think the the reverse engineering goes deeper than just protecting our games; maybe protecting Construct 2's proprietary codes.

    Like you, I am left with the answer of creating a super sprite sheet, and tons of IF ELSE statements to control object properties.

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