Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Various true beginner questions about ebook template, lua coding and tnt animator — Gideros Forum

Various true beginner questions about ebook template, lua coding and tnt animator

piepie Member
edited February 2013 in General questions
Hi,
sorry if my questions are stupid, I am a total beginner to LUA and game development (from code pov).
I am no coder, I worked as a game designer some years ago.
Now I'm trying to learn something new and I found Gideros.

I've read the gideros ultimate guide, this lua tutorial (http://luatut.com/), and some code example: I think I can mostly understand what happens, (I can follow the logic) but I'm clearly missing something "basic" :).

The only succesfull way I know to learn something, is to face it in the field: choose an easy target and try to achieve it, then add complexity and so on... so I started out with the ebook example.

I added sprites around in the first page (page0) and experimented editing code here and there: it works as expected, but I still can't understand some things:

(_QUESTION_0_)
is "self" actually a keyword, referring to the function it is included in (like saying "myself" if I were the function)?
Or could this be any_word_that_could_be_a_meaningful_variable?

I imported a character (a dog, an "actor") with animations made with TNT animator:
following the provided example code, I placed it inside the Page0:init function code, as another function (every example provided with TNTanimator "include the actor" as a function:
I don't think that using a function is needed for my actual purpose, but since I'm trying to learn, it's ok: more things to learn, less to change in the original code from Gregbug - thank you for tntanimator :) ).

Then I tried to apply to my actor (dog) the code found about the "bird" on Page4 (same ebook template).

My Goal: on mouseclick/touch
I'd like to change the dog animation (i figured out to use setAnimation("NAME")) and GTWEEN the dog outside the screen. But I fear I'm missing something important.

I noted down my other questions as comments in the following code (sorry if it's not highlighted properly, maybe an admin could help.. I used this FAQ http://giderosmobile.com/DevCenter/index.php/Forum_FAQ#How_can_I_highlight_my_Lua_code_in_the_forum_post.3F but it doesn't seem to work - at least in preview mode).
 
function Page0:init( pageNo, parent)
-- [[ _QUESTION_1_:  is ".no"  a variable of the function Page0:init?  ".background" is also a variable of the same function? ]]--
 
	self.no = pageNo
	self.background = Bitmap.new(TextureRegion.new(Texture.new("assets/bg.png", true)))	
 
	self.button= Bitmap.new(TextureRegion.new(Texture.new("assets/button.png", true)))	
 
	-- just read texture pack dog
	local dogTexturePack = TexturePack.new("assets/dog.txt", "assets/dog.png")
	local dogLoader = CTNTAnimatorLoader.new()
	dogLoader:loadAnimations("assets/dog.tan", dogTexturePack, true) 
 
--[[ _QUESTION_2_ According to the example provided with TNTanimator I need to declare the dog class as Sprite: does using CORE.Class(Sprite) instead of GIDEROS.Class(Sprite) (as seen in other examples) make any difference? ]]
 
	dog = Core.class(Sprite)
 
	function dog:init()
		self.dog= CTNTAnimator.new(dogLoader)
		self.dog:setAnimation("DOG_STOP")
 
-- [[ _QUESTION_3_ : What does the following line exactly do? If I remove it, I remove the dog from stage. But I expected that it was self:addChild(object) "giving birth" to things. (see below, QUESTION5 ) ]]--
 
		self.dog:addToParent(self)
 
 
		self.dog:playAnimation()
 
-- _QUESTION_4_: why SELF: and not SELF.DOG? (self.dog places dog to 0,0 on screen)
 
		self:setPosition((Swidth /2),(Sheight -150))
	end
 
 
		self:addChild(self.background)
		self:addChild(self.button)
 
--[[ _QUESTION_5_ (this is the line I was referring to in question3) . Is there another way/method to "call my dog?" ]]-- 
		self:addChild(dog.new())
 
 
 
		--this is coming from page4	and needed for touch event
		self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self)	
		self:addEventListener(Event.REMOVED_FROM_STAGE, self.onRemovedFromStage, self)	
 
 
	end
 
		--this is also coming from Page4: I just changed the names accordingly.
		function Page0:onMouseDown(event)
 
--[[ _QUESTION_6_ : The following line of code on touch gives ERROR "attempt to index field 'dog' (a nil value)" ]]--
 
		if (self.dog:hitTestPoint(event.x, event.y) == true) then 
		print("TOUCH")
 
--[[
If I change it this way:
 
		if (dog:hitTestPoint(event.x, event.y) == true) then 
		print("TOUCH")
 
(dog instead of self.dog) 
I get this other error "index '__userdata' cannot be found" 
What am I doing wrong? What is __userdata?
 
 
 I also tried changing the hittestPoint area to a Bitmap (button in my code), which is working: but then I have another error trying to GTWeen the dog.
(gtween.lua:163: attempt to index field 'target' (a nil value))
 Gtween was working when the dog was an AnimatedSprite()
 
I suppose I can't refer to TNTanimatedObjects as I would refer to AnimatedSprites or Bitmaps in GTween lib. 
How do I address those?
 
]]--
		end
 
	end	
 
	function Page0:onAddedToStage()
		-- we need mouse functions to interact with the toy
		self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	end
 
 
	function Page0:onRemovedFromStage()
 
	end


QUESTION7: I am not sure what I am asking right now :) : Is there a way (code, trick, magic) to "explode" the lua metatables and/or print the object hierarchy for debugging purposes?
I am not either sure they are named like this- I just read about those "metatables" and if I write "print(dog)" in the lua code after I create the dog I get a Table: (id?)
If I could see what's inside an object maybe it could be easier for me to understand how things work.

