• Godot Help
  • Looping over a variable with more than one value

Am trying to convert an old actionscript quiz game to Godot.
In AS you could state the variables within the for loop, like so...

function hideAnswerAssets() {
	for (var ix:int= 0; ix<maxOption; ix++) 
	{
		quizzes["button"+ix].visible = false;
		quizzes["Answer"+ix+"Display"].text = "";
	}
}

I'm really struggling with how to convert a simple AS function into Godot.
Any help would be much appreciated.

for i in range(max_option):
    # do something with i

    Thanks matey. Much appreciated.
    So far so good.

    6 days later

    cybereality

    Thanks again for your help. I can't test it because there's a whole lot more of the quiz logic to be added. But if I could ask...
    does this godot syntax look right to you considering the original AS?

    func hideAnswerAssets():

    var button
    var AnswerDisplay
    
    for ix in range(maxOption):
    	#ix = 0;	
    	ix+=1;
    	button = int(+ix)
    	AnswerDisplay = int(+ix)
    	$Quizzes["button"].visible = false;		
    	$Quizzes["AnswerDisplay"].text = "";

    The original AS is to count the answers starting from 0.
    I admit I did get a lot of help with the arrays on my original game and never fully understood the logic.
    I started with the Hide Answers function b/c that seemed the simplest to begin with in my godot learning.

    No, that code doesn't really make sense. You can do something like this:

    func hideAnswerAssets():
    	for ix in range(maxOption):
    		$Quizzes.get_node("button"+str(ix)).visible = false;
    		$Quizzes.get_node("Answer"+str(ix)+"Display").text = "";

    There are easier ways to do it, but that would be a 1:1 port of the code.

      I mean, that is how to convert AS3 to GDScript. If you were writing in GDScript you could do this:

      var quizzes = get_node("Quizzes")
      for quiz in quizzes:
          if quiz is Button:
              quiz.visible = false
          if quiz is Label:
              quiz.text = ""

        cybereality Thank you. I appreciate your help in explaining this for me.

        I was wondering if I could trouble you again. I'm still struggling with the 'looping through arrays' logic for other parts of the quiz and converting to GD. The questions/answers are pulled from json files (they were xml) on my server. They were randomised. I found a godot quiz game on github that I'm using for reference but questions/answers in this game are not. It's not challenging to a player if they appear in the same order.

        In GD I'm not understanding the difference between rand_range, randi and randf.

        This was the AS for shuffling questions...

        //////////////// SHUFFLE ARRAY /////////////////////////

        function ShuffleArray(input:Array) 
        {
        	for (var i:int = input.length-1; i >=0; i--) {
        		var randomIndex:int = Math.floor(Math.random()*(i+1));
        		var tempValue = input[randomIndex];
        		input[randomIndex] = input[i];
        		input[i] = tempValue;
        	}
        }

        //////////////PUSH NUMBER OF QUESTIONS INTO ARRAY//////////////////

        function shuffleNumber(arr:Array, num:int, rand:String) 
        {
        	if (arr.length > 0) {
        		arr = [];
        	}
        	for (var i:int = 0; i <num; i++) {
        		arr[i] = i;
        	}
        	if (rand == "TRUE") {
        			ShuffleArray(arr);
        	}		
        }

        Am more than certain that my GD conversion is totally wrong.

         func ShuffleArray(input:Array): 
        	#i >=0
        	#i = input.size()-1 
        	for i in range():
        		i-=1
        		var randomIndex:int = randi()*(i+1)
        		var tempValue = input[randomIndex];
        		input[randomIndex] = input[i];
        		input[i] = tempValue;
        
        func setupAnswerOptions(op:int):
        	for i in range(op): 
        		i+=1
        		var mc = $Quizzes.get_node("button"+str(i));
        		mc.visible = true;
        		mc.id = i;
        		#checkAnswer();