Results 1 to 5 of 5

Thread: How would a ninja do it? (Space Trading game)

  1. #1

    How would a ninja do it? (Space Trading game)

    Hi all

    I'm currently working on a project, which is a space trading game. Players start with a ship and a commander and buy/sell goods on planets in order to buy upgrades to make their ship faster/attacking/defensive and so on.

    I would like to automate as much of it as possible, but I am concerned that as I have no experience at all I am going about things in a way that is going to make my life progressively harder as the project has more and more moving parts. [URL="https://xkcd.com/1513/"]Something like this issue[/URL]

    At current I have a button to "Start game", which I just use as a reset so the game tidies itself away when I save and play. The user is then presented with 4 buttons in order to choose their ship type and on doing so they are given the appropriate counters/dice/etc and the game deals card out from the relevant decks. I'm pretty sure that there's no real way of getting this wrong so far.

    I'm now looking at spawning scripting zones around planets on the board so that when an object is dropped into them (the only object that belongs on the board is a ship) they create buttons to buy/sell resources on that planet. I am struggling to deal with the scripting zones at current (I've already posted a question).

    There are a few snippets of code below which may give you and insight into what I'm doing and how - I would thoroughly appreciate any guidance at all (or a point towards a style guide of any sort) so that I can improve. I'm sorry if there's any sort of etiquette to posting this sort of thing that I am going against, I am also willing to learn that too!

    I'm using this to get position/guid

    weapon_deck = {}
    weapon_deck_guid = '3fe6ae'
    weapon_deck = getObjectFromGUID(weapon_deck_guid)
    weapon_deck_pos = {}
    weapon_deck_pos=weapon_deck.getPosition()
    weapon_deck.shuffle()

    This to create buttons:

    local button = {}
    button.position={-48,5,-5}
    button.rotation={90,180,0}
    button.label="Fighter: D4 Movement D6 ATTACK D4 Defence D4 Actions"
    button.width=14000
    button.height=1000
    button.font_size=500
    button.click_function='pick_fighter'
    button.fucntion_owner=Global
    sysobj.createButton(button)

    And this to deal out cards to points in relation to the deck, using the WX variables to store whether there is a card in slot 1, 2 and 3 (so that I can look at this value and re-deal cards to the right slot at any necessary point).

    x=1
    wpos_x=weapon_deck_pos.x+2.7
    for i=1,3 do
    local params = {}
    params.flip=true
    params.position={wpos_x,weapon_deck_pos.y,weapon_d eck_pos.z}
    weapon_deck.takeObject(params)
    if x==1 then wpos_x=wpos_x+3 end
    if x==2 then wpos_x=wpos_x+2.7 end
    local x=x+1
    end
    W1=1
    W2=1
    W3=1

  2. #2
    We usually use code tags (see the '#' button in the message editing formatting selectors).

    But I haven't run into any style Nazi's in these forums... but if you google "Lua code" you'll get links to all kinds of examples of the 'standard' style. Most important thing is adopting a strict approach to variable/table naming, and indentation. Done consistently, both will make your life much easier when it comes to the 'why doesn't this work?' stage. Most people use the lowerUpperUpper naming style like weaponDeckPos. There's also a good argument for just using all lowercase and underscores like you did - weapon_deck_pos. Use whichever you like, it shouldn't phase anyone trying to help you either way.

    Indentation helps you know the boundaries of chunks of code. For your last snippet, the indentation below makes the start and end of the function and 'for' loop obvious.
    Code:
    function set_weapon_pos()
        local x=1
        wpos_x=weapon_deck_pos.x+2.7
        for i=1,3 do
            local params = {}
            params.flip=true
            params.position={wpos_x,weapon_deck_pos.y,weapon_d eck_pos.z}
            weapon_deck.takeObject(params)
            if x==1 then wpos_x=wpos_x+3 end
            if x==2 then wpos_x=wpos_x+2.7 end
            x=x+1
        end
        W1=1
        W2=1
        W3=1
    end
    Note that I moved the 'local' tag on the 'x' variable from near the bottom to the top. The 'local' tag just only go on the very first instance of a variable/table within a section of code that it exists in. As far as I know, it causes Lua to create a new variable which in your version would mean the 'x' at the end would not be the same variable as the 'x' at the beginning. Even though they have the same name.

  3. #3
    Thank you very much - I'm surprised actually, I was very much expecting an "Oh my gosh, what is that?!?" reaction!

    I did do some reading on local variables and it seemed to indicate to me that local variables exist within the function/for/if that they are initialised in, do they live in a hierarchy so in your example because it's initialised in the function in general it is still 'local' to any for/ifs that are also contained within the function?

    Can you mess with global variables within the local environment only? So if I were to say (global)X=1, could I then (local)X=(global)X+1? Actually scrap that, I think I'd probably just call them different things!

    Very much appreciated

  4. #4
    I'm lacking in formal knowledge, but in my experience with learning scripting in this game, this has been my understanding of local variables:

    Code:
    x = 'x'
    
    function example()
         local y = 'y'
         if x == 'x' then
              local z = 'z'
              y = 'a'
              local x = 'b'
         end
         print(x)
         print(y)
         print(z)
    end
    The result for the prints would be:
    x
    a
    lua error: nil value

    declaring a variable as local seems to only exist within that level of indentation or further (not based on the actual indentation, but the conditional,loop, or function -- another reason to use indentation formatting to help you out).

    In the example, note that local x does not replace the global x outside of the if/then conditional.
    note that local z cannot be called due to not existing outside the if/then conditional.
    note that the use of y does not have to have 'local' in front of it as it follows after the local being declared -- and is still relevant as it is a child of the function level of indentation.

    regarding the usage of x as both a global and a local -- i wouldn't do that. I guess the local might overwrite the other, but it would be too confusing to not just use two different variables.

    note that if you wanted to use a local variable in a child paragraph, but it may not have reason to exist prior to it, you can do this:
    Code:
    function example()
         local y = {}
         if x == 'x' then
              y = 'a'
         end
    end
    keeps the local y as existing for all paragraphs under the function example().

  5. #5
    Ha ha! Not at all. Your code was more or less fine, but for a lack of indentation... and a few typos which I glossed over as finger-fumbles.

    Yes, the 'x' variable would be 'local' to the entire function, as long as no other x's were created in a subsection of it. The subsections inherit the variables of the parent sections above them, but not vice versa. It is also true that subsection local variables override parent (or global) versions. So...

    Global vs local... yes, scrap that... the local X would override the global X. Any code after the local X declaration would be unaware of the global X variable.

    Edit: Ninja'd... (LoL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •