Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   For loop problem (https://forums.graalonline.com/forums/showthread.php?t=134268211)

JohnnyChimpo 05-13-2013 06:01 PM

For loop problem
 
Why does this not put the file string into temp.filename array, the output of this is 0.
PHP Code:


const lngNumber 8;

function 
onActionServerside() {
  for (
temp.=0;i<lngNumber;i++){
    
temp.filename[i] = "levels/translations/server_de.po";
  }


This isn't all the script obviously but when i take the for loop out everything works correctly i'm stumped. Thanks in advance.

callimuc 05-13-2013 07:18 PM

tryo to make it

PHP Code:

const lngNumber 8;

function 
onActionServerside() {
  
temp.filename NULL//dont know why, just prefer to set it to NULL before using it
  
for (temp.=0;i<lngNumber;i++){
    
temp.filename.add("levels/translations/server_de.po");
  }



cbk1994 05-13-2013 08:44 PM

What are you doing with the variable? You haven't even defined it as an array, so you definitely can't access it like that.

Cubical 05-13-2013 09:06 PM

Quote:

Originally Posted by JohnnyChimpo (Post 1717900)
Why does this not put the file string into temp.filename array, the output of this is 0.
PHP Code:


const lngNumber 8;

function 
onActionServerside() {
  for (
temp.=0;i<lngNumber;i++){
    
temp.filename[i] = "levels/translations/server_de.po";
  }


This isn't all the script obviously but when i take the for loop out everything works correctly i'm stumped. Thanks in advance.

Why not just store the languages like this
PHP Code:

function onChangeLanguage(lang){
temp.translationfiles = {
{
"English""en"},
{
"Dutch""de"},
{
"Troll""tr"}
};
for(
temp.translationfile temp.translationfiles){
  if(
temp.translationfile[0]  == lang){
// then for your path do something like and whatever else you want to do
loadtranslation("levels/translations/server_" temp.translationfile[1] @ ".po");


I typed this on my phone so by the time i finished i don't know if it went where it needed to go to answer your original question and im sure the formatting is off.

JohnnyChimpo 05-14-2013 03:38 PM

Quote:

Originally Posted by callimuc (Post 1717903)
tryo to make it

PHP Code:

const lngNumber 8;

function 
onActionServerside() {
  
temp.filename NULL//dont know why, just prefer to set it to NULL before using it
  
for (temp.=0;i<lngNumber;i++){
    
temp.filename.add("levels/translations/server_de.po");
  }



That worked thank you very much callimuc, i just have one thing to say however. In the whole grand scheme of things isn't quite inefficient to go and add another function call there? This language should accept the form i had written the first time. That and i could be wrong, but it would be a better means of doing it, based on the two.

callimuc 05-14-2013 06:20 PM

Quote:

Originally Posted by JohnnyChimpo (Post 1717929)
That worked thank you very much callimuc, i just have one thing to say however. In the whole grand scheme of things isn't quite inefficient to go and add another function call there? This language should accept the form i had written the first time. That and i could be wrong, but it would be a better means of doing it, based on the two.

only working with player/npcs attributes. but only because they are only defined.

like cbk said, you didt define it as a array yet. if you really want to use your version you can try this

PHP Code:

const lngNumber 8;

function 
onActionServerside() {
  
temp.filename NULL//like i said before, i prefer to do this
  
for (temp.j=0;temp.j<lngNumbers;temp.j++) {
    
temp.filename.add(""); //this way you are at least creating an array, it will be blank
  
}

  for (
temp.=0;temp.i<lngNumber;temp.i++){
    
temp.filename[i] = "levels/translations/server_de.po";
  }


you have to create the array by using the version i did (using .add("something");) or by creating it like temp.array = {"hi", "", NULL, player.head, 5, ": D"};

cbk1994 05-15-2013 12:18 AM

Quote:

Originally Posted by JohnnyChimpo (Post 1717929)
That worked thank you very much callimuc, i just have one thing to say however. In the whole grand scheme of things isn't quite inefficient to go and add another function call there? This language should accept the form i had written the first time. That and i could be wrong, but it would be a better means of doing it, based on the two.

The first way you wrote it didn't define the array.

You could do...

PHP Code:

// create an array with 8 elements and fill them all with the same thing
const LANGUAGE_COUNT 8;

function 
onActionServerside() {
  
temp.filenameArray = new[LANGUAGE_COUNT]; // this is better than callimuc's version
  
  
for (temp.0temp.LANGUAGE_COUNTtemp.++) {
    
temp.filenameArray[temp.i] = "levels/translations/server_de.po";
  }


but it's a bit silly. Is it faster than using array.add()? Uh, probably, but you'll never notice and there's no reason to over-optimize the code at the expense of readability. I almost never see arrays defined like this (I actually had to look up the syntax to make sure I was right).

PHP Code:

// create an array with 8 elements and fill them all with the same thing
function onActionServerside() {
  
temp.filenameArray = {};
  
  for (
temp.0temp.8temp.++) {
    
temp.filenameArray.add("levels/translations/server_de.po");
  }


I'm not really sure what you're trying to do.

edit: saw this in your other thread, this is better than all of the above:

PHP Code:

function onCreated() {
  
this.languages = {"de","es","fr","it","ne","no","po","sw"}; 
  
this.fileArray = {};
  
  for (
temp.lang this.languages) {
    
this.fileArray.add("levels/translations/server_" temp.lang ".po");
  }



callimuc 05-15-2013 11:31 PM

Quote:

Originally Posted by cbk1994 (Post 1717947)
The first way you wrote it didn't define the array.

You could do...

[PHP]temp.filenameArray = new[LANGUAGE_COUNT];

never really played with this kind of new stuff (mostly because i didnt need it yet) but cool to know


All times are GMT +2. The time now is 02:15 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.