Skip to main content

News

Topic: My Very Simple Menu System (Read 12584 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: My Very Simple Menu System
Reply #15
He's saying the stats of his characters change every time he closes and reopens the menu.  This means that, for whatever reason, his stat setting code (or worse, the characters are being re-generated wholesale) is getting called every time the menu is shown.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

Re: My Very Simple Menu System
Reply #16
I think Radnen said something about running something out of a loop. My entire menu is inside a loop, I thought this was the right way to do it. I'll examine my code and see whats really going on. It could be a minor logic-error with where I'm setting the characters stats.
I've attached the game so you guys can have a look through and we can solve this problem together ::).

After downloading the game. Extract it and run it in Sphere. I reccomend Scale 2x since my graphics are small 16x16

KEY_1 Opens up the Menu
Go to Party ( Navigate using the arrow keys )
Hit KEY_Z to select it
Choose any Member
Select Status

Notice how after reading the stats if you read them again while in the menu they are the same

Notice how after reading the stats if you close the menu and open the menu again then go back to read them, the stats change

Notice how its the same for all characters
  • Last Edit: August 09, 2013, 03:15:34 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: My Very Simple Menu System
Reply #17
Oh wow that was a pretty terrible menu design. See, what you were doing was creaing a bunch of objects in Menu() at the begenning and no sooner than using them you throw them away. In JS, just like in most languages objects you create within the scope of a function are deleted right after the function ends.

Code: (javascript) [Select]

// the gist of what you are doing:
function Show()
{
    function do_stuff() {
        //...
    }

    var party = [];
    CreateParty(party);

    do_stuff(party);
}

SetUpdateScript("Show();");


The problem with the above is that each time you call Show, a party is created, displayed and then at that last '{' bracket, the party is destroyed. The next time you see the party you have different stats, not only that you can't level up or add or remove stats.

You were also creating local functions in a continuously updating loop. I figured that would have a huge impact on the FPS and it did, your maximum FPS is 900 on my machine,  with my code fix it's at over 1800. I'll attach my fixes, they aren't pretty but it solves the problem. Basically I just created a single initializer, it runs once to add the info and then that's it. Ideally you want to have a global manager do this stuff for you. What happens if you need to add 3 hp to the 4th party member? It's not that easy in your design. In fact your menu shouldn't contain the actual items/party members/etc. Only represent them. Code design can be harder than the actual programming. You have to identify the code flow and know what values must persist (and this stay in the global scope) and what values can be thrown away.

I'd create a global static variable that holds all of the party info and then use that variable in my menu code to display party members.

Example, based on your code ideas:
Code: (javascript) [Select]

var PartyManager = (function() {

    var members = [];
    var exp = Exponential(10);
    members[0] = new Player("name_here", 85, 6, 45, 4);
    MapOn(members[0].stats, [2, 3, 5, 6, 8], function() { return exp; });
    var exp = Exponential(10);
    members[1] = new Player("name_here", 85, 6, 45, 4);
    MapOn(members[1].stats, [5, 6, 7, 9, 10], function() { return exp; });

    return {
        players: members,
        money: 0,
    }

}());

Abort(PartyManager.players[0].name);



If you ever have to do something like this:
Menu.party[4].hp += 3;

Then that's bad design, it should be something like:
PartyManager.player[4].hp += 3;

And the menu would have the changes automatically. In fact you should be able to create and throw away menus without affecting the party/items/etc in any way.
  • Last Edit: August 09, 2013, 04:44:06 am by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: My Very Simple Menu System
Reply #18
 :( I'm going to scrap everything and stat again properly. You're menu has the same problem but its my fault. I should have designed the menu better. The skeleton is the most important part and I messed that up.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: My Very Simple Menu System
Reply #19

:( I'm going to scrap everything and stat again properly. You're menu has the same problem but its my fault. I should have designed the menu better. The skeleton is the most important part and I messed that up.


Well I didn't know what values to expect. All I could do was a single initialization step if there was more to it, it was actually too hard for me to figure out. ;) Sometimes code isn't difficult but when you are learning you kinda make it more difficult, I ran into this problem long ago on my first games where it got so convoluted I had no idea where exactly to debug my code!

A good design from the beginning is always miles better. I'd say you had a good start with the menu - you just now need to focus on design. ;)
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: My Very Simple Menu System
Reply #20
Given that this is related to menu design, are there suggestions on how I can improve my menu tutorial?

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: My Very Simple Menu System
Reply #21
I would add that a menu should only be used to display info, not used to carry your actual data throughout gameplay. I think that's an important distinction for newer users.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: My Very Simple Menu System
Reply #22
Good idea!

edit: aaaaand done :)
  • Last Edit: August 09, 2013, 07:01:50 pm by N E O

Re: My Very Simple Menu System
Reply #23
I'm going re-read that tutorial, I sort of got exited and rushed into coding thinking oh I'll figure it out as I go along.
Thanks for the pointers, I've decided to simultaneously take a crash course in JavaScript too, there are a lot of simple things like using variables to store values and how to properly use and create objects that I rushed through learning or learned from seeing it in other code but never really understood the theory/method to it. These small things might have not seemed important to understand but they creep up on you. Just like the menu I need to build a foundation in Javascript so as I really know what I'm doing.

Back to the drawing board! (Always wanted to say that ) (^_^)