I think it's everything for now, before I get stuck again :)
Thank you for reading my whole post, I hope that I didn't miss anything, and that someone can share some knowledge with me.

kind regards, thank you in advance and keep gideros up! :)
Pie.


Comments

  • zvardinzvardin Member
    Accepted Answer
    Answer 1: "no" and "background" are variables of a Page0 instance. When a function is defined with a colon, "self" is a parameter handled automatically by lua, which is generally used to refer to an instance of an object.

    Answer 2: Core.class is the standard way now, and Gideros.class was an old way of doing it (although it is still supported AFAIK)

    Answer 3: addToParent adds to whatever Display (Spirte) object is higher up in the hierarchy, I would assume (I don't see it in the normal Gideros documentation however)

    Answer 4: This really depends on your setup, but in this case you have a containing sprite that dog is a part of. This can be useful for a number of reasons (maybe you have an effect that appears on top of the dog and you want to be able to move the dog and the effect together)

    Answer 5: dog.new() is making an instance of a dog class. This allows you to make multiple dogs. The "addChild" etc functions are what make those objects actually get displayed. You could have classes that are only for data, but have nothing to display on the screen, so you might not use "addChild" with them.

    Answer 6: Your Page0 class doesn't seem to have a dog variable set. "self.dog = " is in your dog class, but the function "Page0:onMouseDown(event)" would have no access to that, so you need to store your dog in a Page0 instance if you need to access it:
     
    -- in question 5, changing the code to this would probably work:
    self.dog = dog.new()
    self:addChild(self.dog)
    Answer 7: A simple way to spit out a table is to do:
    -- this will not print out the metatable, but will do all normal key/values
    for k,v in pairs(table) do
      print(k,v)
    end
     
    -- for metatable you could fetch it and do
    local mt = getmetatable(table)
    if mt then
      for k,v in pairs(mt) do
       print(k,v)
    end
    end
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    My main suggestion while writing all answers would be to drop all the templates and additional tools and try to create something from the scratch without any of the helping libs, that is if you truely want to learn and understand something, you need to understand the basics behind them ;)

    So if anything does not make sense, just ask for more ;)

    QUESTION 0
    self is a keyword in Gideros class system that references the instance of the class internally. If that makes any sense, if not I can try explaining with some examples :)
    But basically yes as you said, only you can use it across all functions of same class. Meaning it will be the same in all function where you have Page0 defined before the name of the function.

    QUESTION_1_

    You can define your own arguments as you please, it's not something predefined. It is assigned so it could be accessed from any method (method is a function of a class) so if inside init (the first function which is called when class is created) you define self.no then inside any method you can use self.no value or define any other value or define any other property (property is a variables that instance has as self.no :) )

    _QUESTION_2_
    Gideros.class is deprecated and should not be used (because it can be dropped at any moment and your code will just break), so you should use Core.class.
    In idea there should be no difference (only if @atilim keeps updating Core.class function and not gideros.class, then Core.class may have some additional features)

    _QUESTION_3_
    As all the provided animations are somehow handled inside CTNTAnimator class @GregBUG decided that it is more convenient for the user to provide the parent, so he could do all the addChild himself inside the class as he pleases to. Basically it allows to operate inside the class more freely. There is still addChild inside there somewhere. :)

    _QUESTION_4_
    Because if you look, you are inside dog:init method now, which means that there self is referenced to dog class instance. Basically you will have a hierarchy as:
    Page0 -> dog(which is Sprite) -> dog(which is CTNTAnimator)
    This another layer dog(which is Sprite) is added for convenience, basically Sprite object allows you to group and layer you hierarchy, but you could do all the same without it.

    _QUESTION_5_
    And here basically you add you dog(which is Sprite) instance to Page0. And as you remember inside dog:init method you have provided dog(which is Sprite) as a parent to dog(which is CTNTAnimator)

    _QUESTION_6_
    Yes because dog is a class and not an instance of a class. And self.dog was created inside dog:init method which also was another scope (in dog class scope).

    If you would like to access it from any Page0 method, you need to make it as property and define as self.dogAnimation for example inside Page0:init method. Then it can be accessed as self.dogAnimation in any method of Page0 like this:
    self.dogAnimation = dog.new()
    self:addChild(self.dogAnimation)
    And QUESTION7

    For printing I'm usually using this function:
    --recursive print of tables
    function print_r (t, indent, done)
      done = done or {}
      indent = indent or ''
      local nextIndent -- Storage for next indentation value
      for key, value in pairs (t) do
        if type (value) == "table" and not done [value] then
          nextIndent = nextIndent or
              (indent .. string.rep(' ',string.len(tostring (key))+2))
              -- Shortcut conditional allocation
          done [value] = true
          print (indent .. "[" .. tostring (key) .. "] => Table {");
          print  (nextIndent .. "{");
          print_r (value, nextIndent .. string.rep(' ',2), done)
          print  (nextIndent .. "}");
        else
          print  (indent .. "[" .. tostring (key) .. "] => " .. tostring (value).."")
        end
      end
    end
    Hope that helps ;)

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you very much, those answers really helped me.

    There's still something I can't figure out at first glance: it's time for me to experiment some more "under those lights" :D

    thank you again,
    kind regards
  • hgvyas123hgvyas123 Guru
    Accepted Answer
    By the way it must have to be start with question 1 as in lua everything starts with 1

    :))

    Likes: phongtt, pie, tytadas

    +1 -1 (+3 / -0 )Share on Facebook
Sign In or Register to comment.