Enum Error: error 008: must be a constant expression; assumed zero
#1

Hello again all,

So I have a MySQL database table containing info on all of the skins, including skin ids. I've created an Enum to store the skins. I have ran an sql query and am now trying to store each row of data into it's own enum. SO basically, looping through the result of skins that comes back from the database, and then foreach one create a new Skin Enum, with it's values inside. However, it's not working. My code is below:

Код:
enum sSkin{ //skins enum
	id,
	gender_id,
	model_name[50],
	name_type[100],
	is_private
}

forward OnSkinsLoaded();
public OnSkinsLoaded(){
	//This callback runs from OnGameModeInit, selecting all skins
	//Now set skin enum foreach skin
	if(mysql_errno() == 0){
	    new rowCount = cache_get_row_count(mysql);
    	if(rowCount){
            //character(s) found
            for(new i = 0; i < rowCount; i++){
                new skinID = cache_get_field_content_int(i, "id");	//get skin id from db
                new Skin[skinID][sSkin]; //Create skin enum for skin id
                Skin[skinID][gender_id] = cache_get_field_content_int(i, "gender_id");	//gender_id
			    cache_get_field_content(i, "model_name", Skin[skinID][model_name], mysql, 50);    //model_name
			    cache_get_field_content(i, "name_type", Skin[skinID][name_type], mysql, 100);    //name_type
			    Skin[skinID][is_private] = cache_get_field_content_int(i, "is_private");	//is skin private?
            }
		}
		return true;
	}else{
	    print("ERROR LOADING SKINS FROM MYSQL DATABASE");
	    return false;
	}
}
The error I'm recieving is:

Код:
..\include\systems\character\char_core.pwn(463) : error 008: must be a constant expression; assumed zero
Now, I know exactly what the issue is. It is "new Skin[skinID][sSkin]; //Create skin enum for skin id". It's because Pawno requires the value to be a constant where it says "[skinID]".

However, does anyone know a work around for this so I can have an enum for each of the skins loaded from this callback function ive made?

Many thanks
Reply
#2

Have a constant value (like knowing that there will be aprox. 50 allowed skins) and declare it as global so it can be accessed anywhere else.
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Having a constant value and declare it as global.
But what if I dont know myself what that value would be. Not so much an issue for this I guess, as the number of skins is a constant value. However, what if I had a table of results which were added to all the time, and I wanted to set each one of those up as an enum? I wouldn't know what that constant value would be as it would be changing all the time. I hope you understand what I mean haha.
Reply
#4

Sorry for the many edits of my post.

You pretty much know that there are 312 skins include 74 which is invalid. I didn't understand your last part, can you explain more please?
Reply
#5

Yes I understand that what you said on the skins, and I've fixed my issue with the way you suggested. I'll explain the last part some more trying to use an example, I'll use houses as my example.

Say you have a MySQL table called house. This table has a row for each house on your server. It contains the following values: HouseID, OwnerID. I know realistically there would be more data than that, but just for ease.

Anyways, so you create an Enum in your script called House. It would look like the following:

Код:
enum House{
	HouseID,
	OwnerID
}
Whilst in game, an admin can create a new house. Therefore the MySQL house table will always be getting new rows. There is no way to determine what CONSTANT NUMBER OF ROWS the House table would hold, as it can be added to any time.

So my questions is, if I wanted to pull from the database all of the houses, and then instantiate a new House enum for each one, how would I do this? I wouldn't be able to put something like "new Skin[MAX_HOUSES][House];" because I wouldn't be able to define exactly how many houses there is going to be, as this would be a dynamic value. Sometimes there might be 20 houses, othertimes there might be 35 houses, it could be any amount.

From my logic, I would need to get the row count from the MySQL after query execution and do something like "new Skin[sqlRowCount][House];", but that wouldn't work as sqlRowCount wouldn't be a constant value.

I hope that makes sense and you can understand what I'm asking from that.
Reply
#6

Make a variable that holds the number of houses. For starters, make it ~40. When your gamemode starts and mysql loads up the houses, use mysql to retrieve how many rows there are. So you get the number of rows (houses) and then set that number for the variable that holds the number of houses. Lets say an admin uses a command to add a house. When the admin uses the command, just do ++ to the variable that holds the number of houses. And that's it.

Whole idea:
Код:
new MAX_HOUSES=40,Skin[MAX_HOUSES][House];

enum House
{
	HouseID,
	OwnerID
}


public OnGameModeInit()
{
	// mysql loading
	MAX_HOUSES=mysql_rows;
}

CMD:addhouse(playerid, params[])
{
	// add house...
	MAX_HOUSES++;
	return 1;
}
I'm not sure if this would be the best idea, but that would be how I would do it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)